xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 05bd46c030030ed6799b18cc90dea272bcd1ac5f)
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>
5b7f5c055SJed Brown #include <petsc/private/kernels/blockmatmult.h>
6b7f5c055SJed Brown #include <petsc/private/kernels/blockinvert.h>
7552f7358SJed Brown 
8b09969d6SVaclav Hapla PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList;
958cd63d5SVaclav Hapla 
109318fe57SMatthew G. Knepley /* External function declarations here */
119318fe57SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm);
129318fe57SMatthew G. Knepley 
13e600fa54SMatthew G. Knepley /* This copies internal things in the Plex structure that we generally want when making a new, related Plex */
14e600fa54SMatthew G. Knepley PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, DM dmout)
15e600fa54SMatthew G. Knepley {
16e600fa54SMatthew G. Knepley   const DMBoundaryType *bd;
17e600fa54SMatthew G. Knepley   const PetscReal      *maxCell, *L;
18e600fa54SMatthew G. Knepley   PetscBool             isper, dist;
19e600fa54SMatthew G. Knepley 
20e600fa54SMatthew G. Knepley   PetscFunctionBegin;
21e600fa54SMatthew G. Knepley   if (copyPeriodicity) {
229566063dSJacob Faibussowitsch     PetscCall(DMGetPeriodicity(dmin, &isper, &maxCell, &L, &bd));
239566063dSJacob Faibussowitsch     PetscCall(DMSetPeriodicity(dmout, isper,  maxCell,  L,  bd));
24e600fa54SMatthew G. Knepley   }
259566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dmin, &dist));
269566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeSetDefault(dmout, dist));
27e600fa54SMatthew G. Knepley   ((DM_Plex *) dmout->data)->useHashLocation = ((DM_Plex *) dmin->data)->useHashLocation;
28e600fa54SMatthew G. Knepley   PetscFunctionReturn(0);
29e600fa54SMatthew G. Knepley }
30e600fa54SMatthew G. Knepley 
319318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
329318fe57SMatthew G. Knepley    - Share the DM_Plex structure
339318fe57SMatthew G. Knepley    - Share the coordinates
349318fe57SMatthew G. Knepley    - Share the SF
359318fe57SMatthew G. Knepley */
369318fe57SMatthew G. Knepley static PetscErrorCode DMPlexReplace_Static(DM dm, DM *ndm)
379318fe57SMatthew G. Knepley {
389318fe57SMatthew G. Knepley   PetscSF               sf;
399318fe57SMatthew G. Knepley   DM                    dmNew = *ndm, coordDM, coarseDM;
409318fe57SMatthew G. Knepley   Vec                   coords;
419318fe57SMatthew G. Knepley   PetscBool             isper;
429318fe57SMatthew G. Knepley   const PetscReal      *maxCell, *L;
439318fe57SMatthew G. Knepley   const DMBoundaryType *bd;
449318fe57SMatthew G. Knepley   PetscInt              dim, cdim;
459318fe57SMatthew G. Knepley 
469318fe57SMatthew G. Knepley   PetscFunctionBegin;
479318fe57SMatthew G. Knepley   if (dm == dmNew) {
489566063dSJacob Faibussowitsch     PetscCall(DMDestroy(ndm));
499318fe57SMatthew G. Knepley     PetscFunctionReturn(0);
509318fe57SMatthew G. Knepley   }
519318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
529566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dmNew, &dim));
539566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
549566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dmNew, &cdim));
559566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
569566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sf));
579566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sf));
589566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmNew, &coordDM));
599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmNew, &coords));
609566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, coordDM));
619566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coords));
629318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
639566063dSJacob Faibussowitsch   PetscCall(DMFieldDestroy(&dm->coordinateField));
649318fe57SMatthew G. Knepley   dm->coordinateField = dmNew->coordinateField;
6561a622f3SMatthew G. Knepley   ((DM_Plex *) dmNew->data)->coordFunc = ((DM_Plex *) dm->data)->coordFunc;
669566063dSJacob Faibussowitsch   PetscCall(DMGetPeriodicity(dmNew, &isper, &maxCell, &L, &bd));
679566063dSJacob Faibussowitsch   PetscCall(DMSetPeriodicity(dm, isper, maxCell, L, bd));
689566063dSJacob Faibussowitsch   PetscCall(DMDestroy_Plex(dm));
699566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
709318fe57SMatthew G. Knepley   dm->data = dmNew->data;
719318fe57SMatthew G. Knepley   ((DM_Plex *) dmNew->data)->refct++;
729566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(dm));
739566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL));
749566063dSJacob Faibussowitsch   PetscCall(DMGetCoarseDM(dmNew,&coarseDM));
759566063dSJacob Faibussowitsch   PetscCall(DMSetCoarseDM(dm,coarseDM));
769566063dSJacob Faibussowitsch   PetscCall(DMDestroy(ndm));
779318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
789318fe57SMatthew G. Knepley }
799318fe57SMatthew G. Knepley 
809318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
819318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
829318fe57SMatthew G. Knepley    - Swap the coordinates
839318fe57SMatthew G. Knepley    - Swap the point PetscSF
849318fe57SMatthew G. Knepley */
859318fe57SMatthew G. Knepley static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
869318fe57SMatthew G. Knepley {
879318fe57SMatthew G. Knepley   DM              coordDMA, coordDMB;
889318fe57SMatthew G. Knepley   Vec             coordsA,  coordsB;
899318fe57SMatthew G. Knepley   PetscSF         sfA,      sfB;
909318fe57SMatthew G. Knepley   DMField         fieldTmp;
919318fe57SMatthew G. Knepley   void            *tmp;
929318fe57SMatthew G. Knepley   DMLabelLink     listTmp;
939318fe57SMatthew G. Knepley   DMLabel         depthTmp;
949318fe57SMatthew G. Knepley   PetscInt        tmpI;
959318fe57SMatthew G. Knepley 
969318fe57SMatthew G. Knepley   PetscFunctionBegin;
979318fe57SMatthew G. Knepley   if (dmA == dmB) PetscFunctionReturn(0);
989566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmA, &sfA));
999566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmB, &sfB));
1009566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sfA));
1019566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmA, sfB));
1029566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmB, sfA));
1039566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) sfA));
1049318fe57SMatthew G. Knepley 
1059566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmA, &coordDMA));
1069566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmB, &coordDMB));
1079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) coordDMA));
1089566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmA, coordDMB));
1099566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmB, coordDMA));
1109566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) coordDMA));
1119318fe57SMatthew G. Knepley 
1129566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmA, &coordsA));
1139566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmB, &coordsB));
1149566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) coordsA));
1159566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmA, coordsB));
1169566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmB, coordsA));
1179566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) coordsA));
1189318fe57SMatthew G. Knepley 
1199318fe57SMatthew G. Knepley   fieldTmp             = dmA->coordinateField;
1209318fe57SMatthew G. Knepley   dmA->coordinateField = dmB->coordinateField;
1219318fe57SMatthew G. Knepley   dmB->coordinateField = fieldTmp;
1229318fe57SMatthew G. Knepley   tmp       = dmA->data;
1239318fe57SMatthew G. Knepley   dmA->data = dmB->data;
1249318fe57SMatthew G. Knepley   dmB->data = tmp;
1259318fe57SMatthew G. Knepley   listTmp   = dmA->labels;
1269318fe57SMatthew G. Knepley   dmA->labels = dmB->labels;
1279318fe57SMatthew G. Knepley   dmB->labels = listTmp;
1289318fe57SMatthew G. Knepley   depthTmp  = dmA->depthLabel;
1299318fe57SMatthew G. Knepley   dmA->depthLabel = dmB->depthLabel;
1309318fe57SMatthew G. Knepley   dmB->depthLabel = depthTmp;
1319318fe57SMatthew G. Knepley   depthTmp  = dmA->celltypeLabel;
1329318fe57SMatthew G. Knepley   dmA->celltypeLabel = dmB->celltypeLabel;
1339318fe57SMatthew G. Knepley   dmB->celltypeLabel = depthTmp;
1349318fe57SMatthew G. Knepley   tmpI         = dmA->levelup;
1359318fe57SMatthew G. Knepley   dmA->levelup = dmB->levelup;
1369318fe57SMatthew G. Knepley   dmB->levelup = tmpI;
1379318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1389318fe57SMatthew G. Knepley }
1399318fe57SMatthew G. Knepley 
1409318fe57SMatthew G. Knepley static PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
1419318fe57SMatthew G. Knepley {
1429318fe57SMatthew G. Knepley   DM             idm;
1439318fe57SMatthew G. Knepley 
1449318fe57SMatthew G. Knepley   PetscFunctionBegin;
1459566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolate(dm, &idm));
1469566063dSJacob Faibussowitsch   PetscCall(DMPlexCopyCoordinates(dm, idm));
1479566063dSJacob Faibussowitsch   PetscCall(DMPlexReplace_Static(dm, &idm));
1489318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1499318fe57SMatthew G. Knepley }
1509318fe57SMatthew G. Knepley 
1519318fe57SMatthew G. Knepley /*@C
1529318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
1539318fe57SMatthew G. Knepley 
1549318fe57SMatthew G. Knepley   Collective
1559318fe57SMatthew G. Knepley 
1569318fe57SMatthew G. Knepley   Input Parameters:
1579318fe57SMatthew G. Knepley + DM        - The DM
1584f9ab2b4SJed Brown . degree    - The degree of the finite element or PETSC_DECIDE
1599318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
1609318fe57SMatthew G. Knepley 
1619318fe57SMatthew G. Knepley   Level: advanced
1629318fe57SMatthew G. Knepley 
1639318fe57SMatthew G. Knepley .seealso: PetscFECreateLagrange(), DMGetCoordinateDM()
1649318fe57SMatthew G. Knepley @*/
1659318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscPointFunc coordFunc)
1669318fe57SMatthew G. Knepley {
1679318fe57SMatthew G. Knepley   DM_Plex      *mesh = (DM_Plex *) dm->data;
1689318fe57SMatthew G. Knepley   DM            cdm;
1699318fe57SMatthew G. Knepley   PetscDS       cds;
1709318fe57SMatthew G. Knepley   PetscFE       fe;
1719318fe57SMatthew G. Knepley   PetscClassId  id;
1729318fe57SMatthew G. Knepley 
1739318fe57SMatthew G. Knepley   PetscFunctionBegin;
1749566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
1759566063dSJacob Faibussowitsch   PetscCall(DMGetDS(cdm, &cds));
1769566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(cds, 0, (PetscObject *) &fe));
1779566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId((PetscObject) fe, &id));
1789318fe57SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
1799318fe57SMatthew G. Knepley     PetscBool      simplex;
1809318fe57SMatthew G. Knepley     PetscInt       dim, dE, qorder;
1815f80ce2aSJacob Faibussowitsch     PetscErrorCode ierr;
1829318fe57SMatthew G. Knepley 
1839566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &dim));
1849566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &dE));
1859318fe57SMatthew G. Knepley     qorder = degree;
1869566063dSJacob Faibussowitsch     ierr = PetscObjectOptionsBegin((PetscObject) cdm);PetscCall(ierr);
1879566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBoundedInt("-coord_dm_default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0));
1889566063dSJacob Faibussowitsch     ierr = PetscOptionsEnd();PetscCall(ierr);
1894f9ab2b4SJed Brown     if (degree == PETSC_DECIDE) fe = NULL;
1904f9ab2b4SJed Brown     else {
1918c4475acSStefano Zampini       PetscCall(DMPlexIsSimplex(dm, &simplex));
1929566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, degree, qorder, &fe));
1934f9ab2b4SJed Brown     }
1949566063dSJacob Faibussowitsch     PetscCall(DMProjectCoordinates(dm, fe));
1959566063dSJacob Faibussowitsch     PetscCall(PetscFEDestroy(&fe));
1969318fe57SMatthew G. Knepley   }
1979318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
1989318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1999318fe57SMatthew G. Knepley }
2009318fe57SMatthew G. Knepley 
2011df5d5c5SMatthew G. Knepley /*@
2021df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
2031df5d5c5SMatthew G. Knepley 
204d083f849SBarry Smith   Collective
2051df5d5c5SMatthew G. Knepley 
2061df5d5c5SMatthew G. Knepley   Input Parameters:
2071df5d5c5SMatthew G. Knepley + comm - The communicator for the DM object
2081df5d5c5SMatthew G. Knepley . dim - The spatial dimension
2091df5d5c5SMatthew G. Knepley . simplex - Flag for simplicial cells, otherwise they are tensor product cells
2101df5d5c5SMatthew G. Knepley . interpolate - Flag to create intermediate mesh pieces (edges, faces)
2111df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
2121df5d5c5SMatthew G. Knepley 
2131df5d5c5SMatthew G. Knepley   Output Parameter:
2141df5d5c5SMatthew G. Knepley . dm - The DM object
2151df5d5c5SMatthew G. Knepley 
2161df5d5c5SMatthew G. Knepley   Level: beginner
2171df5d5c5SMatthew G. Knepley 
2181df5d5c5SMatthew G. Knepley .seealso: DMSetType(), DMCreate()
2191df5d5c5SMatthew G. Knepley @*/
2200fdc7489SMatthew Knepley PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
2211df5d5c5SMatthew G. Knepley {
2221df5d5c5SMatthew G. Knepley   DM             dm;
2231df5d5c5SMatthew G. Knepley   PetscMPIInt    rank;
2241df5d5c5SMatthew G. Knepley 
2251df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2269566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, &dm));
2279566063dSJacob Faibussowitsch   PetscCall(DMSetType(dm, DMPLEX));
2289566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2299566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
230ce78fa2fSMatthew G. Knepley   switch (dim) {
231ce78fa2fSMatthew G. Knepley   case 2:
2329566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject) dm, "triangular"));
2339566063dSJacob Faibussowitsch     else         PetscCall(PetscObjectSetName((PetscObject) dm, "quadrilateral"));
234ce78fa2fSMatthew G. Knepley     break;
235ce78fa2fSMatthew G. Knepley   case 3:
2369566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject) dm, "tetrahedral"));
2379566063dSJacob Faibussowitsch     else         PetscCall(PetscObjectSetName((PetscObject) dm, "hexahedral"));
238ce78fa2fSMatthew G. Knepley     break;
239ce78fa2fSMatthew G. Knepley   default:
24098921bdaSJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %D", dim);
241ce78fa2fSMatthew G. Knepley   }
2421df5d5c5SMatthew G. Knepley   if (rank) {
2431df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
2449566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL));
2451df5d5c5SMatthew G. Knepley   } else {
2461df5d5c5SMatthew G. Knepley     switch (dim) {
2471df5d5c5SMatthew G. Knepley     case 2:
2481df5d5c5SMatthew G. Knepley       if (simplex) {
2491df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
2501df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
2511df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4,  5, 4, 3};
2521df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0,  0, 0, 0};
2531df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
2541df5d5c5SMatthew G. Knepley 
2559566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2561df5d5c5SMatthew G. Knepley       } else {
2571df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
2581df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
2591df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5,  3, 6, 7, 4};
2601df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
2611df5d5c5SMatthew 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};
2621df5d5c5SMatthew G. Knepley 
2639566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2641df5d5c5SMatthew G. Knepley       }
2651df5d5c5SMatthew G. Knepley       break;
2661df5d5c5SMatthew G. Knepley     case 3:
2671df5d5c5SMatthew G. Knepley       if (simplex) {
2681df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
2691df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
2701df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2,  5, 3, 4, 6};
2711df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
2721df5d5c5SMatthew 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};
2731df5d5c5SMatthew G. Knepley 
2749566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2751df5d5c5SMatthew G. Knepley       } else {
2761df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
2771df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
2781df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9,  5, 4, 10, 11, 7, 12, 13, 8};
2791df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0,  0, 0,  0,  0, 0,  0,  0, 0};
2801df5d5c5SMatthew 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,
2811df5d5c5SMatthew 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,
2821df5d5c5SMatthew 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};
2831df5d5c5SMatthew G. Knepley 
2849566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2851df5d5c5SMatthew G. Knepley       }
2861df5d5c5SMatthew G. Knepley       break;
2871df5d5c5SMatthew G. Knepley     default:
28898921bdaSJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %D", dim);
2891df5d5c5SMatthew G. Knepley     }
2901df5d5c5SMatthew G. Knepley   }
2911df5d5c5SMatthew G. Knepley   *newdm = dm;
2921df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
2931df5d5c5SMatthew G. Knepley     DM rdm;
2941df5d5c5SMatthew G. Knepley     const char *name;
2951df5d5c5SMatthew G. Knepley 
2969566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*newdm, PETSC_FALSE));
2979566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(*newdm, refinementLimit));
2989566063dSJacob Faibussowitsch     PetscCall(DMRefine(*newdm, comm, &rdm));
2999566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) *newdm, &name));
3009566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)    rdm,  name));
3019566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3021df5d5c5SMatthew G. Knepley     *newdm = rdm;
3031df5d5c5SMatthew G. Knepley   }
3041df5d5c5SMatthew G. Knepley   if (interpolate) {
3055fd9971aSMatthew G. Knepley     DM idm;
3061df5d5c5SMatthew G. Knepley 
3079566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*newdm, &idm));
3089566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3091df5d5c5SMatthew G. Knepley     *newdm = idm;
3101df5d5c5SMatthew G. Knepley   }
3111df5d5c5SMatthew G. Knepley   PetscFunctionReturn(0);
3121df5d5c5SMatthew G. Knepley }
3131df5d5c5SMatthew G. Knepley 
3149318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
3159318fe57SMatthew G. Knepley {
3169318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
3179318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
3189318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
3199318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
3209318fe57SMatthew G. Knepley   Vec            coordinates;
3219318fe57SMatthew G. Knepley   PetscSection   coordSection;
3229318fe57SMatthew G. Knepley   PetscScalar   *coords;
3239318fe57SMatthew G. Knepley   PetscInt       coordSize;
3249318fe57SMatthew G. Knepley   PetscMPIInt    rank;
3259318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
326552f7358SJed Brown 
3279318fe57SMatthew G. Knepley   PetscFunctionBegin;
3289566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
3299318fe57SMatthew G. Knepley   if (markerSeparate) {
3309318fe57SMatthew G. Knepley     markerRight  = 2;
3319318fe57SMatthew G. Knepley     markerLeft   = 1;
3329318fe57SMatthew G. Knepley   }
3339566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
3349318fe57SMatthew G. Knepley   if (!rank) {
3359566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numVertices));
3369566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
3379566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 0, markerLeft));
3389566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 1, markerRight));
3399318fe57SMatthew G. Knepley   }
3409566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
3419566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
3429318fe57SMatthew G. Knepley   /* Build coordinates */
3439566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
3449566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
3459566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
3469566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, 0, numVertices));
3479566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
3489318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
3499566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
3509566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
3519318fe57SMatthew G. Knepley   }
3529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
3539566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
3549566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
3559566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
3569566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
3579566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
3589566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
3599566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
3609318fe57SMatthew G. Knepley   coords[0] = lower[0];
3619318fe57SMatthew G. Knepley   coords[1] = upper[0];
3629566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
3639566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
3649566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
3659318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
3669318fe57SMatthew G. Knepley }
36726492d91SMatthew G. Knepley 
3689318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
369552f7358SJed Brown {
3701df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0]+1)*(edges[1]+1);
3711df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0]*(edges[1]+1) + (edges[0]+1)*edges[1];
372552f7358SJed Brown   PetscInt       markerTop      = 1;
373552f7358SJed Brown   PetscInt       markerBottom   = 1;
374552f7358SJed Brown   PetscInt       markerRight    = 1;
375552f7358SJed Brown   PetscInt       markerLeft     = 1;
376552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
377552f7358SJed Brown   Vec            coordinates;
378552f7358SJed Brown   PetscSection   coordSection;
379552f7358SJed Brown   PetscScalar    *coords;
380552f7358SJed Brown   PetscInt       coordSize;
381552f7358SJed Brown   PetscMPIInt    rank;
382552f7358SJed Brown   PetscInt       v, vx, vy;
383552f7358SJed Brown 
384552f7358SJed Brown   PetscFunctionBegin;
3859566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
386552f7358SJed Brown   if (markerSeparate) {
3871df21d24SMatthew G. Knepley     markerTop    = 3;
3881df21d24SMatthew G. Knepley     markerBottom = 1;
3891df21d24SMatthew G. Knepley     markerRight  = 2;
3901df21d24SMatthew G. Knepley     markerLeft   = 4;
391552f7358SJed Brown   }
3929566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
393dd400576SPatrick Sanan   if (rank == 0) {
394552f7358SJed Brown     PetscInt e, ex, ey;
395552f7358SJed Brown 
3969566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numEdges+numVertices));
397552f7358SJed Brown     for (e = 0; e < numEdges; ++e) {
3989566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, e, 2));
399552f7358SJed Brown     }
4009566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
401552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
402552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
403552f7358SJed Brown         PetscInt edge   = vx*edges[1] + ey + edges[0]*(edges[1]+1);
404552f7358SJed Brown         PetscInt vertex = ey*(edges[0]+1) + vx + numEdges;
405da80777bSKarl Rupp         PetscInt cone[2];
406552f7358SJed Brown 
407da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+edges[0]+1;
4089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
409552f7358SJed Brown         if (vx == edges[0]) {
4109566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge,    markerRight));
4119566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
412552f7358SJed Brown           if (ey == edges[1]-1) {
4139566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
4149566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerRight));
415552f7358SJed Brown           }
416552f7358SJed Brown         } else if (vx == 0) {
4179566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge,    markerLeft));
4189566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
419552f7358SJed Brown           if (ey == edges[1]-1) {
4209566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
4219566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft));
422552f7358SJed Brown           }
423552f7358SJed Brown         }
424552f7358SJed Brown       }
425552f7358SJed Brown     }
426552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
427552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
428552f7358SJed Brown         PetscInt edge   = vy*edges[0]     + ex;
429552f7358SJed Brown         PetscInt vertex = vy*(edges[0]+1) + ex + numEdges;
430da80777bSKarl Rupp         PetscInt cone[2];
431552f7358SJed Brown 
432da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+1;
4339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
434552f7358SJed Brown         if (vy == edges[1]) {
4359566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge,    markerTop));
4369566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
437552f7358SJed Brown           if (ex == edges[0]-1) {
4389566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
4399566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerTop));
440552f7358SJed Brown           }
441552f7358SJed Brown         } else if (vy == 0) {
4429566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge,    markerBottom));
4439566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
444552f7358SJed Brown           if (ex == edges[0]-1) {
4459566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
4469566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom));
447552f7358SJed Brown           }
448552f7358SJed Brown         }
449552f7358SJed Brown       }
450552f7358SJed Brown     }
451552f7358SJed Brown   }
4529566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
4539566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
454552f7358SJed Brown   /* Build coordinates */
4559566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 2));
4569566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
4579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
4589566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices));
4599566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2));
460552f7358SJed Brown   for (v = numEdges; v < numEdges+numVertices; ++v) {
4619566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 2));
4629566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2));
463552f7358SJed Brown   }
4649566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
4659566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
4669566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
4679566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
4689566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
4699566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 2));
4709566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
4719566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
472552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
473552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
474552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/edges[0])*vx;
475552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/edges[1])*vy;
476552f7358SJed Brown     }
477552f7358SJed Brown   }
4789566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
4799566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
4809566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
481552f7358SJed Brown   PetscFunctionReturn(0);
482552f7358SJed Brown }
483552f7358SJed Brown 
4849318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
485552f7358SJed Brown {
4869e8abbc3SMichael Lange   PetscInt       vertices[3], numVertices;
4877b59f5a9SMichael Lange   PetscInt       numFaces    = 2*faces[0]*faces[1] + 2*faces[1]*faces[2] + 2*faces[0]*faces[2];
488552f7358SJed Brown   Vec            coordinates;
489552f7358SJed Brown   PetscSection   coordSection;
490552f7358SJed Brown   PetscScalar    *coords;
491552f7358SJed Brown   PetscInt       coordSize;
492552f7358SJed Brown   PetscMPIInt    rank;
493552f7358SJed Brown   PetscInt       v, vx, vy, vz;
4947b59f5a9SMichael Lange   PetscInt       voffset, iface=0, cone[4];
495552f7358SJed Brown 
496552f7358SJed Brown   PetscFunctionBegin;
4972c71b3e2SJacob Faibussowitsch   PetscCheckFalse((faces[0] < 1) || (faces[1] < 1) || (faces[2] < 1),PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Must have at least 1 face per side");
4989566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
4999e8abbc3SMichael Lange   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
5009e8abbc3SMichael Lange   numVertices = vertices[0]*vertices[1]*vertices[2];
501dd400576SPatrick Sanan   if (rank == 0) {
502552f7358SJed Brown     PetscInt f;
503552f7358SJed Brown 
5049566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numFaces+numVertices));
505552f7358SJed Brown     for (f = 0; f < numFaces; ++f) {
5069566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, f, 4));
507552f7358SJed Brown     }
5089566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
5097b59f5a9SMichael Lange 
5107b59f5a9SMichael Lange     /* Side 0 (Top) */
5117b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5127b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5137b59f5a9SMichael Lange         voffset = numFaces + vertices[0]*vertices[1]*(vertices[2]-1) + vy*vertices[0] + vx;
5147b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]+1; cone[3] = voffset+vertices[0];
5159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
5169566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", iface, 1));
5179566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, 1));
5189566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+1, 1));
5199566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1));
5209566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1));
5217b59f5a9SMichael Lange         iface++;
522552f7358SJed Brown       }
523552f7358SJed Brown     }
5247b59f5a9SMichael Lange 
5257b59f5a9SMichael Lange     /* Side 1 (Bottom) */
5267b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5277b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5287b59f5a9SMichael Lange         voffset = numFaces + vy*(faces[0]+1) + vx;
5297b59f5a9SMichael Lange         cone[0] = voffset+1; cone[1] = voffset; cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]+1;
5309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
5319566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", iface, 1));
5329566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, 1));
5339566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+1, 1));
5349566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1));
5359566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1));
5367b59f5a9SMichael Lange         iface++;
537552f7358SJed Brown       }
538552f7358SJed Brown     }
5397b59f5a9SMichael Lange 
5407b59f5a9SMichael Lange     /* Side 2 (Front) */
5417b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5427b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5437b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vx;
5447b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]*vertices[1]+1; cone[3] = voffset+vertices[0]*vertices[1];
5459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
5469566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", iface, 1));
5479566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, 1));
5489566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+1, 1));
5499566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1));
5509566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1));
5517b59f5a9SMichael Lange         iface++;
552552f7358SJed Brown       }
5537b59f5a9SMichael Lange     }
5547b59f5a9SMichael Lange 
5557b59f5a9SMichael Lange     /* Side 3 (Back) */
5567b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5577b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5587b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vertices[0]*(vertices[1]-1) + vx;
5597b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
5607b59f5a9SMichael Lange         cone[2] = voffset+1; cone[3] = voffset;
5619566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
5629566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", iface, 1));
5639566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, 1));
5649566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+1, 1));
5659566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1));
5669566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1));
5677b59f5a9SMichael Lange         iface++;
5687b59f5a9SMichael Lange       }
5697b59f5a9SMichael Lange     }
5707b59f5a9SMichael Lange 
5717b59f5a9SMichael Lange     /* Side 4 (Left) */
5727b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5737b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
5747b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
5757b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
5767b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
5779566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
5789566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", iface, 1));
5799566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, 1));
5809566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1));
5819566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[1]+0, 1));
5829566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1));
5837b59f5a9SMichael Lange         iface++;
5847b59f5a9SMichael Lange       }
5857b59f5a9SMichael Lange     }
5867b59f5a9SMichael Lange 
5877b59f5a9SMichael Lange     /* Side 5 (Right) */
5887b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5897b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
590aab5bcd8SJed Brown         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0] + faces[0];
5917b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset;
5927b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]*vertices[1]+vertices[0];
5939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
5949566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", iface, 1));
5959566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, 1));
5969566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1));
5979566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1));
5989566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1));
5997b59f5a9SMichael Lange         iface++;
6007b59f5a9SMichael Lange       }
601552f7358SJed Brown     }
602552f7358SJed Brown   }
6039566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
6049566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
605552f7358SJed Brown   /* Build coordinates */
6069566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 3));
6079566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6089566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
6099566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices));
6109566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 3));
611552f7358SJed Brown   for (v = numFaces; v < numFaces+numVertices; ++v) {
6129566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 3));
6139566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 3));
614552f7358SJed Brown   }
6159566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
6169566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
6179566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
6189566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
6199566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
6209566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 3));
6219566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
6229566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
623552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
624552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
625552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
626552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
627552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
628552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
629552f7358SJed Brown       }
630552f7358SJed Brown     }
631552f7358SJed Brown   }
6329566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6339566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
6349566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
635552f7358SJed Brown   PetscFunctionReturn(0);
636552f7358SJed Brown }
637552f7358SJed Brown 
6389318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
6399318fe57SMatthew G. Knepley {
6409318fe57SMatthew G. Knepley   PetscFunctionBegin;
6419318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6429566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim-1));
6439566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim));
6449318fe57SMatthew G. Knepley   switch (dim) {
6459566063dSJacob Faibussowitsch     case 1: PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces));break;
6469566063dSJacob Faibussowitsch     case 2: PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces));break;
6479566063dSJacob Faibussowitsch     case 3: PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces));break;
64898921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Dimension not supported: %D", dim);
6499318fe57SMatthew G. Knepley   }
6509566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
6519318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
6529318fe57SMatthew G. Knepley }
6539318fe57SMatthew G. Knepley 
6549318fe57SMatthew G. Knepley /*@C
6559318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
6569318fe57SMatthew G. Knepley 
6579318fe57SMatthew G. Knepley   Collective
6589318fe57SMatthew G. Knepley 
6599318fe57SMatthew G. Knepley   Input Parameters:
6609318fe57SMatthew G. Knepley + comm        - The communicator for the DM object
6619318fe57SMatthew G. Knepley . dim         - The spatial dimension of the box, so the resulting mesh is has dimension dim-1
6629318fe57SMatthew 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
6639318fe57SMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
6649318fe57SMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
6659318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
6669318fe57SMatthew G. Knepley 
6679318fe57SMatthew G. Knepley   Output Parameter:
6689318fe57SMatthew G. Knepley . dm  - The DM object
6699318fe57SMatthew G. Knepley 
6709318fe57SMatthew G. Knepley   Level: beginner
6719318fe57SMatthew G. Knepley 
6729318fe57SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreateBoxMesh(), DMPlexCreateFromFile(), DMSetType(), DMCreate()
6739318fe57SMatthew G. Knepley @*/
6749318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
6759318fe57SMatthew G. Knepley {
6769318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
6779318fe57SMatthew G. Knepley   PetscReal      low[3] = {0, 0, 0};
6789318fe57SMatthew G. Knepley   PetscReal      upp[3] = {1, 1, 1};
6799318fe57SMatthew G. Knepley 
6809318fe57SMatthew G. Knepley   PetscFunctionBegin;
6819566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm,dm));
6829566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm,DMPLEX));
6839566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate));
6849318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
6859318fe57SMatthew G. Knepley }
6869318fe57SMatthew G. Knepley 
6879318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm,PetscInt segments,PetscReal lower,PetscReal upper,DMBoundaryType bd)
688fdbf62faSLisandro Dalcin {
689fdbf62faSLisandro Dalcin   PetscInt       i,fStart,fEnd,numCells = 0,numVerts = 0;
690fdbf62faSLisandro Dalcin   PetscInt       numPoints[2],*coneSize,*cones,*coneOrientations;
691fdbf62faSLisandro Dalcin   PetscScalar    *vertexCoords;
692fdbf62faSLisandro Dalcin   PetscReal      L,maxCell;
693fdbf62faSLisandro Dalcin   PetscBool      markerSeparate = PETSC_FALSE;
694fdbf62faSLisandro Dalcin   PetscInt       markerLeft  = 1, faceMarkerLeft  = 1;
695fdbf62faSLisandro Dalcin   PetscInt       markerRight = 1, faceMarkerRight = 2;
696fdbf62faSLisandro Dalcin   PetscBool      wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
697fdbf62faSLisandro Dalcin   PetscMPIInt    rank;
698fdbf62faSLisandro Dalcin 
699fdbf62faSLisandro Dalcin   PetscFunctionBegin;
7009318fe57SMatthew G. Knepley   PetscValidPointer(dm,1);
701fdbf62faSLisandro Dalcin 
7029566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm,1));
7039566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm,"marker"));
7049566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm,"Face Sets"));
705fdbf62faSLisandro Dalcin 
7069566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm),&rank));
707dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
708dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
709fdbf62faSLisandro Dalcin 
710fdbf62faSLisandro Dalcin   numPoints[0] = numVerts ; numPoints[1] = numCells;
7119566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(numCells+numVerts,&coneSize,numCells*2,&cones,numCells+numVerts,&coneOrientations,numVerts,&vertexCoords));
7129566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(coneOrientations,numCells+numVerts));
713fdbf62faSLisandro Dalcin   for (i = 0; i < numCells; ++i) { coneSize[i] = 2; }
714fdbf62faSLisandro Dalcin   for (i = 0; i < numVerts; ++i) { coneSize[numCells+i] = 0; }
715fdbf62faSLisandro Dalcin   for (i = 0; i < numCells; ++i) { cones[2*i] = numCells + i%numVerts; cones[2*i+1] = numCells + (i+1)%numVerts; }
716fdbf62faSLisandro Dalcin   for (i = 0; i < numVerts; ++i) { vertexCoords[i] = lower + (upper-lower)*((PetscReal)i/(PetscReal)numCells); }
7179566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateFromDAG(dm,1,numPoints,coneSize,cones,coneOrientations,vertexCoords));
7189566063dSJacob Faibussowitsch   PetscCall(PetscFree4(coneSize,cones,coneOrientations,vertexCoords));
719fdbf62faSLisandro Dalcin 
7209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options,((PetscObject)dm)->prefix,"-dm_plex_separate_marker",&markerSeparate,NULL));
721fdbf62faSLisandro Dalcin   if (markerSeparate) { markerLeft = faceMarkerLeft; markerRight = faceMarkerRight;}
722dd400576SPatrick Sanan   if (!wrap && rank == 0) {
7239566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm,1,&fStart,&fEnd));
7249566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm,"marker",fStart,markerLeft));
7259566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm,"marker",fEnd-1,markerRight));
7269566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm,"Face Sets",fStart,faceMarkerLeft));
7279566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm,"Face Sets",fEnd-1,faceMarkerRight));
728fdbf62faSLisandro Dalcin   }
729fdbf62faSLisandro Dalcin   if (wrap) {
730fdbf62faSLisandro Dalcin     L       = upper - lower;
731fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1*(L/(PetscReal)PetscMax(1,segments));
7329566063dSJacob Faibussowitsch     PetscCall(DMSetPeriodicity(dm,PETSC_TRUE,&maxCell,&L,&bd));
733fdbf62faSLisandro Dalcin   }
7349566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
735fdbf62faSLisandro Dalcin   PetscFunctionReturn(0);
736fdbf62faSLisandro Dalcin }
737fdbf62faSLisandro Dalcin 
7389318fe57SMatthew 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)
739d6218766SMatthew G. Knepley {
7409318fe57SMatthew G. Knepley   DM             boundary, vol;
741768d5fceSMatthew G. Knepley   PetscInt       i;
742d6218766SMatthew G. Knepley 
743d6218766SMatthew G. Knepley   PetscFunctionBegin;
7449318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
7452c71b3e2SJacob Faibussowitsch   for (i = 0; i < dim; ++i) PetscCheckFalse(periodicity[i] != DM_BOUNDARY_NONE,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Periodicity is not supported for simplex meshes");
7469566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), &boundary));
7479566063dSJacob Faibussowitsch   PetscCall(DMSetType(boundary, DMPLEX));
7489566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
7499566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
7509566063dSJacob Faibussowitsch   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, vol));
7519566063dSJacob Faibussowitsch   PetscCall(DMPlexReplace_Static(dm, &vol));
7529566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&boundary));
753d6218766SMatthew G. Knepley   PetscFunctionReturn(0);
754d6218766SMatthew G. Knepley }
755d6218766SMatthew G. Knepley 
7563dfda0b1SToby Isaac static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
7573dfda0b1SToby Isaac {
758ed0e4b50SMatthew G. Knepley   DMLabel        cutLabel = NULL;
759f4eb4c5dSMatthew G. Knepley   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
760f4eb4c5dSMatthew G. Knepley   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
761f4eb4c5dSMatthew G. Knepley   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
762f4eb4c5dSMatthew G. Knepley   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
763f4eb4c5dSMatthew G. Knepley   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
764f4eb4c5dSMatthew G. Knepley   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
7653dfda0b1SToby Isaac   PetscInt       dim;
766d8211ee3SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
7673dfda0b1SToby Isaac   PetscMPIInt    rank;
7683dfda0b1SToby Isaac 
7693dfda0b1SToby Isaac   PetscFunctionBegin;
7709566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm,&dim));
7719566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
7729566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm,"marker"));
7739566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm,"Face Sets"));
7749566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
775d8211ee3SMatthew G. Knepley   if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ||
776d8211ee3SMatthew G. Knepley       bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ||
777d8211ee3SMatthew G. Knepley       bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) {
7784c67ea77SStefano Zampini 
7799566063dSJacob Faibussowitsch     if (cutMarker) {PetscCall(DMCreateLabel(dm, "periodic_cut")); PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));}
780d8211ee3SMatthew G. Knepley   }
7813dfda0b1SToby Isaac   switch (dim) {
7823dfda0b1SToby Isaac   case 2:
783f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
784f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
785f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
786f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
7873dfda0b1SToby Isaac     break;
7883dfda0b1SToby Isaac   case 3:
789f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
790f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
791f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
792f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
793f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
794f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
7953dfda0b1SToby Isaac     break;
7963dfda0b1SToby Isaac   default:
79798921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %D not supported",dim);
7983dfda0b1SToby Isaac   }
7999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
800f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
801f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
802f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
803f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
804f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
805f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
806f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
8073dfda0b1SToby Isaac   }
8083dfda0b1SToby Isaac   {
809dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
810dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
811dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
812dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
813dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
814dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
8153dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
8163dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges*numZEdges;
8173dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges*numZEdges;
8183dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges*numYEdges;
8193dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices*numXFaces;
8203dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices*numYFaces;
8213dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices*numZFaces;
8223dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
8233dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
8243dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
8253dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
8263dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
8273dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
8283dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
8293dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
8303dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
8313dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
8323dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
8333dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
8343dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
8353dfda0b1SToby Isaac     Vec            coordinates;
8363dfda0b1SToby Isaac     PetscSection   coordSection;
8373dfda0b1SToby Isaac     PetscScalar   *coords;
8383dfda0b1SToby Isaac     PetscInt       coordSize;
8393dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
8403dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
8413dfda0b1SToby Isaac 
8429566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices));
8433dfda0b1SToby Isaac     for (c = 0; c < numCells; c++) {
8449566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, c, 6));
8453dfda0b1SToby Isaac     }
8463dfda0b1SToby Isaac     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
8479566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, f, 4));
8483dfda0b1SToby Isaac     }
8493dfda0b1SToby Isaac     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
8509566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, e, 2));
8513dfda0b1SToby Isaac     }
8529566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
8533dfda0b1SToby Isaac     /* Build cells */
8543dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
8553dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
8563dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
8573dfda0b1SToby Isaac           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
8583dfda0b1SToby Isaac           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
8593dfda0b1SToby Isaac           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
8603dfda0b1SToby Isaac           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
8613dfda0b1SToby Isaac           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
8623dfda0b1SToby Isaac           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
8633dfda0b1SToby Isaac           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
8643dfda0b1SToby Isaac                             /* B,  T,  F,  K,  R,  L */
865b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2,  0,  0, -3,  0, -2}; /* ??? */
86642206facSLisandro Dalcin           PetscInt cone[6];
8673dfda0b1SToby Isaac 
8683dfda0b1SToby Isaac           /* no boundary twisting in 3D */
8693dfda0b1SToby Isaac           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
8709566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
8719566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
8729566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
8739566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
8749566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
8753dfda0b1SToby Isaac         }
8763dfda0b1SToby Isaac       }
8773dfda0b1SToby Isaac     }
8783dfda0b1SToby Isaac     /* Build x faces */
8793dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
8803dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
8813dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
8823dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz*numYEdges+fy)     *numXVertices+fx;
8833dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy                   *numXVertices+fx)*numZEdges + fz;
8843dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
8853dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz                   *numXVertices+fx)*numYEdges + fy;
8863dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
887b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
8883dfda0b1SToby Isaac           PetscInt cone[4];
8893dfda0b1SToby Isaac 
8903dfda0b1SToby Isaac           if (dim == 3) {
8913dfda0b1SToby Isaac             /* markers */
8923dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
8933dfda0b1SToby Isaac               if (fx == numXVertices-1) {
8949566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
8959566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
8963dfda0b1SToby Isaac               }
8973dfda0b1SToby Isaac               else if (fx == 0) {
8989566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
8999566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
9003dfda0b1SToby Isaac               }
9013dfda0b1SToby Isaac             }
9023dfda0b1SToby Isaac           }
9033dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9049566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
9059566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
9063dfda0b1SToby Isaac         }
9073dfda0b1SToby Isaac       }
9083dfda0b1SToby Isaac     }
9093dfda0b1SToby Isaac     /* Build y faces */
9103dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
91142206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
9123dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
9133dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
9143dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx)*numZEdges + fz;
9153dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
9163dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz                   *numYVertices+fy)*numXEdges + fx;
9173dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
918b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9193dfda0b1SToby Isaac           PetscInt cone[4];
9203dfda0b1SToby Isaac 
9213dfda0b1SToby Isaac           if (dim == 3) {
9223dfda0b1SToby Isaac             /* markers */
9233dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
9243dfda0b1SToby Isaac               if (fy == numYVertices-1) {
9259566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
9269566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
9273dfda0b1SToby Isaac               }
9283dfda0b1SToby Isaac               else if (fy == 0) {
9299566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
9309566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
9313dfda0b1SToby Isaac               }
9323dfda0b1SToby Isaac             }
9333dfda0b1SToby Isaac           }
9343dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9359566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
9369566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
9373dfda0b1SToby Isaac         }
9383dfda0b1SToby Isaac       }
9393dfda0b1SToby Isaac     }
9403dfda0b1SToby Isaac     /* Build z faces */
9413dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
9423dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
9433dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
9443dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
9453dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx)*numYEdges + fy;
9463dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
9473dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy)*numXEdges + fx;
9483dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
949b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9503dfda0b1SToby Isaac           PetscInt cone[4];
9513dfda0b1SToby Isaac 
9523dfda0b1SToby Isaac           if (dim == 2) {
953b5a892a1SMatthew G. Knepley             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -1;}
9543dfda0b1SToby Isaac             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
9559566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
9569566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
957d1c88043SMatthew G. Knepley           } else {
9583dfda0b1SToby Isaac             /* markers */
9593dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
9603dfda0b1SToby Isaac               if (fz == numZVertices-1) {
9619566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
9629566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
9633dfda0b1SToby Isaac               }
9643dfda0b1SToby Isaac               else if (fz == 0) {
9659566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
9669566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
9673dfda0b1SToby Isaac               }
9683dfda0b1SToby Isaac             }
9693dfda0b1SToby Isaac           }
9703dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9719566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
9729566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
9733dfda0b1SToby Isaac         }
9743dfda0b1SToby Isaac       }
9753dfda0b1SToby Isaac     }
9763dfda0b1SToby Isaac     /* Build Z edges*/
9773dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
9783dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
9793dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
9803dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
9813dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez                   *numYVertices+vy)*numXVertices + vx;
9823dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
9833dfda0b1SToby Isaac           PetscInt       cone[2];
9843dfda0b1SToby Isaac 
9853dfda0b1SToby Isaac           if (dim == 3) {
9863dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
9873dfda0b1SToby Isaac               if (vx == numXVertices-1) {
9889566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
9893dfda0b1SToby Isaac               }
9903dfda0b1SToby Isaac               else if (vx == 0) {
9919566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
9923dfda0b1SToby Isaac               }
9933dfda0b1SToby Isaac             }
9943dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
9953dfda0b1SToby Isaac               if (vy == numYVertices-1) {
9969566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
9973dfda0b1SToby Isaac               }
9983dfda0b1SToby Isaac               else if (vy == 0) {
9999566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
10003dfda0b1SToby Isaac               }
10013dfda0b1SToby Isaac             }
10023dfda0b1SToby Isaac           }
10033dfda0b1SToby Isaac           cone[0] = vertexB; cone[1] = vertexT;
10049566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
10053dfda0b1SToby Isaac         }
10063dfda0b1SToby Isaac       }
10073dfda0b1SToby Isaac     }
10083dfda0b1SToby Isaac     /* Build Y edges*/
10093dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
10103dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
10113dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
10123dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
10133dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
10143dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
10153dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
10163dfda0b1SToby Isaac           PetscInt       cone[2];
10173dfda0b1SToby Isaac 
10183dfda0b1SToby Isaac           cone[0] = vertexF; cone[1] = vertexK;
10199566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
10203dfda0b1SToby Isaac           if (dim == 2) {
10213dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
10223dfda0b1SToby Isaac               if (vx == numXVertices-1) {
10239566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
10249566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge,    markerRight));
10259566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
10263dfda0b1SToby Isaac                 if (ey == numYEdges-1) {
10279566063dSJacob Faibussowitsch                   PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
10283dfda0b1SToby Isaac                 }
1029d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
10309566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
10319566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge,    markerLeft));
10329566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
10333dfda0b1SToby Isaac                 if (ey == numYEdges-1) {
10349566063dSJacob Faibussowitsch                   PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
10353dfda0b1SToby Isaac                 }
10363dfda0b1SToby Isaac               }
1037d8211ee3SMatthew G. Knepley             } else {
10384c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
10399566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge,    1));
10409566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1041d8211ee3SMatthew G. Knepley                 if (ey == numYEdges-1) {
10429566063dSJacob Faibussowitsch                   PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
10433dfda0b1SToby Isaac                 }
10443dfda0b1SToby Isaac               }
1045d8211ee3SMatthew G. Knepley             }
1046d8211ee3SMatthew G. Knepley           } else {
10473dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
10483dfda0b1SToby Isaac               if (vx == numXVertices-1) {
10499566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1050d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
10519566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
10523dfda0b1SToby Isaac               }
10533dfda0b1SToby Isaac             }
10543dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
10553dfda0b1SToby Isaac               if (vz == numZVertices-1) {
10569566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1057d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
10589566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
10593dfda0b1SToby Isaac               }
10603dfda0b1SToby Isaac             }
10613dfda0b1SToby Isaac           }
10623dfda0b1SToby Isaac         }
10633dfda0b1SToby Isaac       }
10643dfda0b1SToby Isaac     }
10653dfda0b1SToby Isaac     /* Build X edges*/
10663dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
10673dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
10683dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
10693dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
10703dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
10713dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
10723dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
10733dfda0b1SToby Isaac           PetscInt       cone[2];
10743dfda0b1SToby Isaac 
10753dfda0b1SToby Isaac           cone[0] = vertexL; cone[1] = vertexR;
10769566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
10773dfda0b1SToby Isaac           if (dim == 2) {
10783dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
10793dfda0b1SToby Isaac               if (vy == numYVertices-1) {
10809566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
10819566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge,    markerTop));
10829566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
10833dfda0b1SToby Isaac                 if (ex == numXEdges-1) {
10849566063dSJacob Faibussowitsch                   PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
10853dfda0b1SToby Isaac                 }
1086d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
10879566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
10889566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge,    markerBottom));
10899566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
10903dfda0b1SToby Isaac                 if (ex == numXEdges-1) {
10919566063dSJacob Faibussowitsch                   PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
10923dfda0b1SToby Isaac                 }
10933dfda0b1SToby Isaac               }
1094d8211ee3SMatthew G. Knepley             } else {
10954c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
10969566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge,    1));
10979566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1098d8211ee3SMatthew G. Knepley                 if (ex == numXEdges-1) {
10999566063dSJacob Faibussowitsch                   PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
11003dfda0b1SToby Isaac                 }
11013dfda0b1SToby Isaac               }
1102d8211ee3SMatthew G. Knepley             }
1103d8211ee3SMatthew G. Knepley           } else {
11043dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
11053dfda0b1SToby Isaac               if (vy == numYVertices-1) {
11069566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
11073dfda0b1SToby Isaac               }
11083dfda0b1SToby Isaac               else if (vy == 0) {
11099566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
11103dfda0b1SToby Isaac               }
11113dfda0b1SToby Isaac             }
11123dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
11133dfda0b1SToby Isaac               if (vz == numZVertices-1) {
11149566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
11153dfda0b1SToby Isaac               }
11163dfda0b1SToby Isaac               else if (vz == 0) {
11179566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
11183dfda0b1SToby Isaac               }
11193dfda0b1SToby Isaac             }
11203dfda0b1SToby Isaac           }
11213dfda0b1SToby Isaac         }
11223dfda0b1SToby Isaac       }
11233dfda0b1SToby Isaac     }
11249566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
11259566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
11263dfda0b1SToby Isaac     /* Build coordinates */
11279566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
11289566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
11299566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
11309566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices));
11313dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
11329566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
11339566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
11343dfda0b1SToby Isaac     }
11359566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
11369566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
11379566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
11389566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
11399566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
11409566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
11419566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates,VECSTANDARD));
11429566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
11433dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
11443dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
11453dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
11463dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
11473dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
11483dfda0b1SToby Isaac           if (dim == 3) {
11493dfda0b1SToby Isaac             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
11503dfda0b1SToby Isaac           }
11513dfda0b1SToby Isaac         }
11523dfda0b1SToby Isaac       }
11533dfda0b1SToby Isaac     }
11549566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
11559566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
11569566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
11573dfda0b1SToby Isaac   }
11583dfda0b1SToby Isaac   PetscFunctionReturn(0);
11593dfda0b1SToby Isaac }
11603dfda0b1SToby Isaac 
11619318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1162a6dfd86eSKarl Rupp {
11639318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
11649318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1165552f7358SJed Brown 
1166552f7358SJed Brown   PetscFunctionBegin;
11679318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
11689318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
11699566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
11709318fe57SMatthew G. Knepley   for (d = 0; d < dim; ++d) {fac[d] = faces[d]; bdt[d] = periodicity[d];}
11719566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
1172768d5fceSMatthew G. Knepley   if (periodicity[0] == DM_BOUNDARY_PERIODIC || periodicity[0] == DM_BOUNDARY_TWIST ||
1173768d5fceSMatthew G. Knepley       periodicity[1] == DM_BOUNDARY_PERIODIC || periodicity[1] == DM_BOUNDARY_TWIST ||
1174768d5fceSMatthew G. Knepley       (dim > 2 && (periodicity[2] == DM_BOUNDARY_PERIODIC || periodicity[2] == DM_BOUNDARY_TWIST))) {
1175768d5fceSMatthew G. Knepley     PetscReal L[3];
1176768d5fceSMatthew G. Knepley     PetscReal maxCell[3];
1177552f7358SJed Brown 
11789318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
11799318fe57SMatthew G. Knepley       L[d]       = upper[d] - lower[d];
11809318fe57SMatthew G. Knepley       maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1181768d5fceSMatthew G. Knepley     }
11829566063dSJacob Faibussowitsch     PetscCall(DMSetPeriodicity(dm, PETSC_TRUE, maxCell, L, periodicity));
1183768d5fceSMatthew G. Knepley   }
11849566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
11859318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
11869318fe57SMatthew G. Knepley }
11879318fe57SMatthew G. Knepley 
11889318fe57SMatthew 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)
11899318fe57SMatthew G. Knepley {
11909318fe57SMatthew G. Knepley   PetscFunctionBegin;
11919566063dSJacob Faibussowitsch   if (dim == 1)      PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
11929566063dSJacob Faibussowitsch   else if (simplex)  PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
11939566063dSJacob Faibussowitsch   else               PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
11949318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1195768d5fceSMatthew G. Knepley     DM udm;
1196768d5fceSMatthew G. Knepley 
11979566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
11989566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
11999566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &udm));
1200768d5fceSMatthew G. Knepley   }
1201768d5fceSMatthew G. Knepley   PetscFunctionReturn(0);
1202c8c68bd8SToby Isaac }
1203c8c68bd8SToby Isaac 
1204768d5fceSMatthew G. Knepley /*@C
1205768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1206768d5fceSMatthew G. Knepley 
1207d083f849SBarry Smith   Collective
1208768d5fceSMatthew G. Knepley 
1209768d5fceSMatthew G. Knepley   Input Parameters:
1210768d5fceSMatthew G. Knepley + comm        - The communicator for the DM object
1211768d5fceSMatthew G. Knepley . dim         - The spatial dimension
1212768d5fceSMatthew G. Knepley . simplex     - PETSC_TRUE for simplices, PETSC_FALSE for tensor cells
1213fdbf62faSLisandro Dalcin . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
1214768d5fceSMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
1215768d5fceSMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
1216fdbf62faSLisandro Dalcin . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1217768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1218768d5fceSMatthew G. Knepley 
1219768d5fceSMatthew G. Knepley   Output Parameter:
1220768d5fceSMatthew G. Knepley . dm  - The DM object
1221768d5fceSMatthew G. Knepley 
12229318fe57SMatthew G. Knepley   Note: If you want to customize this mesh using options, you just need to
12239318fe57SMatthew G. Knepley $  DMCreate(comm, &dm);
12249318fe57SMatthew G. Knepley $  DMSetType(dm, DMPLEX);
12259318fe57SMatthew G. Knepley $  DMSetFromOptions(dm);
12269318fe57SMatthew G. Knepley and use the options on the DMSetFromOptions() page.
12271367e252SJed Brown 
12281367e252SJed Brown   Here is the numbering returned for 2 faces in each direction for tensor cells:
1229768d5fceSMatthew G. Knepley $ 10---17---11---18----12
1230768d5fceSMatthew G. Knepley $  |         |         |
1231768d5fceSMatthew G. Knepley $  |         |         |
1232768d5fceSMatthew G. Knepley $ 20    2   22    3    24
1233768d5fceSMatthew G. Knepley $  |         |         |
1234768d5fceSMatthew G. Knepley $  |         |         |
1235768d5fceSMatthew G. Knepley $  7---15----8---16----9
1236768d5fceSMatthew G. Knepley $  |         |         |
1237768d5fceSMatthew G. Knepley $  |         |         |
1238768d5fceSMatthew G. Knepley $ 19    0   21    1   23
1239768d5fceSMatthew G. Knepley $  |         |         |
1240768d5fceSMatthew G. Knepley $  |         |         |
1241768d5fceSMatthew G. Knepley $  4---13----5---14----6
1242768d5fceSMatthew G. Knepley 
1243768d5fceSMatthew G. Knepley and for simplicial cells
1244768d5fceSMatthew G. Knepley 
1245768d5fceSMatthew G. Knepley $ 14----8---15----9----16
1246768d5fceSMatthew G. Knepley $  |\     5  |\      7 |
1247768d5fceSMatthew G. Knepley $  | \       | \       |
1248768d5fceSMatthew G. Knepley $ 13   2    14    3    15
1249768d5fceSMatthew G. Knepley $  | 4   \   | 6   \   |
1250768d5fceSMatthew G. Knepley $  |       \ |       \ |
1251768d5fceSMatthew G. Knepley $ 11----6---12----7----13
1252768d5fceSMatthew G. Knepley $  |\        |\        |
1253768d5fceSMatthew G. Knepley $  | \    1  | \     3 |
1254768d5fceSMatthew G. Knepley $ 10   0    11    1    12
1255768d5fceSMatthew G. Knepley $  | 0   \   | 2   \   |
1256768d5fceSMatthew G. Knepley $  |       \ |       \ |
1257768d5fceSMatthew G. Knepley $  8----4----9----5----10
1258768d5fceSMatthew G. Knepley 
1259768d5fceSMatthew G. Knepley   Level: beginner
1260768d5fceSMatthew G. Knepley 
12619318fe57SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreateFromFile(), DMPlexCreateHexCylinderMesh(), DMSetType(), DMCreate()
1262768d5fceSMatthew G. Knepley @*/
1263768d5fceSMatthew 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)
1264552f7358SJed Brown {
12659318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1266fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1267fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1268fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1269552f7358SJed Brown 
1270768d5fceSMatthew G. Knepley   PetscFunctionBegin;
12719566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm,dm));
12729566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm,DMPLEX));
12739566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
12749318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
12759318fe57SMatthew G. Knepley }
1276fdbf62faSLisandro Dalcin 
1277d410b0cfSMatthew G. Knepley static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
12789318fe57SMatthew G. Knepley {
12799318fe57SMatthew G. Knepley   DM             bdm, vol;
12809318fe57SMatthew G. Knepley   PetscInt       i;
12819318fe57SMatthew G. Knepley 
12829318fe57SMatthew G. Knepley   PetscFunctionBegin;
12832c71b3e2SJacob Faibussowitsch   for (i = 0; i < 3; ++i) PetscCheckFalse(periodicity[i] != DM_BOUNDARY_NONE,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Periodicity not yet supported");
12849566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), &bdm));
12859566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
12869566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
12879566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
12889566063dSJacob Faibussowitsch   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, NULL, NULL, &vol));
12899566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
12909566063dSJacob Faibussowitsch   PetscCall(DMPlexReplace_Static(dm, &vol));
12919318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
12929318fe57SMatthew G. Knepley     Vec          v;
12939318fe57SMatthew G. Knepley     PetscScalar *x;
12949318fe57SMatthew G. Knepley     PetscInt     cDim, n;
12959318fe57SMatthew G. Knepley 
12969566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
12979566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
12989566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
12999566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
13009318fe57SMatthew G. Knepley     x   += cDim;
13019318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
13029566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v,&x));
13039566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
13049318fe57SMatthew G. Knepley   }
1305552f7358SJed Brown   PetscFunctionReturn(0);
1306552f7358SJed Brown }
1307552f7358SJed Brown 
130800dabe28SStefano Zampini /*@
130900dabe28SStefano Zampini   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tesselating the (x,y) plane and extruding in the third direction using wedge cells.
131000dabe28SStefano Zampini 
1311d083f849SBarry Smith   Collective
131200dabe28SStefano Zampini 
131300dabe28SStefano Zampini   Input Parameters:
131400dabe28SStefano Zampini + comm        - The communicator for the DM object
131500dabe28SStefano Zampini . faces       - Number of faces per dimension, or NULL for (1, 1, 1)
131600dabe28SStefano Zampini . lower       - The lower left corner, or NULL for (0, 0, 0)
131700dabe28SStefano Zampini . upper       - The upper right corner, or NULL for (1, 1, 1)
131800dabe28SStefano Zampini . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1319d0fcb9c2SMatthew G. Knepley . orderHeight - If PETSC_TRUE, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
132000dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
132100dabe28SStefano Zampini 
132200dabe28SStefano Zampini   Output Parameter:
132300dabe28SStefano Zampini . dm  - The DM object
132400dabe28SStefano Zampini 
132500dabe28SStefano Zampini   Level: beginner
132600dabe28SStefano Zampini 
1327d410b0cfSMatthew G. Knepley .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateWedgeCylinderMesh(), DMExtrude(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
132800dabe28SStefano Zampini @*/
1329d0fcb9c2SMatthew 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)
133000dabe28SStefano Zampini {
13319318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
133200dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
133300dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
133400dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
133500dabe28SStefano Zampini 
133600dabe28SStefano Zampini   PetscFunctionBegin;
13379566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm,dm));
13389566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm,DMPLEX));
13399566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
1340d410b0cfSMatthew G. Knepley   if (!interpolate) {
1341d410b0cfSMatthew G. Knepley     DM udm;
134200dabe28SStefano Zampini 
13439566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
13449566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(*dm, &udm));
134500dabe28SStefano Zampini   }
134600dabe28SStefano Zampini   PetscFunctionReturn(0);
134700dabe28SStefano Zampini }
134800dabe28SStefano Zampini 
1349a9074c1eSMatthew G. Knepley /*@C
1350a9074c1eSMatthew G. Knepley   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all DM options in the database.
1351a9074c1eSMatthew G. Knepley 
1352d083f849SBarry Smith   Logically Collective on dm
1353a9074c1eSMatthew G. Knepley 
1354a9074c1eSMatthew G. Knepley   Input Parameters:
1355a9074c1eSMatthew G. Knepley + dm - the DM context
1356a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1357a9074c1eSMatthew G. Knepley 
1358a9074c1eSMatthew G. Knepley   Notes:
1359a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1360a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1361a9074c1eSMatthew G. Knepley 
1362a9074c1eSMatthew G. Knepley   Level: advanced
1363a9074c1eSMatthew G. Knepley 
1364a9074c1eSMatthew G. Knepley .seealso: SNESSetFromOptions()
1365a9074c1eSMatthew G. Knepley @*/
1366a9074c1eSMatthew G. Knepley PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1367a9074c1eSMatthew G. Knepley {
1368a9074c1eSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1369a9074c1eSMatthew G. Knepley 
1370a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1371a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13729566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject) dm, prefix));
13739566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject) mesh->partitioner, prefix));
1374a9074c1eSMatthew G. Knepley   PetscFunctionReturn(0);
1375a9074c1eSMatthew G. Knepley }
1376a9074c1eSMatthew G. Knepley 
13779318fe57SMatthew G. Knepley /* Remap geometry to cylinder
137861a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
137961a622f3SMatthew G. Knepley 
13809318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
13819318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
13829318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
13830510c589SMatthew G. Knepley 
13849318fe57SMatthew G. Knepley        phi     = arctan(y/x)
13859318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
13869318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
13870510c589SMatthew G. Knepley 
13889318fe57SMatthew G. Knepley      so we remap them using
13890510c589SMatthew G. Knepley 
13909318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
13919318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
13920510c589SMatthew G. Knepley 
13939318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
13949318fe57SMatthew G. Knepley */
13959318fe57SMatthew G. Knepley static void snapToCylinder(PetscInt dim, PetscInt Nf, PetscInt NfAux,
13969318fe57SMatthew G. Knepley                            const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
13979318fe57SMatthew G. Knepley                            const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
13989318fe57SMatthew G. Knepley                            PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
13999318fe57SMatthew G. Knepley {
14009318fe57SMatthew G. Knepley   const PetscReal dis = 1.0/PetscSqrtReal(2.0);
14019318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5*dis;
140222cc497dSMatthew G. Knepley 
14039318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
14049318fe57SMatthew G. Knepley     f0[0] = u[0];
14059318fe57SMatthew G. Knepley     f0[1] = u[1];
14069318fe57SMatthew G. Knepley   } else {
14079318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
14080510c589SMatthew G. Knepley 
14099318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
14109318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
14119318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
14129318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
14139318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
14149318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI/4.0) && (PetscAbsReal(phi) < 3.0*PETSC_PI/4.0)) {
14159318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2/sinp);
14169318fe57SMatthew G. Knepley       df = PetscAbsReal(dis/sinp);
14179318fe57SMatthew G. Knepley       xc = ds2*x/PetscAbsReal(y);
14189318fe57SMatthew G. Knepley       yc = ds2*PetscSignReal(y);
14199318fe57SMatthew G. Knepley     } else {
14209318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2/cosp);
14219318fe57SMatthew G. Knepley       df = PetscAbsReal(dis/cosp);
14229318fe57SMatthew G. Knepley       xc = ds2*PetscSignReal(x);
14239318fe57SMatthew G. Knepley       yc = ds2*y/PetscAbsReal(x);
14249318fe57SMatthew G. Knepley     }
14259318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc)*(1.0 - dc)/(df - dc);
14269318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc)*(1.0 - dc)/(df - dc);
14279318fe57SMatthew G. Knepley   }
14289318fe57SMatthew G. Knepley   f0[2] = u[2];
14299318fe57SMatthew G. Knepley }
14300510c589SMatthew G. Knepley 
14319318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ)
14320510c589SMatthew G. Knepley {
14330510c589SMatthew G. Knepley   const PetscInt dim = 3;
14349318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1435d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
14360510c589SMatthew G. Knepley 
14370510c589SMatthew G. Knepley   PetscFunctionBegin;
14389566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
14399566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
14400510c589SMatthew G. Knepley   /* Create topology */
14410510c589SMatthew G. Knepley   {
14420510c589SMatthew G. Knepley     PetscInt cone[8], c;
14430510c589SMatthew G. Knepley 
1444dd400576SPatrick Sanan     numCells    = rank == 0 ?  5 : 0;
1445dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
1446006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1447ae8bcbbbSMatthew G. Knepley       numCells   *= 3;
1448dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
1449006a8963SMatthew G. Knepley     }
14509566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells+numVertices));
14519566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
14529566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
1453dd400576SPatrick Sanan     if (rank == 0) {
1454006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1455ae8bcbbbSMatthew G. Knepley         cone[0] = 15; cone[1] = 18; cone[2] = 17; cone[3] = 16;
1456ae8bcbbbSMatthew G. Knepley         cone[4] = 31; cone[5] = 32; cone[6] = 33; cone[7] = 34;
14579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
1458ae8bcbbbSMatthew G. Knepley         cone[0] = 16; cone[1] = 17; cone[2] = 24; cone[3] = 23;
1459ae8bcbbbSMatthew G. Knepley         cone[4] = 32; cone[5] = 36; cone[6] = 37; cone[7] = 33; /* 22 25 26 21 */
14609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
1461ae8bcbbbSMatthew G. Knepley         cone[0] = 18; cone[1] = 27; cone[2] = 24; cone[3] = 17;
1462ae8bcbbbSMatthew G. Knepley         cone[4] = 34; cone[5] = 33; cone[6] = 37; cone[7] = 38;
14639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
1464ae8bcbbbSMatthew G. Knepley         cone[0] = 29; cone[1] = 27; cone[2] = 18; cone[3] = 15;
1465ae8bcbbbSMatthew G. Knepley         cone[4] = 35; cone[5] = 31; cone[6] = 34; cone[7] = 38;
14669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
1467ae8bcbbbSMatthew G. Knepley         cone[0] = 29; cone[1] = 15; cone[2] = 16; cone[3] = 23;
1468ae8bcbbbSMatthew G. Knepley         cone[4] = 35; cone[5] = 36; cone[6] = 32; cone[7] = 31;
14699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1470006a8963SMatthew G. Knepley 
1471ae8bcbbbSMatthew G. Knepley         cone[0] = 31; cone[1] = 34; cone[2] = 33; cone[3] = 32;
1472ae8bcbbbSMatthew G. Knepley         cone[4] = 19; cone[5] = 22; cone[6] = 21; cone[7] = 20;
14739566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
1474ae8bcbbbSMatthew G. Knepley         cone[0] = 32; cone[1] = 33; cone[2] = 37; cone[3] = 36;
1475ae8bcbbbSMatthew G. Knepley         cone[4] = 22; cone[5] = 25; cone[6] = 26; cone[7] = 21;
14769566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
1477ae8bcbbbSMatthew G. Knepley         cone[0] = 34; cone[1] = 38; cone[2] = 37; cone[3] = 33;
1478ae8bcbbbSMatthew G. Knepley         cone[4] = 20; cone[5] = 21; cone[6] = 26; cone[7] = 28;
14799566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
1480ae8bcbbbSMatthew G. Knepley         cone[0] = 35; cone[1] = 38; cone[2] = 34; cone[3] = 31;
1481ae8bcbbbSMatthew G. Knepley         cone[4] = 30; cone[5] = 19; cone[6] = 20; cone[7] = 28;
14829566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
1483ae8bcbbbSMatthew G. Knepley         cone[0] = 35; cone[1] = 31; cone[2] = 32; cone[3] = 36;
1484ae8bcbbbSMatthew G. Knepley         cone[4] = 30; cone[5] = 25; cone[6] = 22; cone[7] = 19;
14859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
1486ae8bcbbbSMatthew G. Knepley 
1487ae8bcbbbSMatthew G. Knepley         cone[0] = 19; cone[1] = 20; cone[2] = 21; cone[3] = 22;
1488ae8bcbbbSMatthew G. Knepley         cone[4] = 15; cone[5] = 16; cone[6] = 17; cone[7] = 18;
14899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
1490ae8bcbbbSMatthew G. Knepley         cone[0] = 22; cone[1] = 21; cone[2] = 26; cone[3] = 25;
1491ae8bcbbbSMatthew G. Knepley         cone[4] = 16; cone[5] = 23; cone[6] = 24; cone[7] = 17;
14929566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
1493ae8bcbbbSMatthew G. Knepley         cone[0] = 20; cone[1] = 28; cone[2] = 26; cone[3] = 21;
1494ae8bcbbbSMatthew G. Knepley         cone[4] = 18; cone[5] = 17; cone[6] = 24; cone[7] = 27;
14959566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
1496ae8bcbbbSMatthew G. Knepley         cone[0] = 30; cone[1] = 28; cone[2] = 20; cone[3] = 19;
1497ae8bcbbbSMatthew G. Knepley         cone[4] = 29; cone[5] = 15; cone[6] = 18; cone[7] = 27;
14989566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
1499ae8bcbbbSMatthew G. Knepley         cone[0] = 30; cone[1] = 19; cone[2] = 22; cone[3] = 25;
1500ae8bcbbbSMatthew G. Knepley         cone[4] = 29; cone[5] = 23; cone[6] = 16; cone[7] = 15;
15019566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
1502006a8963SMatthew G. Knepley       } else {
150310c6f908SMatthew G. Knepley         cone[0] =  5; cone[1] =  8; cone[2] =  7; cone[3] =  6;
150410c6f908SMatthew G. Knepley         cone[4] =  9; cone[5] = 12; cone[6] = 11; cone[7] = 10;
15059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
150610c6f908SMatthew G. Knepley         cone[0] =  6; cone[1] =  7; cone[2] = 14; cone[3] = 13;
150710c6f908SMatthew G. Knepley         cone[4] = 12; cone[5] = 15; cone[6] = 16; cone[7] = 11;
15089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
150910c6f908SMatthew G. Knepley         cone[0] =  8; cone[1] = 17; cone[2] = 14; cone[3] =  7;
151010c6f908SMatthew G. Knepley         cone[4] = 10; cone[5] = 11; cone[6] = 16; cone[7] = 18;
15119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
151210c6f908SMatthew G. Knepley         cone[0] = 19; cone[1] = 17; cone[2] =  8; cone[3] =  5;
151310c6f908SMatthew G. Knepley         cone[4] = 20; cone[5] =  9; cone[6] = 10; cone[7] = 18;
15149566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
151510c6f908SMatthew G. Knepley         cone[0] = 19; cone[1] =  5; cone[2] =  6; cone[3] = 13;
151610c6f908SMatthew G. Knepley         cone[4] = 20; cone[5] = 15; cone[6] = 12; cone[7] =  9;
15179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1518006a8963SMatthew G. Knepley       }
1519d8c47e87SMatthew G. Knepley     }
15209566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
15219566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
15220510c589SMatthew G. Knepley   }
1523dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
15240510c589SMatthew G. Knepley   {
15250510c589SMatthew G. Knepley     Vec             coordinates;
15260510c589SMatthew G. Knepley     PetscSection    coordSection;
15270510c589SMatthew G. Knepley     PetscScalar    *coords;
15280510c589SMatthew G. Knepley     PetscInt        coordSize, v;
15290510c589SMatthew G. Knepley     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
15300510c589SMatthew G. Knepley     const PetscReal ds2 = dis/2.0;
15310510c589SMatthew G. Knepley 
15320510c589SMatthew G. Knepley     /* Build coordinates */
15339566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
15349566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
15359566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
15369566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells+numVertices));
15370510c589SMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
15389566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
15399566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
15400510c589SMatthew G. Knepley     }
15419566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
15429566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
15439566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
15449566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
15459566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
15469566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
15479566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates,VECSTANDARD));
15489566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
1549dd400576SPatrick Sanan     if (rank == 0) {
15500510c589SMatthew G. Knepley       coords[0*dim+0] = -ds2; coords[0*dim+1] = -ds2; coords[0*dim+2] = 0.0;
15510510c589SMatthew G. Knepley       coords[1*dim+0] =  ds2; coords[1*dim+1] = -ds2; coords[1*dim+2] = 0.0;
15520510c589SMatthew G. Knepley       coords[2*dim+0] =  ds2; coords[2*dim+1] =  ds2; coords[2*dim+2] = 0.0;
15530510c589SMatthew G. Knepley       coords[3*dim+0] = -ds2; coords[3*dim+1] =  ds2; coords[3*dim+2] = 0.0;
15540510c589SMatthew G. Knepley       coords[4*dim+0] = -ds2; coords[4*dim+1] = -ds2; coords[4*dim+2] = 1.0;
15550510c589SMatthew G. Knepley       coords[5*dim+0] = -ds2; coords[5*dim+1] =  ds2; coords[5*dim+2] = 1.0;
15560510c589SMatthew G. Knepley       coords[6*dim+0] =  ds2; coords[6*dim+1] =  ds2; coords[6*dim+2] = 1.0;
15570510c589SMatthew G. Knepley       coords[7*dim+0] =  ds2; coords[7*dim+1] = -ds2; coords[7*dim+2] = 1.0;
15580510c589SMatthew G. Knepley       coords[ 8*dim+0] =  dis; coords[ 8*dim+1] = -dis; coords[ 8*dim+2] = 0.0;
15590510c589SMatthew G. Knepley       coords[ 9*dim+0] =  dis; coords[ 9*dim+1] =  dis; coords[ 9*dim+2] = 0.0;
15600510c589SMatthew G. Knepley       coords[10*dim+0] =  dis; coords[10*dim+1] = -dis; coords[10*dim+2] = 1.0;
15610510c589SMatthew G. Knepley       coords[11*dim+0] =  dis; coords[11*dim+1] =  dis; coords[11*dim+2] = 1.0;
15620510c589SMatthew G. Knepley       coords[12*dim+0] = -dis; coords[12*dim+1] =  dis; coords[12*dim+2] = 0.0;
15630510c589SMatthew G. Knepley       coords[13*dim+0] = -dis; coords[13*dim+1] =  dis; coords[13*dim+2] = 1.0;
15640510c589SMatthew G. Knepley       coords[14*dim+0] = -dis; coords[14*dim+1] = -dis; coords[14*dim+2] = 0.0;
15650510c589SMatthew G. Knepley       coords[15*dim+0] = -dis; coords[15*dim+1] = -dis; coords[15*dim+2] = 1.0;
1566ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1567ae8bcbbbSMatthew G. Knepley         /* 15 31 19 */ coords[16*dim+0] = -ds2; coords[16*dim+1] = -ds2; coords[16*dim+2] = 0.5;
1568ae8bcbbbSMatthew G. Knepley         /* 16 32 22 */ coords[17*dim+0] =  ds2; coords[17*dim+1] = -ds2; coords[17*dim+2] = 0.5;
1569ae8bcbbbSMatthew G. Knepley         /* 17 33 21 */ coords[18*dim+0] =  ds2; coords[18*dim+1] =  ds2; coords[18*dim+2] = 0.5;
1570ae8bcbbbSMatthew G. Knepley         /* 18 34 20 */ coords[19*dim+0] = -ds2; coords[19*dim+1] =  ds2; coords[19*dim+2] = 0.5;
1571ae8bcbbbSMatthew G. Knepley         /* 29 35 30 */ coords[20*dim+0] = -dis; coords[20*dim+1] = -dis; coords[20*dim+2] = 0.5;
1572ae8bcbbbSMatthew G. Knepley         /* 23 36 25 */ coords[21*dim+0] =  dis; coords[21*dim+1] = -dis; coords[21*dim+2] = 0.5;
1573ae8bcbbbSMatthew G. Knepley         /* 24 37 26 */ coords[22*dim+0] =  dis; coords[22*dim+1] =  dis; coords[22*dim+2] = 0.5;
1574ae8bcbbbSMatthew G. Knepley         /* 27 38 28 */ coords[23*dim+0] = -dis; coords[23*dim+1] =  dis; coords[23*dim+2] = 0.5;
1575ae8bcbbbSMatthew G. Knepley       }
1576d8c47e87SMatthew G. Knepley     }
15779566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
15789566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
15799566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
15800510c589SMatthew G. Knepley   }
1581006a8963SMatthew G. Knepley   /* Create periodicity */
1582006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1583006a8963SMatthew G. Knepley     PetscReal      L[3];
1584006a8963SMatthew G. Knepley     PetscReal      maxCell[3];
1585006a8963SMatthew G. Knepley     DMBoundaryType bdType[3];
1586006a8963SMatthew G. Knepley     PetscReal      lower[3] = {0.0, 0.0, 0.0};
1587ae8bcbbbSMatthew G. Knepley     PetscReal      upper[3] = {1.0, 1.0, 1.5};
1588ae8bcbbbSMatthew G. Knepley     PetscInt       i, numZCells = 3;
1589006a8963SMatthew G. Knepley 
1590006a8963SMatthew G. Knepley     bdType[0] = DM_BOUNDARY_NONE;
1591006a8963SMatthew G. Knepley     bdType[1] = DM_BOUNDARY_NONE;
1592006a8963SMatthew G. Knepley     bdType[2] = periodicZ;
1593006a8963SMatthew G. Knepley     for (i = 0; i < dim; i++) {
1594006a8963SMatthew G. Knepley       L[i]       = upper[i] - lower[i];
1595006a8963SMatthew G. Knepley       maxCell[i] = 1.1 * (L[i] / numZCells);
1596006a8963SMatthew G. Knepley     }
15979566063dSJacob Faibussowitsch     PetscCall(DMSetPeriodicity(dm, PETSC_TRUE, maxCell, L, bdType));
1598006a8963SMatthew G. Knepley   }
1599dbc1dc17SMatthew G. Knepley   {
16009318fe57SMatthew G. Knepley     DM          cdm;
16019318fe57SMatthew G. Knepley     PetscDS     cds;
16029318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
1603dbc1dc17SMatthew G. Knepley 
16049566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, snapToCylinder));
16059566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
16069566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
16079566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
1608dbc1dc17SMatthew G. Knepley   }
16099318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
16109566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
16110510c589SMatthew G. Knepley   PetscFunctionReturn(0);
16120510c589SMatthew G. Knepley }
16130510c589SMatthew G. Knepley 
161424119c2aSMatthew G. Knepley /*@
16159318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
161624119c2aSMatthew G. Knepley 
1617d083f849SBarry Smith   Collective
161824119c2aSMatthew G. Knepley 
161924119c2aSMatthew G. Knepley   Input Parameters:
162024119c2aSMatthew G. Knepley + comm      - The communicator for the DM object
16219318fe57SMatthew G. Knepley - periodicZ - The boundary type for the Z direction
162224119c2aSMatthew G. Knepley 
162324119c2aSMatthew G. Knepley   Output Parameter:
162424119c2aSMatthew G. Knepley . dm  - The DM object
162524119c2aSMatthew G. Knepley 
16269318fe57SMatthew G. Knepley   Note:
16279318fe57SMatthew G. Knepley   Here is the output numbering looking from the bottom of the cylinder:
16289318fe57SMatthew G. Knepley $       17-----14
16299318fe57SMatthew G. Knepley $        |     |
16309318fe57SMatthew G. Knepley $        |  2  |
16319318fe57SMatthew G. Knepley $        |     |
16329318fe57SMatthew G. Knepley $ 17-----8-----7-----14
16339318fe57SMatthew G. Knepley $  |     |     |     |
16349318fe57SMatthew G. Knepley $  |  3  |  0  |  1  |
16359318fe57SMatthew G. Knepley $  |     |     |     |
16369318fe57SMatthew G. Knepley $ 19-----5-----6-----13
16379318fe57SMatthew G. Knepley $        |     |
16389318fe57SMatthew G. Knepley $        |  4  |
16399318fe57SMatthew G. Knepley $        |     |
16409318fe57SMatthew G. Knepley $       19-----13
16419318fe57SMatthew G. Knepley $
16429318fe57SMatthew G. Knepley $ and up through the top
16439318fe57SMatthew G. Knepley $
16449318fe57SMatthew G. Knepley $       18-----16
16459318fe57SMatthew G. Knepley $        |     |
16469318fe57SMatthew G. Knepley $        |  2  |
16479318fe57SMatthew G. Knepley $        |     |
16489318fe57SMatthew G. Knepley $ 18----10----11-----16
16499318fe57SMatthew G. Knepley $  |     |     |     |
16509318fe57SMatthew G. Knepley $  |  3  |  0  |  1  |
16519318fe57SMatthew G. Knepley $  |     |     |     |
16529318fe57SMatthew G. Knepley $ 20-----9----12-----15
16539318fe57SMatthew G. Knepley $        |     |
16549318fe57SMatthew G. Knepley $        |  4  |
16559318fe57SMatthew G. Knepley $        |     |
16569318fe57SMatthew G. Knepley $       20-----15
16579318fe57SMatthew G. Knepley 
165824119c2aSMatthew G. Knepley   Level: beginner
165924119c2aSMatthew G. Knepley 
16609318fe57SMatthew G. Knepley .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
166124119c2aSMatthew G. Knepley @*/
16629318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm)
16639318fe57SMatthew G. Knepley {
16649318fe57SMatthew G. Knepley   PetscFunctionBegin;
16659318fe57SMatthew G. Knepley   PetscValidPointer(dm, 3);
16669566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
16679566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
16689566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ));
16699318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
16709318fe57SMatthew G. Knepley }
16719318fe57SMatthew G. Knepley 
16729318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
167324119c2aSMatthew G. Knepley {
167424119c2aSMatthew G. Knepley   const PetscInt dim = 3;
1675412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
16769fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
167724119c2aSMatthew G. Knepley 
167824119c2aSMatthew G. Knepley   PetscFunctionBegin;
16792c71b3e2SJacob Faibussowitsch   PetscCheckFalse(n < 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %D cannot be negative", n);
16809566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
16819566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
1682412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
16839566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
168424119c2aSMatthew G. Knepley   /* Create topology */
168524119c2aSMatthew G. Knepley   {
168624119c2aSMatthew G. Knepley     PetscInt cone[6], c;
168724119c2aSMatthew G. Knepley 
1688dd400576SPatrick Sanan     numCells    = rank == 0 ?        n : 0;
1689dd400576SPatrick Sanan     numVertices = rank == 0 ?  2*(n+1) : 0;
16909566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells+numVertices));
16919566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
16929566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
169324119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
169424119c2aSMatthew G. Knepley       cone[0] =  c+n*1; cone[1] = (c+1)%n+n*1; cone[2] = 0+3*n;
169524119c2aSMatthew G. Knepley       cone[3] =  c+n*2; cone[4] = (c+1)%n+n*2; cone[5] = 1+3*n;
16969566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
16979566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
169824119c2aSMatthew G. Knepley     }
16999566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
17009566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
170124119c2aSMatthew G. Knepley   }
1702412e9a14SMatthew G. Knepley   for (v = numCells; v < numCells+numVertices; ++v) {
17039566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
170424119c2aSMatthew G. Knepley   }
170524119c2aSMatthew G. Knepley   /* Create cylinder geometry */
170624119c2aSMatthew G. Knepley   {
170724119c2aSMatthew G. Knepley     Vec          coordinates;
170824119c2aSMatthew G. Knepley     PetscSection coordSection;
170924119c2aSMatthew G. Knepley     PetscScalar *coords;
1710412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
171124119c2aSMatthew G. Knepley 
171224119c2aSMatthew G. Knepley     /* Build coordinates */
17139566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
17149566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
17159566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
17169566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells+numVertices));
171724119c2aSMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
17189566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
17199566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
172024119c2aSMatthew G. Knepley     }
17219566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
17229566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
17239566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
17249566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
17259566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
17269566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
17279566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates,VECSTANDARD));
17289566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
172924119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
173024119c2aSMatthew 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;
173124119c2aSMatthew 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;
173224119c2aSMatthew G. Knepley     }
1733dd400576SPatrick Sanan     if (rank == 0) {
173424119c2aSMatthew 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;
173524119c2aSMatthew 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;
17369fe9f049SMatthew G. Knepley     }
17379566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
17389566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
17399566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
174024119c2aSMatthew G. Knepley   }
17419318fe57SMatthew G. Knepley   /* Interpolate */
17429566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
17439318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
17449318fe57SMatthew G. Knepley }
17459318fe57SMatthew G. Knepley 
17469318fe57SMatthew G. Knepley /*@
17479318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
17489318fe57SMatthew G. Knepley 
17499318fe57SMatthew G. Knepley   Collective
17509318fe57SMatthew G. Knepley 
17519318fe57SMatthew G. Knepley   Input Parameters:
17529318fe57SMatthew G. Knepley + comm - The communicator for the DM object
17539318fe57SMatthew G. Knepley . n    - The number of wedges around the origin
17549318fe57SMatthew G. Knepley - interpolate - Create edges and faces
17559318fe57SMatthew G. Knepley 
17569318fe57SMatthew G. Knepley   Output Parameter:
17579318fe57SMatthew G. Knepley . dm  - The DM object
17589318fe57SMatthew G. Knepley 
17599318fe57SMatthew G. Knepley   Level: beginner
17609318fe57SMatthew G. Knepley 
17619318fe57SMatthew G. Knepley .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
17629318fe57SMatthew G. Knepley @*/
17639318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
17649318fe57SMatthew G. Knepley {
17659318fe57SMatthew G. Knepley   PetscFunctionBegin;
17669318fe57SMatthew G. Knepley   PetscValidPointer(dm, 4);
17679566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
17689566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
17699566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
177024119c2aSMatthew G. Knepley   PetscFunctionReturn(0);
177124119c2aSMatthew G. Knepley }
177224119c2aSMatthew G. Knepley 
17739fbee547SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
177465a81367SMatthew G. Knepley {
177565a81367SMatthew G. Knepley   PetscReal prod = 0.0;
177665a81367SMatthew G. Knepley   PetscInt  i;
177765a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
177865a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
177965a81367SMatthew G. Knepley }
17809fbee547SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
178165a81367SMatthew G. Knepley {
178265a81367SMatthew G. Knepley   PetscReal prod = 0.0;
178365a81367SMatthew G. Knepley   PetscInt  i;
178465a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i]*y[i];
178565a81367SMatthew G. Knepley   return prod;
178665a81367SMatthew G. Knepley }
178765a81367SMatthew G. Knepley 
178851a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
178951a74b61SMatthew G. Knepley static void snapToSphere(PetscInt dim, PetscInt Nf, PetscInt NfAux,
179051a74b61SMatthew G. Knepley                          const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
179151a74b61SMatthew G. Knepley                          const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
179251a74b61SMatthew G. Knepley                          PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
179351a74b61SMatthew G. Knepley {
179451a74b61SMatthew G. Knepley   PetscReal r = PetscRealPart(constants[0]);
179551a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
179651a74b61SMatthew G. Knepley   PetscInt  n = uOff[1] - uOff[0], d;
179751a74b61SMatthew G. Knepley 
179851a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
179951a74b61SMatthew G. Knepley   fac = r/PetscSqrtReal(norm2);
180051a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d]*fac;
180151a74b61SMatthew G. Knepley }
180251a74b61SMatthew G. Knepley 
18039318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
18042829fed8SMatthew G. Knepley {
180565a81367SMatthew G. Knepley   const PetscInt  embedDim = dim+1;
180665a81367SMatthew G. Knepley   PetscSection    coordSection;
180765a81367SMatthew G. Knepley   Vec             coordinates;
180865a81367SMatthew G. Knepley   PetscScalar    *coords;
180965a81367SMatthew G. Knepley   PetscReal      *coordsIn;
181065a81367SMatthew G. Knepley   PetscInt        numCells, numEdges, numVerts, firstVertex, v, firstEdge, coordSize, d, c, e;
181165a81367SMatthew G. Knepley   PetscMPIInt     rank;
181265a81367SMatthew G. Knepley 
181365a81367SMatthew G. Knepley   PetscFunctionBegin;
18149318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
18159566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
18169566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim+1));
18179566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
181865a81367SMatthew G. Knepley   switch (dim) {
181965a81367SMatthew G. Knepley   case 2:
182065a81367SMatthew G. Knepley     if (simplex) {
182151a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI*PETSC_PHI)/(1.0 + PETSC_PHI);
182251a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0/(1.0 + PETSC_PHI) * (R/radius);
182365a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
182451a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0/(1.0 + PETSC_PHI), PETSC_PHI/(1.0 + PETSC_PHI)};
182565a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
182665a81367SMatthew G. Knepley       PetscInt        cone[3];
182765a81367SMatthew G. Knepley       PetscInt       *graph, p, i, j, k;
182865a81367SMatthew G. Knepley 
182951a74b61SMatthew G. Knepley       vertex[0] *= R/radius; vertex[1] *= R/radius; vertex[2] *= R/radius;
1830dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
1831dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
183265a81367SMatthew G. Knepley       firstVertex = numCells;
183351a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
183465a81367SMatthew G. Knepley 
183565a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
183665a81367SMatthew G. Knepley 
183765a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
183851a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
183965a81367SMatthew G. Knepley       */
184065a81367SMatthew G. Knepley       /* Construct vertices */
18419566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
1842dd400576SPatrick Sanan       if (rank == 0) {
184365a81367SMatthew G. Knepley         for (p = 0, i = 0; p < embedDim; ++p) {
184465a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
184565a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
184665a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertex[(d+p)%embedDim];
184765a81367SMatthew G. Knepley               ++i;
184865a81367SMatthew G. Knepley             }
184965a81367SMatthew G. Knepley           }
185065a81367SMatthew G. Knepley         }
185145da822fSValeria Barra       }
185265a81367SMatthew G. Knepley       /* Construct graph */
18539566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
185465a81367SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
185565a81367SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
185665a81367SMatthew G. Knepley           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
185765a81367SMatthew G. Knepley         }
18582c71b3e2SJacob Faibussowitsch         PetscCheckFalse(k != degree,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %D degree %D != %D", i, k, degree);
185965a81367SMatthew G. Knepley       }
186065a81367SMatthew G. Knepley       /* Build Topology */
18619566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells+numVerts));
186265a81367SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
18639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(dm, c, embedDim));
186465a81367SMatthew G. Knepley       }
18659566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
186665a81367SMatthew G. Knepley       /* Cells */
186765a81367SMatthew G. Knepley       for (i = 0, c = 0; i < numVerts; ++i) {
186865a81367SMatthew G. Knepley         for (j = 0; j < i; ++j) {
186965a81367SMatthew G. Knepley           for (k = 0; k < j; ++k) {
187065a81367SMatthew G. Knepley             if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i]) {
187165a81367SMatthew G. Knepley               cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k;
187265a81367SMatthew G. Knepley               /* Check orientation */
187365a81367SMatthew G. Knepley               {
187465a81367SMatthew 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}}};
187565a81367SMatthew G. Knepley                 PetscReal normal[3];
187665a81367SMatthew G. Knepley                 PetscInt  e, f;
187765a81367SMatthew G. Knepley 
187865a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
187965a81367SMatthew G. Knepley                   normal[d] = 0.0;
188065a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
188165a81367SMatthew G. Knepley                     for (f = 0; f < embedDim; ++f) {
188265a81367SMatthew G. Knepley                       normal[d] += epsilon[d][e][f]*(coordsIn[j*embedDim+e] - coordsIn[i*embedDim+e])*(coordsIn[k*embedDim+f] - coordsIn[i*embedDim+f]);
188365a81367SMatthew G. Knepley                     }
188465a81367SMatthew G. Knepley                   }
188565a81367SMatthew G. Knepley                 }
188665a81367SMatthew G. Knepley                 if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
188765a81367SMatthew G. Knepley               }
18889566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
188965a81367SMatthew G. Knepley             }
189065a81367SMatthew G. Knepley           }
189165a81367SMatthew G. Knepley         }
189265a81367SMatthew G. Knepley       }
18939566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
18949566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
18959566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
189665a81367SMatthew G. Knepley     } else {
18972829fed8SMatthew G. Knepley       /*
18982829fed8SMatthew G. Knepley         12-21--13
18992829fed8SMatthew G. Knepley          |     |
19002829fed8SMatthew G. Knepley         25  4  24
19012829fed8SMatthew G. Knepley          |     |
19022829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
19032829fed8SMatthew G. Knepley    |     |     |     |
19042829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
19052829fed8SMatthew G. Knepley    |     |     |     |
19062829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
19072829fed8SMatthew G. Knepley          |     |
19082829fed8SMatthew G. Knepley         20  1  19
19092829fed8SMatthew G. Knepley          |     |
19102829fed8SMatthew G. Knepley         10-18--11
19112829fed8SMatthew G. Knepley          |     |
19122829fed8SMatthew G. Knepley         23  2  22
19132829fed8SMatthew G. Knepley          |     |
19142829fed8SMatthew G. Knepley         12-21--13
19152829fed8SMatthew G. Knepley        */
19162829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
19172829fed8SMatthew G. Knepley 
1918dd400576SPatrick Sanan       numCells    = rank == 0 ?  6 : 0;
1919dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
1920dd400576SPatrick Sanan       numVerts    = rank == 0 ?  8 : 0;
192165a81367SMatthew G. Knepley       firstVertex = numCells;
192265a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
19232829fed8SMatthew G. Knepley       /* Build Topology */
19249566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells+numEdges+numVerts));
19252829fed8SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
19269566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(dm, c, 4));
19272829fed8SMatthew G. Knepley       }
19282829fed8SMatthew G. Knepley       for (e = firstEdge; e < firstEdge+numEdges; ++e) {
19299566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(dm, e, 2));
19302829fed8SMatthew G. Knepley       }
19319566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
1932dd400576SPatrick Sanan       if (rank == 0) {
19332829fed8SMatthew G. Knepley         /* Cell 0 */
19342829fed8SMatthew G. Knepley         cone[0] = 14; cone[1] = 15; cone[2] = 16; cone[3] = 17;
19359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
19362829fed8SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = 0; ornt[3] = 0;
19379566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
19382829fed8SMatthew G. Knepley         /* Cell 1 */
19392829fed8SMatthew G. Knepley         cone[0] = 18; cone[1] = 19; cone[2] = 14; cone[3] = 20;
19409566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
1941b5a892a1SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = -1; ornt[3] = 0;
19429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
19432829fed8SMatthew G. Knepley         /* Cell 2 */
19442829fed8SMatthew G. Knepley         cone[0] = 21; cone[1] = 22; cone[2] = 18; cone[3] = 23;
19459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
1946b5a892a1SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = -1; ornt[3] = 0;
19479566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
19482829fed8SMatthew G. Knepley         /* Cell 3 */
19492829fed8SMatthew G. Knepley         cone[0] = 19; cone[1] = 22; cone[2] = 24; cone[3] = 15;
19509566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
1951b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = 0; ornt[3] = -1;
19529566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
19532829fed8SMatthew G. Knepley         /* Cell 4 */
19542829fed8SMatthew G. Knepley         cone[0] = 16; cone[1] = 24; cone[2] = 21; cone[3] = 25;
19559566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1956b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = -1; ornt[3] = 0;
19579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
19582829fed8SMatthew G. Knepley         /* Cell 5 */
19592829fed8SMatthew G. Knepley         cone[0] = 20; cone[1] = 17; cone[2] = 25; cone[3] = 23;
19609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
1961b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = -1; ornt[3] = -1;
19629566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
19632829fed8SMatthew G. Knepley         /* Edges */
19642829fed8SMatthew G. Knepley         cone[0] =  6; cone[1] =  7;
19659566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
19662829fed8SMatthew G. Knepley         cone[0] =  7; cone[1] =  8;
19679566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
19682829fed8SMatthew G. Knepley         cone[0] =  8; cone[1] =  9;
19699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
19702829fed8SMatthew G. Knepley         cone[0] =  9; cone[1] =  6;
19719566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
19722829fed8SMatthew G. Knepley         cone[0] = 10; cone[1] = 11;
19739566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
19742829fed8SMatthew G. Knepley         cone[0] = 11; cone[1] =  7;
19759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
19762829fed8SMatthew G. Knepley         cone[0] =  6; cone[1] = 10;
19779566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
19782829fed8SMatthew G. Knepley         cone[0] = 12; cone[1] = 13;
19799566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
19802829fed8SMatthew G. Knepley         cone[0] = 13; cone[1] = 11;
19819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
19822829fed8SMatthew G. Knepley         cone[0] = 10; cone[1] = 12;
19839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
19842829fed8SMatthew G. Knepley         cone[0] = 13; cone[1] =  8;
19859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
19862829fed8SMatthew G. Knepley         cone[0] = 12; cone[1] =  9;
19879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
198845da822fSValeria Barra       }
19899566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
19909566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
19912829fed8SMatthew G. Knepley       /* Build coordinates */
19929566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
1993dd400576SPatrick Sanan       if (rank == 0) {
199451a74b61SMatthew G. Knepley         coordsIn[0*embedDim+0] = -R; coordsIn[0*embedDim+1] =  R; coordsIn[0*embedDim+2] = -R;
199551a74b61SMatthew G. Knepley         coordsIn[1*embedDim+0] =  R; coordsIn[1*embedDim+1] =  R; coordsIn[1*embedDim+2] = -R;
199651a74b61SMatthew G. Knepley         coordsIn[2*embedDim+0] =  R; coordsIn[2*embedDim+1] = -R; coordsIn[2*embedDim+2] = -R;
199751a74b61SMatthew G. Knepley         coordsIn[3*embedDim+0] = -R; coordsIn[3*embedDim+1] = -R; coordsIn[3*embedDim+2] = -R;
199851a74b61SMatthew G. Knepley         coordsIn[4*embedDim+0] = -R; coordsIn[4*embedDim+1] =  R; coordsIn[4*embedDim+2] =  R;
199951a74b61SMatthew G. Knepley         coordsIn[5*embedDim+0] =  R; coordsIn[5*embedDim+1] =  R; coordsIn[5*embedDim+2] =  R;
200051a74b61SMatthew G. Knepley         coordsIn[6*embedDim+0] = -R; coordsIn[6*embedDim+1] = -R; coordsIn[6*embedDim+2] =  R;
200151a74b61SMatthew G. Knepley         coordsIn[7*embedDim+0] =  R; coordsIn[7*embedDim+1] = -R; coordsIn[7*embedDim+2] =  R;
200265a81367SMatthew G. Knepley       }
200345da822fSValeria Barra     }
200465a81367SMatthew G. Knepley     break;
200565a81367SMatthew G. Knepley   case 3:
2006116ded15SMatthew G. Knepley     if (simplex) {
2007116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0/PETSC_PHI;
200851a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
200951a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
201051a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5*PETSC_PHI, 0.5/PETSC_PHI, 0.0};
2011116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2012116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
2013116ded15SMatthew 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},
2014116ded15SMatthew 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}};
2015116ded15SMatthew G. Knepley       PetscInt        cone[4];
2016116ded15SMatthew G. Knepley       PetscInt       *graph, p, i, j, k, l;
2017116ded15SMatthew G. Knepley 
201851a74b61SMatthew G. Knepley       vertexA[0] *= R; vertexA[1] *= R; vertexA[2] *= R; vertexA[3] *= R;
201951a74b61SMatthew G. Knepley       vertexB[0] *= R; vertexB[1] *= R; vertexB[2] *= R; vertexB[3] *= R;
202051a74b61SMatthew G. Knepley       vertexC[0] *= R; vertexC[1] *= R; vertexC[2] *= R; vertexC[3] *= R;
2021dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
2022dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
2023116ded15SMatthew G. Knepley       firstVertex = numCells;
2024116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2025116ded15SMatthew G. Knepley 
2026116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2027116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2028116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2029116ded15SMatthew G. Knepley 
2030116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
20316333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2032116ded15SMatthew G. Knepley 
2033116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2034116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2035116ded15SMatthew G. Knepley       */
2036116ded15SMatthew G. Knepley       /* Construct vertices */
20379566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2038116ded15SMatthew G. Knepley       i    = 0;
2039dd400576SPatrick Sanan       if (rank == 0) {
2040116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2041116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2042116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2043116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2044116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[d]*vertexA[d];
2045116ded15SMatthew G. Knepley                 ++i;
2046116ded15SMatthew G. Knepley               }
2047116ded15SMatthew G. Knepley             }
2048116ded15SMatthew G. Knepley           }
2049116ded15SMatthew G. Knepley         }
2050116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2051116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2052116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2053116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertexB[(d+p)%embedDim];
2054116ded15SMatthew G. Knepley             ++i;
2055116ded15SMatthew G. Knepley           }
2056116ded15SMatthew G. Knepley         }
2057116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2058116ded15SMatthew G. Knepley           s[3] = 1;
2059116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2060116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2061116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2062116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[evenPerm[p][d]]*vertexC[evenPerm[p][d]];
2063116ded15SMatthew G. Knepley                 ++i;
2064116ded15SMatthew G. Knepley               }
2065116ded15SMatthew G. Knepley             }
2066116ded15SMatthew G. Knepley           }
2067116ded15SMatthew G. Knepley         }
206845da822fSValeria Barra       }
20692c71b3e2SJacob Faibussowitsch       PetscCheckFalse(i != numVerts,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %D != %D", i, numVerts);
2070116ded15SMatthew G. Knepley       /* Construct graph */
20719566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
2072116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2073116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
2074116ded15SMatthew G. Knepley           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
2075116ded15SMatthew G. Knepley         }
20762c71b3e2SJacob Faibussowitsch         PetscCheckFalse(k != degree,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %D degree %D != %D", i, k, degree);
2077116ded15SMatthew G. Knepley       }
2078116ded15SMatthew G. Knepley       /* Build Topology */
20799566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells+numVerts));
2080116ded15SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
20819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(dm, c, embedDim));
2082116ded15SMatthew G. Knepley       }
20839566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2084116ded15SMatthew G. Knepley       /* Cells */
2085dd400576SPatrick Sanan       if (rank == 0) {
2086116ded15SMatthew G. Knepley         for (i = 0, c = 0; i < numVerts; ++i) {
2087116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2088116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2089116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
2090116ded15SMatthew G. Knepley                 if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i] &&
2091116ded15SMatthew G. Knepley                     graph[l*numVerts+i] && graph[l*numVerts+j] && graph[l*numVerts+k]) {
2092116ded15SMatthew G. Knepley                   cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k; cone[3] = firstVertex+l;
2093116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2094116ded15SMatthew G. Knepley                   {
2095116ded15SMatthew 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}},
2096116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  1}, { 0,  0, -1, 0}},
2097116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0, -1}, { 0,  0, 0,  0}, { 0,  1,  0, 0}},
2098116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  1,  0}, { 0, -1, 0,  0}, { 0,  0,  0, 0}}},
2099116ded15SMatthew G. Knepley 
2100116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0, -1}, { 0,  0,  1, 0}},
2101116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2102116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, {-1,  0,  0, 0}},
2103116ded15SMatthew G. Knepley                                                            {{0,  0, -1,  0}, { 0, 0,  0,  0}, { 1,  0, 0,  0}, { 0,  0,  0, 0}}},
2104116ded15SMatthew G. Knepley 
2105116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  1}, { 0,  0, 0,  0}, { 0, -1,  0, 0}},
2106116ded15SMatthew G. Knepley                                                            {{0,  0,  0, -1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 1,  0,  0, 0}},
2107116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2108116ded15SMatthew G. Knepley                                                            {{0,  1,  0,  0}, {-1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}},
2109116ded15SMatthew G. Knepley 
2110116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0, -1,  0}, { 0,  1, 0,  0}, { 0,  0,  0, 0}},
2111116ded15SMatthew G. Knepley                                                            {{0,  0,  1,  0}, { 0, 0,  0,  0}, {-1,  0, 0,  0}, { 0,  0,  0, 0}},
2112116ded15SMatthew G. Knepley                                                            {{0, -1,  0,  0}, { 1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2113116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}}};
2114116ded15SMatthew G. Knepley                     PetscReal normal[4];
2115116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2116116ded15SMatthew G. Knepley 
2117116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2118116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2119116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2120116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2121116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2122116ded15SMatthew 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]);
2123116ded15SMatthew G. Knepley                           }
2124116ded15SMatthew G. Knepley                         }
2125116ded15SMatthew G. Knepley                       }
2126116ded15SMatthew G. Knepley                     }
2127116ded15SMatthew G. Knepley                     if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
2128116ded15SMatthew G. Knepley                   }
21299566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
2130116ded15SMatthew G. Knepley                 }
2131116ded15SMatthew G. Knepley               }
2132116ded15SMatthew G. Knepley             }
2133116ded15SMatthew G. Knepley           }
2134116ded15SMatthew G. Knepley         }
213545da822fSValeria Barra       }
21369566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
21379566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
21389566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
2139116ded15SMatthew G. Knepley       break;
2140116ded15SMatthew G. Knepley     }
214198921bdaSJacob Faibussowitsch   default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %D", dim);
214265a81367SMatthew G. Knepley   }
214365a81367SMatthew G. Knepley   /* Create coordinates */
21449566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
21459566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
21469566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
21479566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVerts));
21482829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex+numVerts; ++v) {
21499566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
21509566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
21512829fed8SMatthew G. Knepley   }
21529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
21539566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
21549566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
21559566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
21569566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
21579566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
21589566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
21599566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
216065a81367SMatthew G. Knepley   for (v = 0; v < numVerts; ++v) for (d = 0; d < embedDim; ++d) {coords[v*embedDim+d] = coordsIn[v*embedDim+d];}
21619566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
21629566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
21639566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
21649566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
216551a74b61SMatthew G. Knepley   {
216651a74b61SMatthew G. Knepley     DM          cdm;
216751a74b61SMatthew G. Knepley     PetscDS     cds;
21689318fe57SMatthew G. Knepley     PetscScalar c = R;
216951a74b61SMatthew G. Knepley 
21709566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, snapToSphere));
21719566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
21729566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
21739566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
217451a74b61SMatthew G. Knepley   }
21759318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
21769566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
21779318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
21789318fe57SMatthew G. Knepley }
21799318fe57SMatthew G. Knepley 
2180b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal*, PetscReal[], PetscReal(*)[3]);
2181b7f5c055SJed Brown 
2182b7f5c055SJed Brown /*
2183b7f5c055SJed Brown  The Schwarz P implicit surface is
2184b7f5c055SJed Brown 
2185b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
2186b7f5c055SJed Brown */
2187b7f5c055SJed Brown static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2188b7f5c055SJed Brown {
2189b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
2190b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
2191b7f5c055SJed Brown   f[0] = c[0] + c[1] + c[2];
2192b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) {
2193b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
2194b7f5c055SJed Brown     for (PetscInt j=0; j<3; j++) {
2195b7f5c055SJed Brown       hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
2196b7f5c055SJed Brown     }
2197b7f5c055SJed Brown   }
2198b7f5c055SJed Brown }
2199b7f5c055SJed Brown 
2200b7f5c055SJed Brown /*
2201b7f5c055SJed Brown  The Gyroid implicit surface is
2202b7f5c055SJed Brown 
2203b7f5c055SJed 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)
2204b7f5c055SJed Brown 
2205b7f5c055SJed Brown */
2206b7f5c055SJed Brown static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2207b7f5c055SJed Brown {
2208b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
2209b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
2210b7f5c055SJed Brown   f[0] = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
2211b7f5c055SJed Brown   grad[0] = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
2212b7f5c055SJed Brown   grad[1] = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
2213b7f5c055SJed Brown   grad[2] = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
2214b7f5c055SJed Brown   hess[0][0] = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
2215b7f5c055SJed Brown   hess[0][1] = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2216b7f5c055SJed Brown   hess[0][2] = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2217b7f5c055SJed Brown   hess[1][0] = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
2218b7f5c055SJed Brown   hess[1][1] = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2219b7f5c055SJed Brown   hess[2][2] = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2220b7f5c055SJed Brown   hess[2][0] = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
2221b7f5c055SJed Brown   hess[2][1] = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2222b7f5c055SJed Brown   hess[2][2] = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2223b7f5c055SJed Brown }
2224b7f5c055SJed Brown 
2225b7f5c055SJed Brown /*
2226b7f5c055SJed Brown    We wish to solve
2227b7f5c055SJed Brown 
2228b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
2229b7f5c055SJed Brown 
2230b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
2231b7f5c055SJed 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
2232b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
2233b7f5c055SJed Brown 
2234b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
2235b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
2236b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
2237b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
2238b7f5c055SJed Brown    Since Q is symmetric, this is equivalent to multipyling by Q and taking the last two entries.
2239b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
2240b7f5c055SJed Brown 
2241b7f5c055SJed Brown      f(y) = 0                       1 equation
2242b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
2243b7f5c055SJed Brown 
2244b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
2245b7f5c055SJed Brown */
2246b7f5c055SJed Brown static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
2247b7f5c055SJed Brown {
2248b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
2249b7f5c055SJed Brown   PetscReal d[3] = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
2250b7f5c055SJed Brown   PetscReal f, grad[3], n[3], n_y[3][3], norm, norm_y[3], nd, nd_y[3], sign;
2251b7f5c055SJed Brown 
2252b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
2253b7f5c055SJed Brown 
2254b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) n[i] = grad[i];
2255b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2256b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) {
2257b7f5c055SJed Brown     norm_y[i] = 1. / norm * n[i] * n_y[i][i];
2258b7f5c055SJed Brown   }
2259b7f5c055SJed Brown 
2260b7f5c055SJed Brown   // Define the Householder reflector
2261b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
2262b7f5c055SJed Brown   n[0] += norm * sign;
2263b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) n_y[0][i] += norm_y[i] * sign;
2264b7f5c055SJed Brown 
2265b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2266b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
2267b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
2268b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
2269b7f5c055SJed Brown 
2270b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) {
2271b7f5c055SJed Brown     n[i] /= norm;
2272b7f5c055SJed Brown     for (PetscInt j=0; j<3; j++) {
2273b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
2274b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
2275b7f5c055SJed Brown     }
2276b7f5c055SJed Brown   }
2277b7f5c055SJed Brown 
2278b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
2279b7f5c055SJed 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];
2280b7f5c055SJed Brown 
2281b7f5c055SJed Brown   res[0] = f;
2282b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
2283b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
2284b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
2285b7f5c055SJed Brown   for (PetscInt j=0; j<3; j++) {
2286b7f5c055SJed Brown     J[0 + j*3] = grad[j];
2287b7f5c055SJed Brown     J[1 + j*3] = (j == 1)*1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
2288b7f5c055SJed Brown     J[2 + j*3] = (j == 2)*1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
2289b7f5c055SJed Brown   }
2290b7f5c055SJed Brown }
2291b7f5c055SJed Brown 
2292b7f5c055SJed Brown /*
2293b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
2294b7f5c055SJed Brown */
2295b7f5c055SJed Brown static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
2296b7f5c055SJed Brown {
2297b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
2298b7f5c055SJed Brown 
2299b7f5c055SJed Brown   PetscFunctionBegin;
2300b7f5c055SJed Brown   for (PetscInt iter=0; iter<10; iter++) {
2301b7f5c055SJed Brown     PetscScalar res[3], J[9];
2302b7f5c055SJed Brown     PetscReal resnorm;
2303b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
2304b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
2305b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
23069566063dSJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%D] res [%g %g %g]\n", iter, PetscRealPart(res[0]), PetscRealPart(res[1]), PetscRealPart(res[2])));
2307b7f5c055SJed Brown     }
2308b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
2309b7f5c055SJed Brown 
2310b7f5c055SJed Brown     // Take the Newton step
23119566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
2312b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
2313b7f5c055SJed Brown   }
2314b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) x[i] = y[i];
2315b7f5c055SJed Brown   PetscFunctionReturn(0);
2316b7f5c055SJed Brown }
2317b7f5c055SJed Brown 
2318b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
2319b7f5c055SJed Brown 
23201436d7faSJed Brown static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
2321b7f5c055SJed Brown {
2322b7f5c055SJed Brown   PetscMPIInt rank;
2323b7f5c055SJed Brown   PetscInt topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
2324b7f5c055SJed Brown   PetscInt (*edges)[2] = NULL, *edgeSets = NULL;
2325b7f5c055SJed Brown   PetscInt *cells_flat = NULL;
2326b7f5c055SJed Brown   PetscReal *vtxCoords = NULL;
2327b7f5c055SJed Brown   TPSEvaluateFunc evalFunc = NULL;
2328b7f5c055SJed Brown   DMLabel label;
2329b7f5c055SJed Brown 
2330b7f5c055SJed Brown   PetscFunctionBegin;
23319566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
2332b7f5c055SJed Brown   PetscCheck((layers != 0) ^ (thickness == 0.), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_INCOMP, "Layers %D must be nonzero iff thickness %g is nonzero", layers, (double)thickness);
2333b7f5c055SJed Brown   switch (tpstype) {
2334b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
2335b7f5c055SJed 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");
2336b7f5c055SJed Brown     if (!rank) {
2337b7f5c055SJed Brown       PetscInt (*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
2338b7f5c055SJed Brown       PetscInt Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
2339b7f5c055SJed Brown       PetscReal L = 1;
2340b7f5c055SJed Brown 
2341b7f5c055SJed Brown       Npipes[0] = (extent[0] + 1) * extent[1] * extent[2];
2342b7f5c055SJed Brown       Npipes[1] = extent[0] * (extent[1] + 1) * extent[2];
2343b7f5c055SJed Brown       Npipes[2] = extent[0] * extent[1] * (extent[2] + 1);
2344b7f5c055SJed Brown       Njunctions = extent[0] * extent[1] * extent[2];
2345b7f5c055SJed Brown       Ncuts = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
2346b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
23479566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3*numVertices, &vtxCoords));
23489566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
23499566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts*4, &edges));
23509566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts*4, &edgeSets));
2351b7f5c055SJed Brown       // x-normal pipes
2352b7f5c055SJed Brown       vcount = 0;
2353b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]+1; i++) {
2354b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]; j++) {
2355b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]; k++) {
2356b7f5c055SJed Brown             for (PetscInt l=0; l<4; l++) {
2357b7f5c055SJed Brown               vtxCoords[vcount++] = (2*i - 1) * L;
2358b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2359b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2360b7f5c055SJed Brown             }
2361b7f5c055SJed Brown           }
2362b7f5c055SJed Brown         }
2363b7f5c055SJed Brown       }
2364b7f5c055SJed Brown       // y-normal pipes
2365b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]; i++) {
2366b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]+1; j++) {
2367b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]; k++) {
2368b7f5c055SJed Brown             for (PetscInt l=0; l<4; l++) {
2369b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2370b7f5c055SJed Brown               vtxCoords[vcount++] = (2*j - 1) * L;
2371b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2372b7f5c055SJed Brown             }
2373b7f5c055SJed Brown           }
2374b7f5c055SJed Brown         }
2375b7f5c055SJed Brown       }
2376b7f5c055SJed Brown       // z-normal pipes
2377b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]; i++) {
2378b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]; j++) {
2379b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]+1; k++) {
2380b7f5c055SJed Brown             for (PetscInt l=0; l<4; l++) {
2381b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2382b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2383b7f5c055SJed Brown               vtxCoords[vcount++] = (2*k - 1) * L;
2384b7f5c055SJed Brown             }
2385b7f5c055SJed Brown           }
2386b7f5c055SJed Brown         }
2387b7f5c055SJed Brown       }
2388b7f5c055SJed Brown       // junctions
2389b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]; i++) {
2390b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]; j++) {
2391b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]; k++) {
2392b7f5c055SJed Brown             const PetscInt J = (i*extent[1] + j)*extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2])*4 + J*8;
2393b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
2394b7f5c055SJed Brown             for (PetscInt ii=0; ii<2; ii++) {
2395b7f5c055SJed Brown               for (PetscInt jj=0; jj<2; jj++) {
2396b7f5c055SJed Brown                 for (PetscInt kk=0; kk<2; kk++) {
2397b7f5c055SJed Brown                   double Ls = (1 - sqrt(2) / 4) * L;
2398b7f5c055SJed Brown                   vtxCoords[vcount++] = 2*i*L + (2*ii-1) * Ls;
2399b7f5c055SJed Brown                   vtxCoords[vcount++] = 2*j*L + (2*jj-1) * Ls;
2400b7f5c055SJed Brown                   vtxCoords[vcount++] = 2*k*L + (2*kk-1) * Ls;
2401b7f5c055SJed Brown                 }
2402b7f5c055SJed Brown               }
2403b7f5c055SJed Brown             }
2404b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
2405b7f5c055SJed Brown               {{3,1,0,2}, {7,5,4,6}}, // x-aligned
2406b7f5c055SJed Brown               {{5,4,0,1}, {7,6,2,3}}, // y-aligned
2407b7f5c055SJed Brown               {{6,2,0,4}, {7,3,1,5}}  // z-aligned
2408b7f5c055SJed Brown             };
2409b7f5c055SJed Brown             const PetscInt pipe_lo[3] = { // vertex numbers of pipes
2410b7f5c055SJed Brown               ((i * extent[1] + j) * extent[2] + k)*4,
2411b7f5c055SJed Brown               ((i * (extent[1] + 1) + j) * extent[2] + k + Npipes[0])*4,
2412b7f5c055SJed Brown               ((i * extent[1] + j) * (extent[2]+1) + k + Npipes[0] + Npipes[1])*4
2413b7f5c055SJed Brown             };
2414b7f5c055SJed Brown             const PetscInt pipe_hi[3] = { // vertex numbers of pipes
2415b7f5c055SJed Brown               (((i + 1) * extent[1] + j) * extent[2] + k)*4,
2416b7f5c055SJed Brown               ((i * (extent[1] + 1) + j + 1) * extent[2] + k + Npipes[0])*4,
2417b7f5c055SJed Brown               ((i * extent[1] + j) * (extent[2]+1) + k + 1 + Npipes[0] + Npipes[1])*4
2418b7f5c055SJed Brown             };
2419b7f5c055SJed Brown             for (PetscInt dir=0; dir<3; dir++) { // x,y,z
2420b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
2421b7f5c055SJed Brown               for (PetscInt l=0; l<4; l++) { // rotations
2422b7f5c055SJed Brown                 cells[J][dir*2+0][l][0] = pipe_lo[dir] + l;
2423b7f5c055SJed Brown                 cells[J][dir*2+0][l][1] = Jvoff + jfaces[dir][0][l];
2424b7f5c055SJed Brown                 cells[J][dir*2+0][l][2] = Jvoff + jfaces[dir][0][(l-1+4)%4];
2425b7f5c055SJed Brown                 cells[J][dir*2+0][l][3] = pipe_lo[dir] + (l-1+4)%4;
2426b7f5c055SJed Brown                 cells[J][dir*2+1][l][0] = Jvoff + jfaces[dir][1][l];
2427b7f5c055SJed Brown                 cells[J][dir*2+1][l][1] = pipe_hi[dir] + l;
2428b7f5c055SJed Brown                 cells[J][dir*2+1][l][2] = pipe_hi[dir] + (l-1+4)%4;
2429b7f5c055SJed Brown                 cells[J][dir*2+1][l][3] = Jvoff + jfaces[dir][1][(l-1+4)%4];
2430b7f5c055SJed Brown                 if (ijk[dir] == 0) {
2431b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
2432b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l+1) % 4;
2433b7f5c055SJed Brown                   edgeSets[numEdges] = dir*2 + 1;
2434b7f5c055SJed Brown                   numEdges++;
2435b7f5c055SJed Brown                 }
2436b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
2437b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
2438b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l+1) % 4;
2439b7f5c055SJed Brown                   edgeSets[numEdges] = dir*2 + 2;
2440b7f5c055SJed Brown                   numEdges++;
2441b7f5c055SJed Brown                 }
2442b7f5c055SJed Brown               }
2443b7f5c055SJed Brown             }
2444b7f5c055SJed Brown           }
2445b7f5c055SJed Brown         }
2446b7f5c055SJed Brown       }
2447b7f5c055SJed Brown       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %D incompatible with number of cuts %D", numEdges, Ncuts);
2448b7f5c055SJed Brown       numFaces = 24 * Njunctions;
2449b7f5c055SJed Brown       cells_flat = cells[0][0][0];
2450b7f5c055SJed Brown     }
2451b7f5c055SJed Brown     evalFunc = TPSEvaluate_SchwarzP;
2452b7f5c055SJed Brown     break;
2453b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
2454b7f5c055SJed Brown     if (!rank) {
2455b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
2456b7f5c055SJed Brown       //
2457b7f5c055SJed 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)
2458b7f5c055SJed Brown       //
2459b7f5c055SJed Brown       // on the cell [0,2]^3.
2460b7f5c055SJed Brown       //
2461b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
2462b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
2463b7f5c055SJed Brown       // like a boomerang:
2464b7f5c055SJed Brown       //
2465b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
2466b7f5c055SJed Brown       //     -----          -------        -------        -------     //
2467b7f5c055SJed Brown       //                                                              //
2468b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
2469b7f5c055SJed Brown       //      \                                   /            \      //
2470b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
2471b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
2472b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
2473b7f5c055SJed Brown       //                                                              //
2474b7f5c055SJed Brown       //                                                              //
2475b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
2476b7f5c055SJed Brown       //     -----          -------        -------        -------     //
2477b7f5c055SJed Brown       //                                                              //
2478b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
2479b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
2480b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
2481b7f5c055SJed Brown       //            \                       /                \        //
2482b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
2483b7f5c055SJed Brown       //
2484b7f5c055SJed Brown       //
2485b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
2486b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
2487b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
2488b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
2489b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
2490b7f5c055SJed Brown       //
2491b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
2492b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
2493b7f5c055SJed Brown       //
2494b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
2495b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
2496b7f5c055SJed Brown 
2497b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
2498b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
2499b7f5c055SJed Brown       PetscInt extentPlus[3];
2500b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
2501b7f5c055SJed Brown       const PetscInt A =  0,   B =  1,   C =  2,   D =  3,   E =  4,   F =  5,   G =  6,   H =  7,
2502b7f5c055SJed Brown         II =  8,   J =  9,   K = 10,   L = 11,   M = 12,   N = 13,   O = 14,   P = 15,
2503b7f5c055SJed Brown         Q = 16,   R = 17,   S = 18,   T = 19,   U = 20,   V = 21,   W = 22,   X = 23,
2504b7f5c055SJed Brown         Y = 24,   Z = 25,  Ap = 26,  Bp = 27,  Cp = 28,  Dp = 29,  Ep = 30,  Fp = 31,
2505b7f5c055SJed Brown         Gp = 32,  Hp = 33,  Ip = 34,  Jp = 35,  Kp = 36,  Lp = 37,  Mp = 38,  Np = 39,
2506b7f5c055SJed Brown         Op = 40,  Pp = 41,  Qp = 42,  Rp = 43,  Sp = 44,  Tp = 45,  Up = 46,  Vp = 47,
2507b7f5c055SJed Brown         Wp = 48,  Xp = 49,  Yp = 50,  Zp = 51,  Aq = 52,  Bq = 53,  Cq = 54,  Dq = 55;
2508b7f5c055SJed Brown       const PetscInt pattern[64][4] =
2509b7f5c055SJed Brown         { /* face to vertex within the coarse discretization of a single gyroid block */
2510b7f5c055SJed Brown           /* layer 0 */
2511b7f5c055SJed 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},
2512b7f5c055SJed Brown           /* layer 1 */
2513b7f5c055SJed 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},
2514b7f5c055SJed Brown           /* layer 2 */
2515b7f5c055SJed 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},
2516b7f5c055SJed Brown           /* layer 3 */
2517b7f5c055SJed 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},
2518b7f5c055SJed Brown           /* layer 4 */
2519b7f5c055SJed 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},
2520b7f5c055SJed Brown           /* layer 5 */
2521b7f5c055SJed 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},
2522b7f5c055SJed Brown           /* layer 6 */
2523b7f5c055SJed 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},
2524b7f5c055SJed Brown           /* layer 7 (the top is the periodic image of the bottom of layer 0) */
2525b7f5c055SJed 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}
2526b7f5c055SJed Brown         };
2527b7f5c055SJed Brown       const PetscReal gamma = PetscAcosReal((PetscSqrtReal(3.)-1.) / PetscSqrtReal(2.)) / PETSC_PI;
2528b7f5c055SJed Brown       const PetscReal patternCoords[56][3] =
2529b7f5c055SJed Brown         {
2530b7f5c055SJed Brown           /* A  */ {1.,0.,0.},
2531b7f5c055SJed Brown           /* B  */ {0.,1.,0.},
2532b7f5c055SJed Brown           /* C  */ {gamma,gamma,0.},
2533b7f5c055SJed Brown           /* D  */ {1+gamma,1-gamma,0.},
2534b7f5c055SJed Brown           /* E  */ {2-gamma,2-gamma,0.},
2535b7f5c055SJed Brown           /* F  */ {1-gamma,1+gamma,0.},
2536b7f5c055SJed Brown 
2537b7f5c055SJed Brown           /* G  */ {.5,0,.25},
2538b7f5c055SJed Brown           /* H  */ {1.5,0.,.25},
2539b7f5c055SJed Brown           /* II */ {.5,1.,.25},
2540b7f5c055SJed Brown           /* J  */ {1.5,1.,.25},
2541b7f5c055SJed Brown           /* K  */ {.25,.5,.25},
2542b7f5c055SJed Brown           /* L  */ {1.25,.5,.25},
2543b7f5c055SJed Brown           /* M  */ {.75,1.5,.25},
2544b7f5c055SJed Brown           /* N  */ {1.75,1.5,.25},
2545b7f5c055SJed Brown 
2546b7f5c055SJed Brown           /* O  */ {0.,0.,.5},
2547b7f5c055SJed Brown           /* P  */ {1.,1.,.5},
2548b7f5c055SJed Brown           /* Q  */ {gamma,1-gamma,.5},
2549b7f5c055SJed Brown           /* R  */ {1+gamma,gamma,.5},
2550b7f5c055SJed Brown           /* S  */ {2-gamma,1+gamma,.5},
2551b7f5c055SJed Brown           /* T  */ {1-gamma,2-gamma,.5},
2552b7f5c055SJed Brown 
2553b7f5c055SJed Brown           /* U  */ {0.,.5,.75},
2554b7f5c055SJed Brown           /* V  */ {0.,1.5,.75},
2555b7f5c055SJed Brown           /* W  */ {1.,.5,.75},
2556b7f5c055SJed Brown           /* X  */ {1.,1.5,.75},
2557b7f5c055SJed Brown           /* Y  */ {.5,.75,.75},
2558b7f5c055SJed Brown           /* Z  */ {.5,1.75,.75},
2559b7f5c055SJed Brown           /* Ap */ {1.5,.25,.75},
2560b7f5c055SJed Brown           /* Bp */ {1.5,1.25,.75},
2561b7f5c055SJed Brown 
2562b7f5c055SJed Brown           /* Cp */ {1.,0.,1.},
2563b7f5c055SJed Brown           /* Dp */ {0.,1.,1.},
2564b7f5c055SJed Brown           /* Ep */ {1-gamma,1-gamma,1.},
2565b7f5c055SJed Brown           /* Fp */ {1+gamma,1+gamma,1.},
2566b7f5c055SJed Brown           /* Gp */ {2-gamma,gamma,1.},
2567b7f5c055SJed Brown           /* Hp */ {gamma,2-gamma,1.},
2568b7f5c055SJed Brown 
2569b7f5c055SJed Brown           /* Ip */ {.5,0.,1.25},
2570b7f5c055SJed Brown           /* Jp */ {1.5,0.,1.25},
2571b7f5c055SJed Brown           /* Kp */ {.5,1.,1.25},
2572b7f5c055SJed Brown           /* Lp */ {1.5,1.,1.25},
2573b7f5c055SJed Brown           /* Mp */ {.75,.5,1.25},
2574b7f5c055SJed Brown           /* Np */ {1.75,.5,1.25},
2575b7f5c055SJed Brown           /* Op */ {.25,1.5,1.25},
2576b7f5c055SJed Brown           /* Pp */ {1.25,1.5,1.25},
2577b7f5c055SJed Brown 
2578b7f5c055SJed Brown           /* Qp */ {0.,0.,1.5},
2579b7f5c055SJed Brown           /* Rp */ {1.,1.,1.5},
2580b7f5c055SJed Brown           /* Sp */ {1-gamma,gamma,1.5},
2581b7f5c055SJed Brown           /* Tp */ {2-gamma,1-gamma,1.5},
2582b7f5c055SJed Brown           /* Up */ {1+gamma,2-gamma,1.5},
2583b7f5c055SJed Brown           /* Vp */ {gamma,1+gamma,1.5},
2584b7f5c055SJed Brown 
2585b7f5c055SJed Brown           /* Wp */ {0.,.5,1.75},
2586b7f5c055SJed Brown           /* Xp */ {0.,1.5,1.75},
2587b7f5c055SJed Brown           /* Yp */ {1.,.5,1.75},
2588b7f5c055SJed Brown           /* Zp */ {1.,1.5,1.75},
2589b7f5c055SJed Brown           /* Aq */ {.5,.25,1.75},
2590b7f5c055SJed Brown           /* Bq */ {.5,1.25,1.75},
2591b7f5c055SJed Brown           /* Cq */ {1.5,.75,1.75},
2592b7f5c055SJed Brown           /* Dq */ {1.5,1.75,1.75},
2593b7f5c055SJed Brown         };
2594b7f5c055SJed Brown       PetscInt  (*cells)[64][4] = NULL;
2595b7f5c055SJed Brown       PetscBool *seen;
2596b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
2597b7f5c055SJed Brown       PetscInt  count;
2598b7f5c055SJed Brown 
2599b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i]  = extent[i] + 1;
2600b7f5c055SJed Brown       numBlocks = 1;
2601b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++)     numBlocks *= extent[i];
2602b7f5c055SJed Brown       numBlocksPlus = 1;
2603b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
2604b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
26059566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
26069566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock,&seen));
2607b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
2608b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
2609b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
2610b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
2611b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
2612b7f5c055SJed Brown                 PetscInt vertRaw = pattern[f][v];
2613b7f5c055SJed Brown                 PetscInt blockidx = vertRaw / 56;
2614b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
2615b7f5c055SJed Brown                 PetscInt xplus = (blockidx & 1);
2616b7f5c055SJed Brown                 PetscInt yplus = (blockidx & 2) >> 1;
2617b7f5c055SJed Brown                 PetscInt zplus = (blockidx & 4) >> 2;
2618b7f5c055SJed Brown                 PetscInt zcoord = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
2619b7f5c055SJed Brown                 PetscInt ycoord = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
2620b7f5c055SJed Brown                 PetscInt xcoord = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
2621b7f5c055SJed Brown                 PetscInt vert = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
2622b7f5c055SJed Brown 
2623b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
2624b7f5c055SJed Brown                 seen[vert] = PETSC_TRUE;
2625b7f5c055SJed Brown               }
2626b7f5c055SJed Brown             }
2627b7f5c055SJed Brown           }
2628b7f5c055SJed Brown         }
2629b7f5c055SJed Brown       }
2630b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) if (seen[i]) numVertices++;
2631b7f5c055SJed Brown       count = 0;
26329566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
26339566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
2634b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
2635b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
2636b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
2637b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
2638b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
2639b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
2640b7f5c055SJed Brown 
2641b7f5c055SJed Brown               if (seen[vIdx]) {
2642b7f5c055SJed Brown                 PetscInt thisVert;
2643b7f5c055SJed Brown 
2644b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
2645b7f5c055SJed Brown 
2646b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
2647b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
2648b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
2649b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
2650b7f5c055SJed Brown               }
2651b7f5c055SJed Brown             }
2652b7f5c055SJed Brown           }
2653b7f5c055SJed Brown         }
2654b7f5c055SJed Brown       }
2655b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
2656b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
2657b7f5c055SJed Brown           for (PetscInt v = 0; v < 4; v++) {
2658b7f5c055SJed Brown             cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
2659b7f5c055SJed Brown           }
2660b7f5c055SJed Brown         }
2661b7f5c055SJed Brown       }
26629566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
26639566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
2664b7f5c055SJed Brown       cells_flat = cells[0][0];
2665b7f5c055SJed Brown       numEdges = 0;
2666b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
2667b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
2668b7f5c055SJed Brown           PetscInt ev[] = {cells_flat[i*4 + e], cells_flat[i*4 + ((e+1)%4)]};
2669b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3*ev[0]], &vtxCoords[3*ev[1]]};
2670b7f5c055SJed Brown 
2671b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
2672b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
2673b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
2674b7f5c055SJed Brown               if (evCoords[0][d] == 2.*extent[d] && evCoords[1][d] == 2.*extent[d]) numEdges++;
2675b7f5c055SJed Brown             }
2676b7f5c055SJed Brown           }
2677b7f5c055SJed Brown         }
2678b7f5c055SJed Brown       }
26799566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
26809566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
2681b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
2682b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
2683b7f5c055SJed Brown           PetscInt ev[] = {cells_flat[i*4 + e], cells_flat[i*4 + ((e+1)%4)]};
2684b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3*ev[0]], &vtxCoords[3*ev[1]]};
2685b7f5c055SJed Brown 
2686b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
2687b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
2688b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
2689b7f5c055SJed Brown                 edges[edge][0] = ev[0];
2690b7f5c055SJed Brown                 edges[edge][1] = ev[1];
2691b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
2692b7f5c055SJed Brown               }
2693b7f5c055SJed Brown               if (evCoords[0][d] == 2.*extent[d] && evCoords[1][d] == 2.*extent[d]) {
2694b7f5c055SJed Brown                 edges[edge][0] = ev[0];
2695b7f5c055SJed Brown                 edges[edge][1] = ev[1];
2696b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
2697b7f5c055SJed Brown               }
2698b7f5c055SJed Brown             }
2699b7f5c055SJed Brown           }
2700b7f5c055SJed Brown         }
2701b7f5c055SJed Brown       }
2702b7f5c055SJed Brown     }
2703b7f5c055SJed Brown     evalFunc = TPSEvaluate_Gyroid;
2704b7f5c055SJed Brown     break;
2705b7f5c055SJed Brown   }
2706b7f5c055SJed Brown 
27079566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
27089566063dSJacob Faibussowitsch   if (!rank) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
27099566063dSJacob Faibussowitsch   else       PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
27109566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
2711b7f5c055SJed Brown   {
2712b7f5c055SJed Brown     DM idm;
27139566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
27149566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &idm));
2715b7f5c055SJed Brown   }
27169566063dSJacob Faibussowitsch   if (!rank) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
27179566063dSJacob Faibussowitsch   else       PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
27189566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
2719b7f5c055SJed Brown 
27209566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
27219566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
2722b7f5c055SJed Brown   for (PetscInt e=0; e<numEdges; e++) {
2723b7f5c055SJed Brown     PetscInt njoin;
2724b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
27259566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
2726b7f5c055SJed Brown     PetscCheck(njoin == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expected unique join of vertices %D and %D", edges[e][0], edges[e][1]);
27279566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
27289566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
2729b7f5c055SJed Brown   }
27309566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
27319566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
27321436d7faSJed Brown   if (tps_distribute) {
27331436d7faSJed Brown     DM               pdm = NULL;
27341436d7faSJed Brown     PetscPartitioner part;
27351436d7faSJed Brown 
27369566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
27379566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
27389566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
27391436d7faSJed Brown     if (pdm) {
27409566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &pdm));
27411436d7faSJed Brown     }
27421436d7faSJed Brown     // Do not auto-distribute again
27439566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
27441436d7faSJed Brown   }
2745b7f5c055SJed Brown 
27469566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
2747b7f5c055SJed Brown   for (PetscInt refine=0; refine<refinements; refine++) {
2748b7f5c055SJed Brown     PetscInt m;
2749b7f5c055SJed Brown     DM dmf;
2750b7f5c055SJed Brown     Vec X;
2751b7f5c055SJed Brown     PetscScalar *x;
27529566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
27539566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &dmf));
2754b7f5c055SJed Brown 
27559566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
27569566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
27579566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
2758b7f5c055SJed Brown     for (PetscInt i=0; i<m; i+=3) {
27599566063dSJacob Faibussowitsch       PetscCall(TPSNearestPoint(evalFunc, &x[i]));
2760b7f5c055SJed Brown     }
27619566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
2762b7f5c055SJed Brown   }
2763b7f5c055SJed Brown 
2764b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
27659566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
27669566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
2767b7f5c055SJed Brown 
2768b7f5c055SJed Brown   if (thickness > 0) {
2769b7f5c055SJed Brown     DM dm3;
27709566063dSJacob Faibussowitsch     PetscCall(DMPlexExtrude(dm, layers, thickness, PETSC_FALSE, PETSC_TRUE, NULL, NULL, &dm3));
27719566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &dm3));
2772b7f5c055SJed Brown   }
2773b7f5c055SJed Brown   PetscFunctionReturn(0);
2774b7f5c055SJed Brown }
2775b7f5c055SJed Brown 
2776b7f5c055SJed Brown /*@
2777b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
2778b7f5c055SJed Brown 
2779b7f5c055SJed Brown   Collective
2780b7f5c055SJed Brown 
2781b7f5c055SJed Brown   Input Parameters:
2782b7f5c055SJed Brown + comm   - The communicator for the DM object
2783b7f5c055SJed Brown . tpstype - Type of triply-periodic surface
2784b7f5c055SJed Brown . extent - Array of length 3 containing number of periods in each direction
2785b7f5c055SJed Brown . periodic - array of length 3 with periodicity, or NULL for non-periodic
27861436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
2787817da375SSatish Balay . refinements - Number of factor-of-2 refinements of 2D manifold mesh
27881436d7faSJed Brown . layers - Number of cell layers extruded in normal direction
2789817da375SSatish Balay - thickness - Thickness in normal direction
2790b7f5c055SJed Brown 
2791b7f5c055SJed Brown   Output Parameter:
2792b7f5c055SJed Brown . dm  - The DM object
2793b7f5c055SJed Brown 
2794b7f5c055SJed Brown   Notes:
2795b7f5c055SJed 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.
2796b7f5c055SJed Brown   https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22) and can be cut with "clean" boundaries.
2797b7f5c055SJed 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.
2798b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
2799b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
2800b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
2801b7f5c055SJed Brown 
2802b7f5c055SJed Brown   The face (edge) sets for the Schwarz P surface are numbered 1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z).
2803b7f5c055SJed Brown   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).  Use DMPlexLabelComplete() to propagate to coarse-level vertices.
2804b7f5c055SJed Brown 
2805b7f5c055SJed Brown   References:
2806606c0280SSatish 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
2807b7f5c055SJed Brown 
2808b7f5c055SJed Brown   Developer Notes:
2809b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
2810b7f5c055SJed Brown 
2811b7f5c055SJed Brown   Level: beginner
2812b7f5c055SJed Brown 
2813b7f5c055SJed Brown .seealso: DMPlexCreateSphereMesh(), DMSetType(), DMCreate()
2814b7f5c055SJed Brown @*/
28151436d7faSJed 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)
2816b7f5c055SJed Brown {
2817b7f5c055SJed Brown   PetscFunctionBegin;
28189566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
28199566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
28209566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
2821b7f5c055SJed Brown   PetscFunctionReturn(0);
2822b7f5c055SJed Brown }
2823b7f5c055SJed Brown 
28249318fe57SMatthew G. Knepley /*@
28259318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
28269318fe57SMatthew G. Knepley 
28279318fe57SMatthew G. Knepley   Collective
28289318fe57SMatthew G. Knepley 
28299318fe57SMatthew G. Knepley   Input Parameters:
28309318fe57SMatthew G. Knepley + comm    - The communicator for the DM object
28319318fe57SMatthew G. Knepley . dim     - The dimension
28329318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
28339318fe57SMatthew G. Knepley - R       - The radius
28349318fe57SMatthew G. Knepley 
28359318fe57SMatthew G. Knepley   Output Parameter:
28369318fe57SMatthew G. Knepley . dm  - The DM object
28379318fe57SMatthew G. Knepley 
28389318fe57SMatthew G. Knepley   Level: beginner
28399318fe57SMatthew G. Knepley 
28409318fe57SMatthew G. Knepley .seealso: DMPlexCreateBallMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
28419318fe57SMatthew G. Knepley @*/
28429318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
28439318fe57SMatthew G. Knepley {
28449318fe57SMatthew G. Knepley   PetscFunctionBegin;
28459318fe57SMatthew G. Knepley   PetscValidPointer(dm, 5);
28469566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
28479566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
28489566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
28499318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
28509318fe57SMatthew G. Knepley }
28519318fe57SMatthew G. Knepley 
28529318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
28539318fe57SMatthew G. Knepley {
28549318fe57SMatthew G. Knepley   DM             sdm, vol;
28559318fe57SMatthew G. Knepley   DMLabel        bdlabel;
28569318fe57SMatthew G. Knepley 
28579318fe57SMatthew G. Knepley   PetscFunctionBegin;
28589566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), &sdm));
28599566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
28609566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject) sdm, "bd_"));
28619566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim-1, PETSC_TRUE, R));
28629566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
28639566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
28649566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
28659566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
28669566063dSJacob Faibussowitsch   PetscCall(DMPlexReplace_Static(dm, &vol));
28679566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
28689566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
28699566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
28709566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
287151a74b61SMatthew G. Knepley   PetscFunctionReturn(0);
287251a74b61SMatthew G. Knepley }
287351a74b61SMatthew G. Knepley 
287451a74b61SMatthew G. Knepley /*@
287551a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
287651a74b61SMatthew G. Knepley 
287751a74b61SMatthew G. Knepley   Collective
287851a74b61SMatthew G. Knepley 
287951a74b61SMatthew G. Knepley   Input Parameters:
288051a74b61SMatthew G. Knepley + comm  - The communicator for the DM object
288151a74b61SMatthew G. Knepley . dim   - The dimension
288251a74b61SMatthew G. Knepley - R     - The radius
288351a74b61SMatthew G. Knepley 
288451a74b61SMatthew G. Knepley   Output Parameter:
288551a74b61SMatthew G. Knepley . dm  - The DM object
288651a74b61SMatthew G. Knepley 
288751a74b61SMatthew G. Knepley   Options Database Keys:
288851a74b61SMatthew G. Knepley - bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
288951a74b61SMatthew G. Knepley 
289051a74b61SMatthew G. Knepley   Level: beginner
289151a74b61SMatthew G. Knepley 
289251a74b61SMatthew G. Knepley .seealso: DMPlexCreateSphereMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
289351a74b61SMatthew G. Knepley @*/
289451a74b61SMatthew G. Knepley PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
289551a74b61SMatthew G. Knepley {
289651a74b61SMatthew G. Knepley   PetscFunctionBegin;
28979566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
28989566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
28999566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
29002829fed8SMatthew G. Knepley   PetscFunctionReturn(0);
29012829fed8SMatthew G. Knepley }
29022829fed8SMatthew G. Knepley 
29039318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
29040a6ba040SMatthew G. Knepley {
29050a6ba040SMatthew G. Knepley   PetscFunctionBegin;
29069318fe57SMatthew G. Knepley   switch (ct) {
29079318fe57SMatthew G. Knepley     case DM_POLYTOPE_POINT:
29089318fe57SMatthew G. Knepley     {
29099318fe57SMatthew G. Knepley       PetscInt    numPoints[1]        = {1};
29109318fe57SMatthew G. Knepley       PetscInt    coneSize[1]         = {0};
29119318fe57SMatthew G. Knepley       PetscInt    cones[1]            = {0};
29129318fe57SMatthew G. Knepley       PetscInt    coneOrientations[1] = {0};
29139318fe57SMatthew G. Knepley       PetscScalar vertexCoords[1]     = {0.0};
29149318fe57SMatthew G. Knepley 
29159566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 0));
29169566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
29179318fe57SMatthew G. Knepley     }
29189318fe57SMatthew G. Knepley     break;
29199318fe57SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
29209318fe57SMatthew G. Knepley     {
29219318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {2, 1};
29229318fe57SMatthew G. Knepley       PetscInt    coneSize[3]         = {2, 0, 0};
29239318fe57SMatthew G. Knepley       PetscInt    cones[2]            = {1, 2};
29249318fe57SMatthew G. Knepley       PetscInt    coneOrientations[2] = {0, 0};
29259318fe57SMatthew G. Knepley       PetscScalar vertexCoords[2]     = {-1.0,  1.0};
29269318fe57SMatthew G. Knepley 
29279566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 1));
29289566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
29299318fe57SMatthew G. Knepley     }
29309318fe57SMatthew G. Knepley     break;
2931b5a892a1SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
2932b5a892a1SMatthew G. Knepley     {
2933b5a892a1SMatthew G. Knepley       PetscInt    numPoints[2]        = {2, 1};
2934b5a892a1SMatthew G. Knepley       PetscInt    coneSize[3]         = {2, 0, 0};
2935b5a892a1SMatthew G. Knepley       PetscInt    cones[2]            = {1, 2};
2936b5a892a1SMatthew G. Knepley       PetscInt    coneOrientations[2] = {0, 0};
2937b5a892a1SMatthew G. Knepley       PetscScalar vertexCoords[2]     = {-1.0,  1.0};
2938b5a892a1SMatthew G. Knepley 
29399566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 1));
29409566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2941b5a892a1SMatthew G. Knepley     }
2942b5a892a1SMatthew G. Knepley     break;
29439318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
29449318fe57SMatthew G. Knepley     {
29459318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {3, 1};
29469318fe57SMatthew G. Knepley       PetscInt    coneSize[4]         = {3, 0, 0, 0};
29479318fe57SMatthew G. Knepley       PetscInt    cones[3]            = {1, 2, 3};
29489318fe57SMatthew G. Knepley       PetscInt    coneOrientations[3] = {0, 0, 0};
29499318fe57SMatthew G. Knepley       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};
29509318fe57SMatthew G. Knepley 
29519566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 2));
29529566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
29539318fe57SMatthew G. Knepley     }
29549318fe57SMatthew G. Knepley     break;
29559318fe57SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
29569318fe57SMatthew G. Knepley     {
29579318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
29589318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
29599318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
29609318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
29619318fe57SMatthew G. Knepley       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};
29629318fe57SMatthew G. Knepley 
29639566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 2));
29649566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
29659318fe57SMatthew G. Knepley     }
29669318fe57SMatthew G. Knepley     break;
29679318fe57SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
29689318fe57SMatthew G. Knepley     {
29699318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
29709318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
29719318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
29729318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
29739318fe57SMatthew G. Knepley       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0,  1.0, 1.0};
29749318fe57SMatthew G. Knepley 
29759566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 2));
29769566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
29779318fe57SMatthew G. Knepley     }
29789318fe57SMatthew G. Knepley     break;
29799318fe57SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
29809318fe57SMatthew G. Knepley     {
29819318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
29829318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
2983f0edb160SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
29849318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
2985f0edb160SMatthew 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};
29869318fe57SMatthew G. Knepley 
29879566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
29889566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
29899318fe57SMatthew G. Knepley     }
29909318fe57SMatthew G. Knepley     break;
29919318fe57SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
29929318fe57SMatthew G. Knepley     {
29939318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {8, 1};
29949318fe57SMatthew G. Knepley       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
2995f0edb160SMatthew G. Knepley       PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
29969318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2997f0edb160SMatthew 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,
29989318fe57SMatthew 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};
29999318fe57SMatthew G. Knepley 
30009566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
30019566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30029318fe57SMatthew G. Knepley     }
30039318fe57SMatthew G. Knepley     break;
30049318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM:
30059318fe57SMatthew G. Knepley     {
30069318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {6, 1};
30079318fe57SMatthew G. Knepley       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
3008f0edb160SMatthew G. Knepley       PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
30099318fe57SMatthew G. Knepley       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
3010f0edb160SMatthew G. Knepley       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0, -1.0,  1.0, -1.0,   1.0, -1.0, -1.0,
30119318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0,  1.0,  1.0};
30129318fe57SMatthew G. Knepley 
30139566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
30149566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30159318fe57SMatthew G. Knepley     }
30169318fe57SMatthew G. Knepley     break;
30179318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
30189318fe57SMatthew G. Knepley     {
30199318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {6, 1};
30209318fe57SMatthew G. Knepley       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
30219318fe57SMatthew G. Knepley       PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
30229318fe57SMatthew G. Knepley       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
30239318fe57SMatthew G. Knepley       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  -1.0, 1.0, -1.0,
30249318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0, 1.0,  1.0};
30259318fe57SMatthew G. Knepley 
30269566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
30279566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30289318fe57SMatthew G. Knepley     }
30299318fe57SMatthew G. Knepley     break;
30309318fe57SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
30319318fe57SMatthew G. Knepley     {
30329318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {8, 1};
30339318fe57SMatthew G. Knepley       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
30349318fe57SMatthew G. Knepley       PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
30359318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
30369318fe57SMatthew 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,
30379318fe57SMatthew 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};
30389318fe57SMatthew G. Knepley 
30399566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
30409566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30419318fe57SMatthew G. Knepley     }
30429318fe57SMatthew G. Knepley     break;
30439318fe57SMatthew G. Knepley     case DM_POLYTOPE_PYRAMID:
30449318fe57SMatthew G. Knepley     {
30459318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {5, 1};
30469318fe57SMatthew G. Knepley       PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
3047f0edb160SMatthew G. Knepley       PetscInt    cones[5]            = {1, 2, 3, 4, 5};
30489318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3049f0edb160SMatthew 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,
30509318fe57SMatthew G. Knepley                                           0.0,  0.0,  1.0};
30519318fe57SMatthew G. Knepley 
30529566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
30539566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30549318fe57SMatthew G. Knepley     }
30559318fe57SMatthew G. Knepley     break;
305698921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
30579318fe57SMatthew G. Knepley   }
30589318fe57SMatthew G. Knepley   {
30599318fe57SMatthew G. Knepley     PetscInt Nv, v;
30609318fe57SMatthew G. Knepley 
30619318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
30629566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
30639566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
30649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
30659566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
30669318fe57SMatthew G. Knepley   }
30679566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
30689566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) rdm, DMPolytopeTypes[ct]));
30690a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
30700a6ba040SMatthew G. Knepley }
30710a6ba040SMatthew G. Knepley 
30729318fe57SMatthew G. Knepley /*@
30739318fe57SMatthew G. Knepley   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell
30749318fe57SMatthew G. Knepley 
30759318fe57SMatthew G. Knepley   Collective
30769318fe57SMatthew G. Knepley 
30779318fe57SMatthew G. Knepley   Input Parameters:
30789318fe57SMatthew G. Knepley + comm - The communicator
30799318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
30809318fe57SMatthew G. Knepley 
30819318fe57SMatthew G. Knepley   Output Parameter:
30829318fe57SMatthew G. Knepley . refdm - The reference cell
30839318fe57SMatthew G. Knepley 
30849318fe57SMatthew G. Knepley   Level: intermediate
30859318fe57SMatthew G. Knepley 
30869318fe57SMatthew G. Knepley .seealso: DMPlexCreateReferenceCell(), DMPlexCreateBoxMesh()
30879318fe57SMatthew G. Knepley @*/
30889318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
30890a6ba040SMatthew G. Knepley {
30900a6ba040SMatthew G. Knepley   PetscFunctionBegin;
30919566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
30929566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
30939566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
30949318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
30959318fe57SMatthew G. Knepley }
309679a015ccSMatthew G. Knepley 
30979318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
30989318fe57SMatthew G. Knepley {
30999318fe57SMatthew G. Knepley   DM             plex;
31009318fe57SMatthew G. Knepley   DMLabel        label;
31019318fe57SMatthew G. Knepley   PetscBool      hasLabel;
31020a6ba040SMatthew G. Knepley 
31039318fe57SMatthew G. Knepley   PetscFunctionBeginUser;
31049566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
31059318fe57SMatthew G. Knepley   if (hasLabel) PetscFunctionReturn(0);
31069566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
31079566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
31089566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
31099566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
31109566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
31119318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
31129318fe57SMatthew G. Knepley }
3113acdc6f61SToby Isaac 
3114*05bd46c0SStefano Zampini const char * const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
31159318fe57SMatthew G. Knepley 
311661a622f3SMatthew G. Knepley static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
31179318fe57SMatthew G. Knepley {
31189318fe57SMatthew G. Knepley   DMPlexShape    shape = DM_SHAPE_BOX;
31199318fe57SMatthew G. Knepley   DMPolytopeType cell  = DM_POLYTOPE_TRIANGLE;
31209318fe57SMatthew G. Knepley   PetscInt       dim   = 2;
31219318fe57SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
3122cd7e8a5eSksagiyam   PetscBool      flg, flg2, fflg, bdfflg, nameflg;
31239318fe57SMatthew G. Knepley   MPI_Comm       comm;
3124ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
3125ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
3126ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
31279318fe57SMatthew G. Knepley 
31289318fe57SMatthew G. Knepley   PetscFunctionBegin;
31299566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
31309318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
31319566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
31329566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
31339566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
31349566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum) cell, (PetscEnum *) &cell, NULL));
31359566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
31369566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum) shape, (PetscEnum *) &shape, &flg));
31379566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
31382c71b3e2SJacob Faibussowitsch   PetscCheckFalse((dim < 0) || (dim > 3),comm, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D should be in [1, 3]", dim);
31399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex,  &simplex, &flg));
31409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
31419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone,  &adjCone, &flg));
31429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure,  &adjClosure, &flg2));
31439566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
31449318fe57SMatthew G. Knepley 
314561a622f3SMatthew G. Knepley   switch (cell) {
314661a622f3SMatthew G. Knepley     case DM_POLYTOPE_POINT:
314761a622f3SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
314861a622f3SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
314961a622f3SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
315061a622f3SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
315161a622f3SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
315261a622f3SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
315361a622f3SMatthew G. Knepley       *useCoordSpace = PETSC_TRUE;break;
315461a622f3SMatthew G. Knepley     default: *useCoordSpace = PETSC_FALSE;break;
315561a622f3SMatthew G. Knepley   }
315661a622f3SMatthew G. Knepley 
31579318fe57SMatthew G. Knepley   if (fflg) {
31589318fe57SMatthew G. Knepley     DM dmnew;
31599318fe57SMatthew G. Knepley 
31609566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), filename, plexname, interpolate, &dmnew));
31619566063dSJacob Faibussowitsch     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, dmnew));
31629566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &dmnew));
31639318fe57SMatthew G. Knepley   } else if (refDomain) {
31649566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
31659318fe57SMatthew G. Knepley   } else if (bdfflg) {
31669318fe57SMatthew G. Knepley     DM bdm, dmnew;
31679318fe57SMatthew G. Knepley 
31689566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), bdFilename, plexname, interpolate, &bdm));
31699566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) bdm, "bd_"));
31709566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
31719566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
31729566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
31739566063dSJacob Faibussowitsch     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, dmnew));
31749566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &dmnew));
31759318fe57SMatthew G. Knepley   } else {
31769566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) dm, DMPlexShapes[shape]));
31779318fe57SMatthew G. Knepley     switch (shape) {
31789318fe57SMatthew G. Knepley       case DM_SHAPE_BOX:
31799318fe57SMatthew G. Knepley       {
31809318fe57SMatthew G. Knepley         PetscInt       faces[3] = {0, 0, 0};
31819318fe57SMatthew G. Knepley         PetscReal      lower[3] = {0, 0, 0};
31829318fe57SMatthew G. Knepley         PetscReal      upper[3] = {1, 1, 1};
31839318fe57SMatthew G. Knepley         DMBoundaryType bdt[3]   = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
31849318fe57SMatthew G. Knepley         PetscInt       i, n;
31859318fe57SMatthew G. Knepley 
31869318fe57SMatthew G. Knepley         n    = dim;
31879318fe57SMatthew G. Knepley         for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4-dim);
31889566063dSJacob Faibussowitsch         PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
31899318fe57SMatthew G. Knepley         n    = 3;
31909566063dSJacob Faibussowitsch         PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
31912c71b3e2SJacob Faibussowitsch         PetscCheckFalse(flg && (n != dim),comm, PETSC_ERR_ARG_SIZ, "Lower box point had %D values, should have been %D", n, dim);
31929318fe57SMatthew G. Knepley         n    = 3;
31939566063dSJacob Faibussowitsch         PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
31942c71b3e2SJacob Faibussowitsch         PetscCheckFalse(flg && (n != dim),comm, PETSC_ERR_ARG_SIZ, "Upper box point had %D values, should have been %D", n, dim);
31959318fe57SMatthew G. Knepley         n    = 3;
31969566063dSJacob Faibussowitsch         PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *) bdt, &n, &flg));
31972c71b3e2SJacob Faibussowitsch         PetscCheckFalse(flg && (n != dim),comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %D values, should have been %D", n, dim);
31989318fe57SMatthew G. Knepley         switch (cell) {
319961a622f3SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
32009566063dSJacob Faibussowitsch             PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
3201d410b0cfSMatthew G. Knepley             if (!interpolate) {
3202d410b0cfSMatthew G. Knepley               DM udm;
3203d410b0cfSMatthew G. Knepley 
32049566063dSJacob Faibussowitsch               PetscCall(DMPlexUninterpolate(dm, &udm));
32059566063dSJacob Faibussowitsch               PetscCall(DMPlexReplace_Static(dm, &udm));
3206d410b0cfSMatthew G. Knepley             }
32079318fe57SMatthew G. Knepley             break;
32089318fe57SMatthew G. Knepley           default:
32099566063dSJacob Faibussowitsch             PetscCall(DMPlexCreateBoxMesh_Internal(dm, dim, simplex, faces, lower, upper, bdt, interpolate));
32109318fe57SMatthew G. Knepley             break;
32119318fe57SMatthew G. Knepley         }
32129318fe57SMatthew G. Knepley       }
32139318fe57SMatthew G. Knepley       break;
32149318fe57SMatthew G. Knepley       case DM_SHAPE_BOX_SURFACE:
32159318fe57SMatthew G. Knepley       {
32169318fe57SMatthew G. Knepley         PetscInt  faces[3] = {0, 0, 0};
32179318fe57SMatthew G. Knepley         PetscReal lower[3] = {0, 0, 0};
32189318fe57SMatthew G. Knepley         PetscReal upper[3] = {1, 1, 1};
32199318fe57SMatthew G. Knepley         PetscInt  i, n;
32209318fe57SMatthew G. Knepley 
32219318fe57SMatthew G. Knepley         n    = dim+1;
32229318fe57SMatthew G. Knepley         for (i = 0; i < dim+1; ++i) faces[i] = (dim+1 == 1 ? 1 : 4-(dim+1));
32239566063dSJacob Faibussowitsch         PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
32249318fe57SMatthew G. Knepley         n    = 3;
32259566063dSJacob Faibussowitsch         PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
32262c71b3e2SJacob Faibussowitsch         PetscCheckFalse(flg && (n != dim+1),comm, PETSC_ERR_ARG_SIZ, "Lower box point had %D values, should have been %D", n, dim+1);
32279318fe57SMatthew G. Knepley         n    = 3;
32289566063dSJacob Faibussowitsch         PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
32292c71b3e2SJacob Faibussowitsch         PetscCheckFalse(flg && (n != dim+1),comm, PETSC_ERR_ARG_SIZ, "Upper box point had %D values, should have been %D", n, dim+1);
32309566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim+1, faces, lower, upper, interpolate));
32319318fe57SMatthew G. Knepley       }
32329318fe57SMatthew G. Knepley       break;
32339318fe57SMatthew G. Knepley       case DM_SHAPE_SPHERE:
32349318fe57SMatthew G. Knepley       {
32359318fe57SMatthew G. Knepley         PetscReal R = 1.0;
32369318fe57SMatthew G. Knepley 
32379566063dSJacob Faibussowitsch         PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R,  &R, &flg));
32389566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
32399318fe57SMatthew G. Knepley       }
32409318fe57SMatthew G. Knepley       break;
32419318fe57SMatthew G. Knepley       case DM_SHAPE_BALL:
32429318fe57SMatthew G. Knepley       {
32439318fe57SMatthew G. Knepley         PetscReal R = 1.0;
32449318fe57SMatthew G. Knepley 
32459566063dSJacob Faibussowitsch         PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R,  &R, &flg));
32469566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
32479318fe57SMatthew G. Knepley       }
32489318fe57SMatthew G. Knepley       break;
32499318fe57SMatthew G. Knepley       case DM_SHAPE_CYLINDER:
32509318fe57SMatthew G. Knepley       {
32519318fe57SMatthew G. Knepley         DMBoundaryType bdt = DM_BOUNDARY_NONE;
32529318fe57SMatthew G. Knepley         PetscInt       Nw  = 6;
32539318fe57SMatthew G. Knepley 
32549566063dSJacob Faibussowitsch         PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum) bdt, (PetscEnum *) &bdt, NULL));
32559566063dSJacob Faibussowitsch         PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
32569318fe57SMatthew G. Knepley         switch (cell) {
325761a622f3SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
32589566063dSJacob Faibussowitsch             PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
32599318fe57SMatthew G. Knepley             break;
32609318fe57SMatthew G. Knepley           default:
32619566063dSJacob Faibussowitsch             PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt));
32629318fe57SMatthew G. Knepley             break;
32639318fe57SMatthew G. Knepley         }
32649318fe57SMatthew G. Knepley       }
32659318fe57SMatthew G. Knepley       break;
3266b7f5c055SJed Brown       case DM_SHAPE_SCHWARZ_P: // fallthrough
3267b7f5c055SJed Brown       case DM_SHAPE_GYROID:
3268b7f5c055SJed Brown       {
3269b7f5c055SJed Brown         PetscInt       extent[3] = {1,1,1}, refine = 0, layers = 0, three;
3270b7f5c055SJed Brown         PetscReal      thickness = 0.;
3271b7f5c055SJed Brown         DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
3272b7f5c055SJed Brown         DMPlexTPSType  tps_type = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
32731436d7faSJed Brown         PetscBool      tps_distribute;
32749566063dSJacob Faibussowitsch         PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three=3, &three), NULL));
32759566063dSJacob Faibussowitsch         PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
32769566063dSJacob Faibussowitsch         PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum*)periodic, (three=3, &three), NULL));
32779566063dSJacob Faibussowitsch         PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
32789566063dSJacob Faibussowitsch         PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
32799566063dSJacob Faibussowitsch         PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
32809566063dSJacob Faibussowitsch         PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
32819566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
3282b7f5c055SJed Brown       }
3283b7f5c055SJed Brown       break;
3284*05bd46c0SStefano Zampini       case DM_SHAPE_DOUBLET:
3285*05bd46c0SStefano Zampini       {
3286*05bd46c0SStefano Zampini         DM        dmnew;
3287*05bd46c0SStefano Zampini         PetscReal rl = 0.0;
3288*05bd46c0SStefano Zampini 
3289*05bd46c0SStefano Zampini         PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
3290*05bd46c0SStefano Zampini         PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
3291*05bd46c0SStefano Zampini         PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, dmnew));
3292*05bd46c0SStefano Zampini         PetscCall(DMPlexReplace_Static(dm, &dmnew));
3293*05bd46c0SStefano Zampini       }
3294*05bd46c0SStefano Zampini       break;
329598921bdaSJacob Faibussowitsch       default: SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
32969318fe57SMatthew G. Knepley     }
32979318fe57SMatthew G. Knepley   }
32989566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
3299ed5e4e85SVaclav Hapla   if (!((PetscObject)dm)->name && nameflg) {
33009566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
3301ed5e4e85SVaclav Hapla   }
33020a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
33030a6ba040SMatthew G. Knepley }
33040a6ba040SMatthew G. Knepley 
330547920aaeSMatthew G. Knepley PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject, DM dm)
33060a6ba040SMatthew G. Knepley {
33070a6ba040SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
3308c0f0dcc3SMatthew G. Knepley   PetscBool      flg;
33099318fe57SMatthew G. Knepley   char           bdLabel[PETSC_MAX_PATH_LEN];
33100a6ba040SMatthew G. Knepley 
33110a6ba040SMatthew G. Knepley   PetscFunctionBegin;
33120a6ba040SMatthew G. Knepley   /* Handle viewing */
33139566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
33149566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL,0));
33159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
33169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL,0));
33179566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
33189566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
33199318fe57SMatthew G. Knepley   /* Labeling */
33209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
33219566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
3322953fc75cSMatthew G. Knepley   /* Point Location */
33239566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
33240848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
33259566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
33262e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
33279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
3328b29cfa1cSToby Isaac   /* Projection behavior */
33299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL,0));
33309566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
3331f12cf164SMatthew G. Knepley   /* Checking structure */
3332f12cf164SMatthew G. Knepley   {
3333e902f1eaSVaclav Hapla     PetscBool   flg = PETSC_FALSE, flg2 = PETSC_FALSE, all = PETSC_FALSE;
3334f12cf164SMatthew G. Knepley 
33359566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all checks", NULL, PETSC_FALSE, &all, &flg2));
33369566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
33379566063dSJacob Faibussowitsch     if (all || (flg && flg2)) PetscCall(DMPlexCheckSymmetry(dm));
33389566063dSJacob 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));
33399566063dSJacob Faibussowitsch     if (all || (flg && flg2)) PetscCall(DMPlexCheckSkeleton(dm, 0));
33409566063dSJacob 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));
33419566063dSJacob Faibussowitsch     if (all || (flg && flg2)) PetscCall(DMPlexCheckFaces(dm, 0));
33429566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
33439566063dSJacob Faibussowitsch     if (all || (flg && flg2)) PetscCall(DMPlexCheckGeometry(dm));
33449566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
33459566063dSJacob Faibussowitsch     if (all || (flg && flg2)) PetscCall(DMPlexCheckPointSF(dm));
33469566063dSJacob 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));
33479566063dSJacob Faibussowitsch     if (all || (flg && flg2)) PetscCall(DMPlexCheckInterfaceCones(dm));
33489566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
33499566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
3350f12cf164SMatthew G. Knepley   }
33519318fe57SMatthew G. Knepley   {
33529318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
33534f3833eaSMatthew G. Knepley 
33549566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
33559318fe57SMatthew G. Knepley     if (flg) {
33569318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
33579318fe57SMatthew G. Knepley 
33589566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
33599566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
33609566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
33619566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
33629318fe57SMatthew G. Knepley     }
33639318fe57SMatthew G. Knepley   }
33649566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
336568d4fef7SMatthew G. Knepley   PetscFunctionReturn(0);
336668d4fef7SMatthew G. Knepley }
336768d4fef7SMatthew G. Knepley 
336846fa42a0SMatthew G. Knepley static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
336968d4fef7SMatthew G. Knepley {
3370bdf63967SMatthew G. Knepley   PetscFunctionList ordlist;
3371bdf63967SMatthew G. Knepley   char              oname[256];
3372d410b0cfSMatthew G. Knepley   PetscReal         volume = -1.0;
33739318fe57SMatthew G. Knepley   PetscInt          prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
3374e600fa54SMatthew 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;
337568d4fef7SMatthew G. Knepley 
337668d4fef7SMatthew G. Knepley   PetscFunctionBegin;
3377064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
33789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsHead(PetscOptionsObject,"DMPlex Options"));
33799318fe57SMatthew G. Knepley   /* Handle automatic creation */
33809566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
33819566063dSJacob Faibussowitsch   if (dim < 0) {PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));created = PETSC_TRUE;}
3382d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
33839566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
3384d89e6e46SMatthew G. Knepley   if (flg) {
3385d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
3386d89e6e46SMatthew G. Knepley 
33879566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
3388d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
3389d89e6e46SMatthew G. Knepley       DM udm;
3390d89e6e46SMatthew G. Knepley 
33919566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
33929566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &udm));
3393d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
3394d89e6e46SMatthew G. Knepley       DM idm;
3395d89e6e46SMatthew G. Knepley 
33969566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
33979566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &idm));
3398d89e6e46SMatthew G. Knepley     }
3399d89e6e46SMatthew G. Knepley   }
34009b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
34019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_ignore_model", "Flag to ignore the geometry model when refining", "DMCreate", ignoreModel, &ignoreModel, &flg));
3402c1cad2e7SMatthew G. Knepley   if (flg) {((DM_Plex *) dm->data)->ignoreModel = ignoreModel;}
34039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
34049566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL,0));
34059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
34069566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
34079566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
34089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
34099318fe57SMatthew G. Knepley   if (flg) {
34109566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
34119566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
34129318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
34139318fe57SMatthew G. Knepley   }
34149b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
34159b44eab4SMatthew G. Knepley     DM             rdm;
34169b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
34179b44eab4SMatthew G. Knepley 
34189566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
34199566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm));
34209566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &rdm));
34219566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
342261a622f3SMatthew G. Knepley     if (coordFunc && remap) {
34239566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
34249b44eab4SMatthew G. Knepley       ((DM_Plex*) dm->data)->coordFunc = coordFunc;
34259b44eab4SMatthew G. Knepley     }
34269b44eab4SMatthew G. Knepley   }
34279566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
34289318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
34299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
34309318fe57SMatthew G. Knepley   if (extLayers) {
34319318fe57SMatthew G. Knepley     DM edm;
34329318fe57SMatthew G. Knepley 
34339566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
34349566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &edm));
343548d16a33SMatthew G. Knepley     ((DM_Plex *) dm->data)->coordFunc = NULL;
34369566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
3437d410b0cfSMatthew G. Knepley     extLayers = 0;
34389318fe57SMatthew G. Knepley   }
3439bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
34409566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
34419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
3442bdf63967SMatthew G. Knepley   if (flg) {
3443bdf63967SMatthew G. Knepley     DM pdm;
3444bdf63967SMatthew G. Knepley     IS perm;
3445bdf63967SMatthew G. Knepley 
34469566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
34479566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
34489566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
34499566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &pdm));
34509566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
3451bdf63967SMatthew G. Knepley   }
34529b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
34539566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
34549566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMCreate", distribute, &distribute, NULL));
34559566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMCreate", overlap, &overlap, NULL, 0));
34569b44eab4SMatthew G. Knepley   if (distribute) {
34579b44eab4SMatthew G. Knepley     DM               pdm = NULL;
34589b44eab4SMatthew G. Knepley     PetscPartitioner part;
34599b44eab4SMatthew G. Knepley 
34609566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
34619566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
34629566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, overlap, NULL, &pdm));
34639b44eab4SMatthew G. Knepley     if (pdm) {
34649566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &pdm));
34659b44eab4SMatthew G. Knepley     }
34669b44eab4SMatthew G. Knepley   }
34679318fe57SMatthew G. Knepley   /* Create coordinate space */
34689318fe57SMatthew G. Knepley   if (created) {
346961a622f3SMatthew G. Knepley     DM_Plex  *mesh = (DM_Plex *) dm->data;
34709318fe57SMatthew G. Knepley     PetscInt  degree = 1;
347161a622f3SMatthew G. Knepley     PetscBool periodic, flg;
34729318fe57SMatthew G. Knepley 
34739566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg));
34749566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
34759566063dSJacob Faibussowitsch     if (coordSpace) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, mesh->coordFunc));
347661a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
347761a622f3SMatthew G. Knepley       DM           cdm;
347861a622f3SMatthew G. Knepley       PetscDS      cds;
347961a622f3SMatthew G. Knepley       PetscObject  obj;
348061a622f3SMatthew G. Knepley       PetscClassId id;
348161a622f3SMatthew G. Knepley 
34829566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateDM(dm, &cdm));
34839566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
34849566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
34859566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
348661a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
348761a622f3SMatthew G. Knepley         PetscContainer dummy;
348861a622f3SMatthew G. Knepley 
34899566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
34909566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject) dummy, "coordinates"));
34919566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject) dummy));
34929566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
34939566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
349461a622f3SMatthew G. Knepley       }
349561a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
349661a622f3SMatthew G. Knepley     }
34979566063dSJacob Faibussowitsch     PetscCall(DMLocalizeCoordinates(dm));
34989566063dSJacob Faibussowitsch     PetscCall(DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL));
34999566063dSJacob Faibussowitsch     if (periodic) PetscCall(DMSetPeriodicity(dm, PETSC_TRUE, NULL, NULL, NULL));
35009318fe57SMatthew G. Knepley   }
350168d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
350261a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
35039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL,0));
35049566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
35059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy,0));
35069566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
350768d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
3508acdc6f61SToby Isaac     DM *dms, coarseDM;
350968d4fef7SMatthew G. Knepley 
35109566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
35119566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
35129566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine,&dms));
35139566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
351468d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
35159566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine-1]));
351668d4fef7SMatthew G. Knepley     if (refine == 1) {
35179566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
35189566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
351968d4fef7SMatthew G. Knepley     } else {
35209566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine-2]));
35219566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
35229566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine-1]));
35239566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
352468d4fef7SMatthew G. Knepley     }
35259566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine-1], coarseDM));
35269566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
352768d4fef7SMatthew G. Knepley     /* Free DMs */
352868d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
35299566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]));
35309566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
353168d4fef7SMatthew G. Knepley     }
35329566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
353368d4fef7SMatthew G. Knepley   } else {
353468d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
35359318fe57SMatthew G. Knepley       DM             rdm;
353651a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
353768d4fef7SMatthew G. Knepley 
35389566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
35399566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm));
354068d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
35419566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &rdm));
35429566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
354361a622f3SMatthew G. Knepley       if (coordFunc && remap) {
35449566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
354551a74b61SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
354651a74b61SMatthew G. Knepley       }
354768d4fef7SMatthew G. Knepley     }
354868d4fef7SMatthew G. Knepley   }
35493cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
35509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL,0));
35519566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy,0));
3552b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
3553b653a561SMatthew G. Knepley     DM *dms;
3554b653a561SMatthew G. Knepley 
35559566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
35569566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
3557b653a561SMatthew G. Knepley     /* Free DMs */
3558b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
35599566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]));
35609566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
3561b653a561SMatthew G. Knepley     }
35629566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
3563b653a561SMatthew G. Knepley   } else {
3564b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
35659318fe57SMatthew G. Knepley       DM             cdm;
35669318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
35673cf6fe12SMatthew G. Knepley 
35689566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
35699566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &cdm));
35703cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
35719566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &cdm));
35729566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
35739318fe57SMatthew G. Knepley       if (coordFunc) {
35749566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
35759318fe57SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
35769318fe57SMatthew G. Knepley       }
35773cf6fe12SMatthew G. Knepley     }
3578b653a561SMatthew G. Knepley   }
3579909dfd52SMatthew G. Knepley   /* Handle ghost cells */
35809566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
3581909dfd52SMatthew G. Knepley   if (ghostCells) {
3582909dfd52SMatthew G. Knepley     DM   gdm;
3583909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
3584909dfd52SMatthew G. Knepley 
3585909dfd52SMatthew G. Knepley     lname[0] = '\0';
35869566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
35879566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
35889566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &gdm));
3589909dfd52SMatthew G. Knepley   }
35906913077dSMatthew G. Knepley   /* Handle 1D order */
35916913077dSMatthew G. Knepley   {
35926913077dSMatthew G. Knepley     DM           cdm, rdm;
35936913077dSMatthew G. Knepley     PetscDS      cds;
35946913077dSMatthew G. Knepley     PetscObject  obj;
35956913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
35966913077dSMatthew G. Knepley     IS           perm;
35976913077dSMatthew G. Knepley     PetscInt     dim, Nf;
35986913077dSMatthew G. Knepley     PetscBool    distributed;
35996913077dSMatthew G. Knepley 
36009566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &dim));
36019566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
36029566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
36039566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
36049566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
36056913077dSMatthew G. Knepley     if (Nf) {
36069566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
36079566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
36086913077dSMatthew G. Knepley     }
36096913077dSMatthew G. Knepley     if (dim == 1 && !distributed && id != PETSCFE_CLASSID) {
36109566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
36119566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
36129566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &rdm));
36139566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
36146913077dSMatthew G. Knepley     }
36156913077dSMatthew G. Knepley   }
36163cf6fe12SMatthew G. Knepley   /* Handle */
36179566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
36189566063dSJacob Faibussowitsch   PetscCall(PetscOptionsTail());
36190a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
36200a6ba040SMatthew G. Knepley }
36210a6ba040SMatthew G. Knepley 
3622552f7358SJed Brown static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
3623552f7358SJed Brown {
3624552f7358SJed Brown   PetscFunctionBegin;
36259566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm,vec));
36269566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
36279566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex));
36289566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native));
36299566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex));
36309566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native));
3631552f7358SJed Brown   PetscFunctionReturn(0);
3632552f7358SJed Brown }
3633552f7358SJed Brown 
3634552f7358SJed Brown static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
3635552f7358SJed Brown {
3636552f7358SJed Brown   PetscFunctionBegin;
36379566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm,vec));
36389566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local));
36399566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local));
3640552f7358SJed Brown   PetscFunctionReturn(0);
3641552f7358SJed Brown }
3642552f7358SJed Brown 
3643793f3fe5SMatthew G. Knepley static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3644793f3fe5SMatthew G. Knepley {
3645793f3fe5SMatthew G. Knepley   PetscInt       depth, d;
3646793f3fe5SMatthew G. Knepley 
3647793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
36489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
3649793f3fe5SMatthew G. Knepley   if (depth == 1) {
36509566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
36519566063dSJacob Faibussowitsch     if (dim == 0)      PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
36529566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
3653793f3fe5SMatthew G. Knepley     else               {*pStart = 0; *pEnd = 0;}
3654793f3fe5SMatthew G. Knepley   } else {
36559566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
3656793f3fe5SMatthew G. Knepley   }
3657793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3658793f3fe5SMatthew G. Knepley }
3659793f3fe5SMatthew G. Knepley 
366028d58a37SPierre Jolivet static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
3661502a2867SDave May {
3662502a2867SDave May   PetscSF           sf;
36630a19bb7dSprj-   PetscInt          niranks, njranks, n;
36640a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
36650a19bb7dSprj-   DM_Plex           *data = (DM_Plex*) dm->data;
3666502a2867SDave May 
36672f356facSMatthew G. Knepley   PetscFunctionBegin;
36689566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
36690a19bb7dSprj-   if (!data->neighbors) {
36709566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
36719566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
36729566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
36739566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
36749566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
36759566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
36760a19bb7dSprj-     n = njranks + niranks;
36779566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
36780a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
36799566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
36800a19bb7dSprj-   }
36810a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
36820a19bb7dSprj-   if (ranks) {
36830a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
36840a19bb7dSprj-     else                    *ranks = NULL;
36850a19bb7dSprj-   }
3686502a2867SDave May   PetscFunctionReturn(0);
3687502a2867SDave May }
3688502a2867SDave May 
36891eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
36901eb70e55SToby Isaac 
369146fa42a0SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm)
3692552f7358SJed Brown {
3693552f7358SJed Brown   PetscFunctionBegin;
3694552f7358SJed Brown   dm->ops->view                            = DMView_Plex;
36952c40f234SMatthew G. Knepley   dm->ops->load                            = DMLoad_Plex;
3696552f7358SJed Brown   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
369738221697SMatthew G. Knepley   dm->ops->clone                           = DMClone_Plex;
3698552f7358SJed Brown   dm->ops->setup                           = DMSetUp_Plex;
36991bb6d2a8SBarry Smith   dm->ops->createlocalsection              = DMCreateLocalSection_Plex;
370066ad2231SToby Isaac   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
3701552f7358SJed Brown   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
3702552f7358SJed Brown   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
3703184d77edSJed Brown   dm->ops->getlocaltoglobalmapping         = NULL;
37040298fd71SBarry Smith   dm->ops->createfieldis                   = NULL;
3705552f7358SJed Brown   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
3706f19dbd58SToby Isaac   dm->ops->createcoordinatefield           = DMCreateCoordinateField_Plex;
37070a6ba040SMatthew G. Knepley   dm->ops->getcoloring                     = NULL;
3708552f7358SJed Brown   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
3709bceba477SMatthew G. Knepley   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
3710bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix                = DMCreateMassMatrix_Plex;
3711b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped          = DMCreateMassMatrixLumped_Plex;
37125a84ad33SLisandro Dalcin   dm->ops->createinjection                 = DMCreateInjection_Plex;
3713552f7358SJed Brown   dm->ops->refine                          = DMRefine_Plex;
37140a6ba040SMatthew G. Knepley   dm->ops->coarsen                         = DMCoarsen_Plex;
37150a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
3716b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
3717d410b0cfSMatthew G. Knepley   dm->ops->extrude                         = DMExtrude_Plex;
37180298fd71SBarry Smith   dm->ops->globaltolocalbegin              = NULL;
37190298fd71SBarry Smith   dm->ops->globaltolocalend                = NULL;
37200298fd71SBarry Smith   dm->ops->localtoglobalbegin              = NULL;
37210298fd71SBarry Smith   dm->ops->localtoglobalend                = NULL;
3722552f7358SJed Brown   dm->ops->destroy                         = DMDestroy_Plex;
3723552f7358SJed Brown   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
37242adcc780SMatthew G. Knepley   dm->ops->createsuperdm                   = DMCreateSuperDM_Plex;
3725793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
3726552f7358SJed Brown   dm->ops->locatepoints                    = DMLocatePoints_Plex;
37270709b2feSToby Isaac   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
37280709b2feSToby Isaac   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
3729bfc4295aSToby Isaac   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
37308c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
3731ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal        = DMProjectBdFieldLabelLocal_Plex;
37320709b2feSToby Isaac   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
3733b698f381SToby Isaac   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
37342a16baeaSToby Isaac   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
373528d58a37SPierre Jolivet   dm->ops->getneighbors                    = DMGetNeighbors_Plex;
37369566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex));
37379566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertTimeDerviativeBoundaryValues_C",DMPlexInsertTimeDerivativeBoundaryValues_Plex));
37389566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex));
37399566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C",DMCreateNeumannOverlap_Plex));
37409566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexGetOverlap_C",DMPlexGetOverlap_Plex));
37419566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexDistributeGetDefault_C",DMPlexDistributeGetDefault_Plex));
37429566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexDistributeSetDefault_C",DMPlexDistributeSetDefault_Plex));
37439566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C",DMInterpolateSolution_Plex));
3744552f7358SJed Brown   PetscFunctionReturn(0);
3745552f7358SJed Brown }
3746552f7358SJed Brown 
374746fa42a0SMatthew G. Knepley PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
374863a16f15SMatthew G. Knepley {
374963a16f15SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
375063a16f15SMatthew G. Knepley 
375163a16f15SMatthew G. Knepley   PetscFunctionBegin;
375263a16f15SMatthew G. Knepley   mesh->refct++;
375363a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
37549566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX));
37559566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
375663a16f15SMatthew G. Knepley   PetscFunctionReturn(0);
375763a16f15SMatthew G. Knepley }
375863a16f15SMatthew G. Knepley 
37598818961aSMatthew G Knepley /*MC
37608818961aSMatthew G Knepley   DMPLEX = "plex" - A DM object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
37618818961aSMatthew G Knepley                     In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
37628818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
37638818961aSMatthew G Knepley                     ownership of the underlying DMPlex points. This is specified by another PetscSection object.
37648818961aSMatthew G Knepley 
3765e5893cccSMatthew G. Knepley   Options Database Keys:
3766250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
3767250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
3768250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
3769250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
3770250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
3771250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
3772250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
3773ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
3774f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
3775f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
3776f12cf164SMatthew G. Knepley . -dm_plex_max_projection_height     - Maxmimum mesh point height used to project locally
3777f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
3778250712c9SMatthew G. Knepley . -dm_plex_check_all                 - Perform all shecks below
3779f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
3780f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
3781f12cf164SMatthew 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
3782f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
3783f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
3784e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
3785e5893cccSMatthew G. Knepley - -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
3786e5893cccSMatthew G. Knepley 
37878818961aSMatthew G Knepley   Level: intermediate
37888818961aSMatthew G Knepley 
37898818961aSMatthew G Knepley .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
37908818961aSMatthew G Knepley M*/
37918818961aSMatthew G Knepley 
37928cc058d9SJed Brown PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
3793552f7358SJed Brown {
3794552f7358SJed Brown   DM_Plex       *mesh;
3795412e9a14SMatthew G. Knepley   PetscInt       unit;
3796552f7358SJed Brown 
3797552f7358SJed Brown   PetscFunctionBegin;
3798552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37999566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(dm,&mesh));
3800552f7358SJed Brown   dm->data = mesh;
3801552f7358SJed Brown 
3802552f7358SJed Brown   mesh->refct             = 1;
38039566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
3804552f7358SJed Brown   mesh->maxConeSize       = 0;
38050298fd71SBarry Smith   mesh->cones             = NULL;
38060298fd71SBarry Smith   mesh->coneOrientations  = NULL;
38079566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
3808552f7358SJed Brown   mesh->maxSupportSize    = 0;
38090298fd71SBarry Smith   mesh->supports          = NULL;
3810552f7358SJed Brown   mesh->refinementUniform = PETSC_TRUE;
3811552f7358SJed Brown   mesh->refinementLimit   = -1.0;
3812e600fa54SMatthew G. Knepley   mesh->distDefault       = PETSC_TRUE;
38137d0f5628SVaclav Hapla   mesh->interpolated      = DMPLEX_INTERPOLATED_INVALID;
38147d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
3815552f7358SJed Brown 
38160298fd71SBarry Smith   mesh->facesTmp = NULL;
3817552f7358SJed Brown 
3818d9deefdfSMatthew G. Knepley   mesh->tetgenOpts   = NULL;
3819d9deefdfSMatthew G. Knepley   mesh->triangleOpts = NULL;
38209566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
38212e62ab5aSMatthew G. Knepley   mesh->remeshBd     = PETSC_FALSE;
3822d9deefdfSMatthew G. Knepley 
38230298fd71SBarry Smith   mesh->subpointMap = NULL;
3824552f7358SJed Brown 
38258865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
3826552f7358SJed Brown 
38270aef6b92SMatthew G. Knepley   mesh->regularRefinement   = PETSC_FALSE;
3828df0420ecSMatthew G. Knepley   mesh->depthState          = -1;
3829ba2698f1SMatthew G. Knepley   mesh->celltypeState       = -1;
38300298fd71SBarry Smith   mesh->globalVertexNumbers = NULL;
38310298fd71SBarry Smith   mesh->globalCellNumbers   = NULL;
3832a68b90caSToby Isaac   mesh->anchorSection       = NULL;
3833a68b90caSToby Isaac   mesh->anchorIS            = NULL;
383441e6d900SToby Isaac   mesh->createanchors       = NULL;
3835fa73a4e1SToby Isaac   mesh->computeanchormatrix = NULL;
3836d961a43aSToby Isaac   mesh->parentSection       = NULL;
3837d961a43aSToby Isaac   mesh->parents             = NULL;
3838d961a43aSToby Isaac   mesh->childIDs            = NULL;
3839d961a43aSToby Isaac   mesh->childSection        = NULL;
3840d961a43aSToby Isaac   mesh->children            = NULL;
3841d6a7ad0dSToby Isaac   mesh->referenceTree       = NULL;
3842dcbd3bf7SToby Isaac   mesh->getchildsymmetry    = NULL;
3843552f7358SJed Brown   mesh->vtkCellHeight       = 0;
3844e228b242SToby Isaac   mesh->useAnchors          = PETSC_FALSE;
3845552f7358SJed Brown 
3846b29cfa1cSToby Isaac   mesh->maxProjectionHeight = 0;
3847b29cfa1cSToby Isaac 
38480a19bb7dSprj-   mesh->neighbors           = NULL;
38490a19bb7dSprj- 
3850552f7358SJed Brown   mesh->printSetValues = PETSC_FALSE;
3851552f7358SJed Brown   mesh->printFEM       = 0;
38526113b454SMatthew G. Knepley   mesh->printTol       = 1.0e-10;
3853552f7358SJed Brown 
38549566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
3855552f7358SJed Brown   PetscFunctionReturn(0);
3856552f7358SJed Brown }
3857552f7358SJed Brown 
3858552f7358SJed Brown /*@
3859552f7358SJed Brown   DMPlexCreate - Creates a DMPlex object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
3860552f7358SJed Brown 
3861d083f849SBarry Smith   Collective
3862552f7358SJed Brown 
3863552f7358SJed Brown   Input Parameter:
3864552f7358SJed Brown . comm - The communicator for the DMPlex object
3865552f7358SJed Brown 
3866552f7358SJed Brown   Output Parameter:
3867552f7358SJed Brown . mesh  - The DMPlex object
3868552f7358SJed Brown 
3869552f7358SJed Brown   Level: beginner
3870552f7358SJed Brown 
3871552f7358SJed Brown @*/
3872552f7358SJed Brown PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
3873552f7358SJed Brown {
3874552f7358SJed Brown   PetscFunctionBegin;
3875552f7358SJed Brown   PetscValidPointer(mesh,2);
38769566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
38779566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
3878552f7358SJed Brown   PetscFunctionReturn(0);
3879552f7358SJed Brown }
3880552f7358SJed Brown 
3881b09969d6SVaclav Hapla /*@C
3882b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel - Build distributed DMPLEX topology from a list of vertices for each cell (common mesh generator output)
3883b09969d6SVaclav Hapla 
3884b09969d6SVaclav Hapla   Input Parameters:
3885b09969d6SVaclav Hapla + dm - The DM
3886b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
3887325d53feSBarry Smith . numVertices - The number of vertices to be owned by this process, or PETSC_DECIDE
3888325d53feSBarry Smith . NVertices - The global number of vertices, or PETSC_DETERMINE
3889b09969d6SVaclav Hapla . numCorners - The number of vertices for each cell
38905e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
3891b09969d6SVaclav Hapla 
3892be8c289dSNicolas Barral   Output Parameters:
3893be8c289dSNicolas Barral + vertexSF - (Optional) SF describing complete vertex ownership
3894be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
3895b09969d6SVaclav Hapla 
3896b09969d6SVaclav Hapla   Notes:
3897b09969d6SVaclav Hapla   Two triangles sharing a face
3898b09969d6SVaclav Hapla $
3899b09969d6SVaclav Hapla $        2
3900b09969d6SVaclav Hapla $      / | \
3901b09969d6SVaclav Hapla $     /  |  \
3902b09969d6SVaclav Hapla $    /   |   \
3903b09969d6SVaclav Hapla $   0  0 | 1  3
3904b09969d6SVaclav Hapla $    \   |   /
3905b09969d6SVaclav Hapla $     \  |  /
3906b09969d6SVaclav Hapla $      \ | /
3907b09969d6SVaclav Hapla $        1
3908b09969d6SVaclav Hapla would have input
3909b09969d6SVaclav Hapla $  numCells = 2, numVertices = 4
3910b09969d6SVaclav Hapla $  cells = [0 1 2  1 3 2]
3911b09969d6SVaclav Hapla $
3912b09969d6SVaclav Hapla which would result in the DMPlex
3913b09969d6SVaclav Hapla $
3914b09969d6SVaclav Hapla $        4
3915b09969d6SVaclav Hapla $      / | \
3916b09969d6SVaclav Hapla $     /  |  \
3917b09969d6SVaclav Hapla $    /   |   \
3918b09969d6SVaclav Hapla $   2  0 | 1  5
3919b09969d6SVaclav Hapla $    \   |   /
3920b09969d6SVaclav Hapla $     \  |  /
3921b09969d6SVaclav Hapla $      \ | /
3922b09969d6SVaclav Hapla $        3
3923b09969d6SVaclav Hapla 
392425b6865aSVaclav Hapla   Vertices are implicitly numbered consecutively 0,...,NVertices.
392525b6865aSVaclav Hapla   Each rank owns a chunk of numVertices consecutive vertices.
392625b6865aSVaclav Hapla   If numVertices is PETSC_DECIDE, PETSc will distribute them as evenly as possible using PetscLayout.
3927325d53feSBarry Smith   If NVertices is PETSC_DETERMINE and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
3928325d53feSBarry Smith   If only NVertices is PETSC_DETERMINE, it is computed as the sum of numVertices over all ranks.
392925b6865aSVaclav Hapla 
3930b09969d6SVaclav Hapla   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
3931b09969d6SVaclav Hapla 
3932b09969d6SVaclav Hapla   Not currently supported in Fortran.
3933b09969d6SVaclav Hapla 
3934b09969d6SVaclav Hapla   Level: advanced
3935b09969d6SVaclav Hapla 
3936b09969d6SVaclav Hapla .seealso: DMPlexBuildFromCellList(), DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildCoordinatesFromCellListParallel()
3937b09969d6SVaclav Hapla @*/
3938be8c289dSNicolas Barral PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
3939a47d0d45SMatthew G. Knepley {
39402464107aSksagiyam   PetscSF         sfPoint;
39412464107aSksagiyam   PetscLayout     layout;
394282fb893eSVaclav Hapla   PetscInt        numVerticesAdj, *verticesAdj, *cones, c, p;
3943a47d0d45SMatthew G. Knepley 
3944a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
394525b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm,NVertices,4);
39469566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0));
394725b6865aSVaclav Hapla   /* Get/check global number of vertices */
394825b6865aSVaclav Hapla   {
394925b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
395025b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
395125b6865aSVaclav Hapla 
395225b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
395325b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
395425b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
395525b6865aSVaclav Hapla     ++NVerticesInCells;
39569566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm)));
395725b6865aSVaclav Hapla 
395825b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
39592c71b3e2SJacob Faibussowitsch     else PetscCheckFalse(NVertices != PETSC_DECIDE && NVertices < NVerticesInCells,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Specified global number of vertices %D must be greater than or equal to the number of vertices in cells %D",NVertices,NVerticesInCells);
396025b6865aSVaclav Hapla   }
39619079aca8SVaclav Hapla   /* Count locally unique vertices */
39629079aca8SVaclav Hapla   {
39639079aca8SVaclav Hapla     PetscHSetI vhash;
39649079aca8SVaclav Hapla     PetscInt off = 0;
39659079aca8SVaclav Hapla 
39669566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
3967a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
3968a47d0d45SMatthew G. Knepley       for (p = 0; p < numCorners; ++p) {
39699566063dSJacob Faibussowitsch         PetscCall(PetscHSetIAdd(vhash, cells[c*numCorners+p]));
3970a47d0d45SMatthew G. Knepley       }
3971a47d0d45SMatthew G. Knepley     }
39729566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
39739566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
3974be8c289dSNicolas Barral     else { verticesAdj = *verticesAdjSaved; }
39759566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
39769566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
39772c71b3e2SJacob Faibussowitsch     PetscCheckFalse(off != numVerticesAdj,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
3978a47d0d45SMatthew G. Knepley   }
39799566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
3980a47d0d45SMatthew G. Knepley   /* Create cones */
39819566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells+numVerticesAdj));
39829566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
39839566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
39849566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm,&cones));
3985a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
3986a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
3987a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c*numCorners+p];
3988a47d0d45SMatthew G. Knepley       PetscInt       lv;
3989a47d0d45SMatthew G. Knepley 
39909079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
39919079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
39929566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
39932c71b3e2SJacob Faibussowitsch       PetscCheckFalse(lv < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
3994961cfab0SVaclav Hapla       cones[c*numCorners+p] = lv+numCells;
3995a47d0d45SMatthew G. Knepley     }
3996a47d0d45SMatthew G. Knepley   }
39972464107aSksagiyam   /* Build point sf */
39989566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
39999566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
40009566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
40019566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
40029566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
40039566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
40049566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
40059566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) sfPoint, "point SF"));
40062464107aSksagiyam   if (dm->sf) {
40072464107aSksagiyam     const char *prefix;
40082464107aSksagiyam 
40099566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
40109566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
40112464107aSksagiyam   }
40129566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
40139566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
40149566063dSJacob Faibussowitsch   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF"));
4015a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
40169566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
40179566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
40189566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0));
4019a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4020a47d0d45SMatthew G. Knepley }
4021a47d0d45SMatthew G. Knepley 
4022b09969d6SVaclav Hapla /*@C
4023b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellListParallel - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4024b09969d6SVaclav Hapla 
4025b09969d6SVaclav Hapla   Input Parameters:
4026b09969d6SVaclav Hapla + dm - The DM
4027b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
4028b09969d6SVaclav Hapla . sfVert - SF describing complete vertex ownership
4029b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4030b09969d6SVaclav Hapla 
4031b09969d6SVaclav Hapla   Level: advanced
4032b09969d6SVaclav Hapla 
4033b09969d6SVaclav Hapla   Notes:
4034b09969d6SVaclav Hapla   Not currently supported in Fortran.
4035b09969d6SVaclav Hapla 
4036b09969d6SVaclav Hapla .seealso: DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildFromCellListParallel()
4037b09969d6SVaclav Hapla @*/
40381edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
4039a47d0d45SMatthew G. Knepley {
4040a47d0d45SMatthew G. Knepley   PetscSection   coordSection;
4041a47d0d45SMatthew G. Knepley   Vec            coordinates;
4042a47d0d45SMatthew G. Knepley   PetscScalar   *coords;
40431edcf0b2SVaclav Hapla   PetscInt       numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
4044a47d0d45SMatthew G. Knepley 
4045a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
40469566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0));
40479566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
40482c71b3e2SJacob Faibussowitsch   PetscCheckFalse(vStart < 0 || vEnd < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
40499566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
40509566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
40512c71b3e2SJacob Faibussowitsch   PetscCheckFalse(vEnd - vStart != numVerticesAdj,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Supplied sfVert has wrong number of leaves = %D != %D = vEnd - vStart",numVerticesAdj,vEnd - vStart);
40529566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
40539566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
40549566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
40559566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
40561edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
40579566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
40589566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
4059a47d0d45SMatthew G. Knepley   }
40609566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
40619566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
40629566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
40639566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
40649566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
40659566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
40669566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
40679566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4068a47d0d45SMatthew G. Knepley   {
4069a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
4070a47d0d45SMatthew G. Knepley 
4071a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
40729566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype));
40739566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
407421016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
407521016a8bSBarry Smith     {
407621016a8bSBarry Smith     PetscScalar *svertexCoords;
407721016a8bSBarry Smith     PetscInt    i;
40789566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numVertices*spaceDim,&svertexCoords));
40793612f820SVaclav Hapla     for (i=0; i<numVertices*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
40809566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE));
40819566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE));
40829566063dSJacob Faibussowitsch     PetscCall(PetscFree(svertexCoords));
408321016a8bSBarry Smith     }
408421016a8bSBarry Smith #else
40859566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE));
40869566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE));
408721016a8bSBarry Smith #endif
40889566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
4089a47d0d45SMatthew G. Knepley   }
40909566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
40919566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
40929566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
40939566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0));
4094a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4095a47d0d45SMatthew G. Knepley }
4096a47d0d45SMatthew G. Knepley 
4097c3edce3dSSatish Balay /*@
4098b09969d6SVaclav Hapla   DMPlexCreateFromCellListParallelPetsc - Create distributed DMPLEX from a list of vertices for each cell (common mesh generator output)
4099a47d0d45SMatthew G. Knepley 
4100a47d0d45SMatthew G. Knepley   Input Parameters:
4101a47d0d45SMatthew G. Knepley + comm - The communicator
4102a47d0d45SMatthew G. Knepley . dim - The topological dimension of the mesh
4103a47d0d45SMatthew G. Knepley . numCells - The number of cells owned by this process
410425b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
410525b6865aSVaclav Hapla . NVertices - The global number of vertices, or PETSC_DECIDE
4106a47d0d45SMatthew G. Knepley . numCorners - The number of vertices for each cell
4107a47d0d45SMatthew G. Knepley . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
4108a47d0d45SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4109a47d0d45SMatthew G. Knepley . spaceDim - The spatial dimension used for coordinates
4110a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4111a47d0d45SMatthew G. Knepley 
4112d8d19677SJose E. Roman   Output Parameters:
411318d54ad4SMichael Lange + dm - The DM
4114be8c289dSNicolas Barral . vertexSF - (Optional) SF describing complete vertex ownership
4115be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
4116a47d0d45SMatthew G. Knepley 
4117b09969d6SVaclav Hapla   Notes:
4118b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(),
4119b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel(), DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellListParallel()
4120a47d0d45SMatthew G. Knepley 
412125b6865aSVaclav Hapla   See DMPlexBuildFromCellListParallel() for an example and details about the topology-related parameters.
412225b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellListParallel() for details about the geometry-related parameters.
412325b6865aSVaclav Hapla 
4124b09969d6SVaclav Hapla   Level: intermediate
4125a47d0d45SMatthew G. Knepley 
4126b09969d6SVaclav Hapla .seealso: DMPlexCreateFromCellListPetsc(), DMPlexBuildFromCellListParallel(), DMPlexBuildCoordinatesFromCellListParallel(), DMPlexCreateFromDAG(), DMPlexCreate()
4127a47d0d45SMatthew G. Knepley @*/
4128be8c289dSNicolas 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)
4129a47d0d45SMatthew G. Knepley {
4130a47d0d45SMatthew G. Knepley   PetscSF        sfVert;
4131a47d0d45SMatthew G. Knepley 
4132a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
41339566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
41349566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
4135a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
4136064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
41379566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
41389566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
4139a47d0d45SMatthew G. Knepley   if (interpolate) {
41405fd9971aSMatthew G. Knepley     DM idm;
4141a47d0d45SMatthew G. Knepley 
41429566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
41439566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
4144a47d0d45SMatthew G. Knepley     *dm  = idm;
4145a47d0d45SMatthew G. Knepley   }
41469566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
414718d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
41489566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
4149a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4150a47d0d45SMatthew G. Knepley }
4151a47d0d45SMatthew G. Knepley 
4152b09969d6SVaclav Hapla /*@C
4153b09969d6SVaclav Hapla   DMPlexBuildFromCellList - Build DMPLEX topology from a list of vertices for each cell (common mesh generator output)
41549298eaa6SMatthew G Knepley 
41559298eaa6SMatthew G Knepley   Input Parameters:
4156b09969d6SVaclav Hapla + dm - The DM
4157b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
4158325d53feSBarry Smith . numVertices - The number of vertices owned by this process, or PETSC_DETERMINE
41599298eaa6SMatthew G Knepley . numCorners - The number of vertices for each cell
41605e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
41619298eaa6SMatthew G Knepley 
4162b09969d6SVaclav Hapla   Level: advanced
41639298eaa6SMatthew G Knepley 
4164b09969d6SVaclav Hapla   Notes:
4165b09969d6SVaclav Hapla   Two triangles sharing a face
41669298eaa6SMatthew G Knepley $
41679298eaa6SMatthew G Knepley $        2
41689298eaa6SMatthew G Knepley $      / | \
41699298eaa6SMatthew G Knepley $     /  |  \
41709298eaa6SMatthew G Knepley $    /   |   \
41719298eaa6SMatthew G Knepley $   0  0 | 1  3
41729298eaa6SMatthew G Knepley $    \   |   /
41739298eaa6SMatthew G Knepley $     \  |  /
41749298eaa6SMatthew G Knepley $      \ | /
41759298eaa6SMatthew G Knepley $        1
41769298eaa6SMatthew G Knepley would have input
41779298eaa6SMatthew G Knepley $  numCells = 2, numVertices = 4
41789298eaa6SMatthew G Knepley $  cells = [0 1 2  1 3 2]
41799298eaa6SMatthew G Knepley $
41809298eaa6SMatthew G Knepley which would result in the DMPlex
41819298eaa6SMatthew G Knepley $
41829298eaa6SMatthew G Knepley $        4
41839298eaa6SMatthew G Knepley $      / | \
41849298eaa6SMatthew G Knepley $     /  |  \
41859298eaa6SMatthew G Knepley $    /   |   \
41869298eaa6SMatthew G Knepley $   2  0 | 1  5
41879298eaa6SMatthew G Knepley $    \   |   /
41889298eaa6SMatthew G Knepley $     \  |  /
41899298eaa6SMatthew G Knepley $      \ | /
41909298eaa6SMatthew G Knepley $        3
41919298eaa6SMatthew G Knepley 
4192325d53feSBarry Smith   If numVertices is PETSC_DETERMINE, it is computed by PETSc as the maximum vertex index in cells + 1.
419325b6865aSVaclav Hapla 
4194b09969d6SVaclav Hapla   Not currently supported in Fortran.
41959298eaa6SMatthew G Knepley 
4196b09969d6SVaclav Hapla .seealso: DMPlexBuildFromCellListParallel(), DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromCellListPetsc()
4197b09969d6SVaclav Hapla @*/
41985e488331SVaclav Hapla PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
4199b09969d6SVaclav Hapla {
4200961cfab0SVaclav Hapla   PetscInt      *cones, c, p, dim;
4201b09969d6SVaclav Hapla 
4202b09969d6SVaclav Hapla   PetscFunctionBegin;
42039566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0));
42049566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
420525b6865aSVaclav Hapla   /* Get/check global number of vertices */
420625b6865aSVaclav Hapla   {
420725b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
420825b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
420925b6865aSVaclav Hapla 
421025b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
421125b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
421225b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
421325b6865aSVaclav Hapla     ++NVerticesInCells;
421425b6865aSVaclav Hapla 
421525b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
42162c71b3e2SJacob Faibussowitsch     else PetscCheckFalse(numVertices < NVerticesInCells,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Specified number of vertices %D must be greater than or equal to the number of vertices in cells %D",numVertices,NVerticesInCells);
421725b6865aSVaclav Hapla   }
42189566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells+numVertices));
4219b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
42209566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, c, numCorners));
4221b09969d6SVaclav Hapla   }
42229566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
42239566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm,&cones));
4224b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
4225b09969d6SVaclav Hapla     for (p = 0; p < numCorners; ++p) {
4226961cfab0SVaclav Hapla       cones[c*numCorners+p] = cells[c*numCorners+p]+numCells;
4227b09969d6SVaclav Hapla     }
4228b09969d6SVaclav Hapla   }
42299566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
42309566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
42319566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0));
4232b09969d6SVaclav Hapla   PetscFunctionReturn(0);
4233b09969d6SVaclav Hapla }
4234b09969d6SVaclav Hapla 
4235b09969d6SVaclav Hapla /*@C
4236b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellList - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4237b09969d6SVaclav Hapla 
4238b09969d6SVaclav Hapla   Input Parameters:
4239b09969d6SVaclav Hapla + dm - The DM
4240b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
4241b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4242b09969d6SVaclav Hapla 
4243b09969d6SVaclav Hapla   Level: advanced
4244b09969d6SVaclav Hapla 
4245b09969d6SVaclav Hapla   Notes:
4246b09969d6SVaclav Hapla   Not currently supported in Fortran.
4247b09969d6SVaclav Hapla 
4248b09969d6SVaclav Hapla .seealso: DMPlexBuildCoordinatesFromCellListParallel(), DMPlexCreateFromCellListPetsc(), DMPlexBuildFromCellList()
4249b09969d6SVaclav Hapla @*/
42501edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
4251b09969d6SVaclav Hapla {
4252b09969d6SVaclav Hapla   PetscSection   coordSection;
4253b09969d6SVaclav Hapla   Vec            coordinates;
4254b09969d6SVaclav Hapla   DM             cdm;
4255b09969d6SVaclav Hapla   PetscScalar   *coords;
42561edcf0b2SVaclav Hapla   PetscInt       v, vStart, vEnd, d;
4257b09969d6SVaclav Hapla 
4258b09969d6SVaclav Hapla   PetscFunctionBegin;
42599566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0));
42609566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
42612c71b3e2SJacob Faibussowitsch   PetscCheckFalse(vStart < 0 || vEnd < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
42629566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
42639566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
42649566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
42659566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
42669566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
42671edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
42689566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
42699566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
4270b09969d6SVaclav Hapla   }
42719566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
4272b09969d6SVaclav Hapla 
42739566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
42749566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
42759566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
42769566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
42779566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
42781edcf0b2SVaclav Hapla   for (v = 0; v < vEnd-vStart; ++v) {
4279b09969d6SVaclav Hapla     for (d = 0; d < spaceDim; ++d) {
4280b09969d6SVaclav Hapla       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
4281b09969d6SVaclav Hapla     }
4282b09969d6SVaclav Hapla   }
42839566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
42849566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
42859566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
42869566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0));
4287b09969d6SVaclav Hapla   PetscFunctionReturn(0);
4288b09969d6SVaclav Hapla }
4289b09969d6SVaclav Hapla 
4290b09969d6SVaclav Hapla /*@
42913df08285SMatthew 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
42923df08285SMatthew G. Knepley 
42933df08285SMatthew G. Knepley   Collective on comm
4294b09969d6SVaclav Hapla 
4295b09969d6SVaclav Hapla   Input Parameters:
4296b09969d6SVaclav Hapla + comm - The communicator
4297b09969d6SVaclav Hapla . dim - The topological dimension of the mesh
42983df08285SMatthew G. Knepley . numCells - The number of cells, only on process 0
42993df08285SMatthew G. Knepley . numVertices - The number of vertices owned by this process, or PETSC_DECIDE, only on process 0
43003df08285SMatthew G. Knepley . numCorners - The number of vertices for each cell, only on process 0
4301b09969d6SVaclav Hapla . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
43023df08285SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
4303b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
43043df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
4305b09969d6SVaclav Hapla 
4306b09969d6SVaclav Hapla   Output Parameter:
43073df08285SMatthew G. Knepley . dm - The DM, which only has points on process 0
4308b09969d6SVaclav Hapla 
4309b09969d6SVaclav Hapla   Notes:
4310b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(), DMPlexBuildFromCellList(),
4311b09969d6SVaclav Hapla   DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellList()
4312b09969d6SVaclav Hapla 
431325b6865aSVaclav Hapla   See DMPlexBuildFromCellList() for an example and details about the topology-related parameters.
431425b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellList() for details about the geometry-related parameters.
43153df08285SMatthew G. Knepley   See DMPlexCreateFromCellListParallelPetsc() for parallel input
431625b6865aSVaclav Hapla 
4317b09969d6SVaclav Hapla   Level: intermediate
4318b09969d6SVaclav Hapla 
4319b09969d6SVaclav Hapla .seealso: DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildFromCellList(), DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
43209298eaa6SMatthew G Knepley @*/
4321a4a685f2SJacob 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)
43229298eaa6SMatthew G Knepley {
43233df08285SMatthew G. Knepley   PetscMPIInt    rank;
43249298eaa6SMatthew G Knepley 
43259298eaa6SMatthew G Knepley   PetscFunctionBegin;
432628b400f6SJacob 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.");
43279566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
43289566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
43299566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
43309566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
43319566063dSJacob Faibussowitsch   if (!rank) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
43329566063dSJacob Faibussowitsch   else       PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
43339298eaa6SMatthew G Knepley   if (interpolate) {
43345fd9971aSMatthew G. Knepley     DM idm;
43359298eaa6SMatthew G Knepley 
43369566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
43379566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
43389298eaa6SMatthew G Knepley     *dm  = idm;
43399298eaa6SMatthew G Knepley   }
43409566063dSJacob Faibussowitsch   if (!rank) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
43419566063dSJacob Faibussowitsch   else       PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
43429298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
43439298eaa6SMatthew G Knepley }
43449298eaa6SMatthew G Knepley 
4345939f6067SMatthew G. Knepley /*@
4346939f6067SMatthew 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
4347939f6067SMatthew G. Knepley 
4348939f6067SMatthew G. Knepley   Input Parameters:
4349c73cfb54SMatthew G. Knepley + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
4350939f6067SMatthew G. Knepley . depth - The depth of the DAG
4351367003a6SStefano Zampini . numPoints - Array of size depth + 1 containing the number of points at each depth
4352939f6067SMatthew G. Knepley . coneSize - The cone size of each point
4353939f6067SMatthew G. Knepley . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
4354939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
4355367003a6SStefano Zampini - vertexCoords - An array of numPoints[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via DMSetCoordinateDim()
4356939f6067SMatthew G. Knepley 
4357939f6067SMatthew G. Knepley   Output Parameter:
4358939f6067SMatthew G. Knepley . dm - The DM
4359939f6067SMatthew G. Knepley 
4360939f6067SMatthew G. Knepley   Note: Two triangles sharing a face would have input
4361939f6067SMatthew G. Knepley $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
4362939f6067SMatthew G. Knepley $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
4363939f6067SMatthew G. Knepley $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
4364939f6067SMatthew G. Knepley $
4365939f6067SMatthew G. Knepley which would result in the DMPlex
4366939f6067SMatthew G. Knepley $
4367939f6067SMatthew G. Knepley $        4
4368939f6067SMatthew G. Knepley $      / | \
4369939f6067SMatthew G. Knepley $     /  |  \
4370939f6067SMatthew G. Knepley $    /   |   \
4371939f6067SMatthew G. Knepley $   2  0 | 1  5
4372939f6067SMatthew G. Knepley $    \   |   /
4373939f6067SMatthew G. Knepley $     \  |  /
4374939f6067SMatthew G. Knepley $      \ | /
4375939f6067SMatthew G. Knepley $        3
4376939f6067SMatthew G. Knepley $
4377a4a685f2SJacob Faibussowitsch $ Notice that all points are numbered consecutively, unlike DMPlexCreateFromCellListPetsc()
4378939f6067SMatthew G. Knepley 
4379939f6067SMatthew G. Knepley   Level: advanced
4380939f6067SMatthew G. Knepley 
4381a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromCellListPetsc(), DMPlexCreate()
4382939f6067SMatthew G. Knepley @*/
43839298eaa6SMatthew G Knepley PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
43849298eaa6SMatthew G Knepley {
43859298eaa6SMatthew G Knepley   Vec            coordinates;
43869298eaa6SMatthew G Knepley   PetscSection   coordSection;
43879298eaa6SMatthew G Knepley   PetscScalar    *coords;
4388811e8653SToby Isaac   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
43899298eaa6SMatthew G Knepley 
43909298eaa6SMatthew G Knepley   PetscFunctionBegin;
43919566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
43929566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
43932c71b3e2SJacob Faibussowitsch   PetscCheckFalse(dimEmbed < dim,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %D cannot be less than intrinsic dimension %d",dimEmbed,dim);
43949298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
43959566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
43969298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
43979566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p-pStart]));
439897e052ccSToby Isaac     if (firstVertex < 0 && !coneSize[p - pStart]) {
439997e052ccSToby Isaac       firstVertex = p - pStart;
44009298eaa6SMatthew G Knepley     }
440197e052ccSToby Isaac   }
44022c71b3e2SJacob Faibussowitsch   PetscCheckFalse(firstVertex < 0 && numPoints[0],PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %D vertices but could not find any", numPoints[0]);
44039566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
44049298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
44059566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
44069566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
44079298eaa6SMatthew G Knepley   }
44089566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
44099566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
44109298eaa6SMatthew G Knepley   /* Build coordinates */
44119566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
44129566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
44139566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
44149566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]));
44159298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
44169566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
44179566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
44189298eaa6SMatthew G Knepley   }
44199566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
44209566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
44219566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
44229566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
44239566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
44249566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, dimEmbed));
44259566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
44269318fe57SMatthew G. Knepley   if (vertexCoords) {
44279566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
44289298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
44299298eaa6SMatthew G Knepley       PetscInt off;
44309298eaa6SMatthew G Knepley 
44319566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v+firstVertex, &off));
4432811e8653SToby Isaac       for (d = 0; d < dimEmbed; ++d) {
4433811e8653SToby Isaac         coords[off+d] = vertexCoords[v*dimEmbed+d];
44349298eaa6SMatthew G Knepley       }
44359298eaa6SMatthew G Knepley     }
44369318fe57SMatthew G. Knepley   }
44379566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
44389566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
44399566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
44409298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
44419298eaa6SMatthew G Knepley }
44428415267dSToby Isaac 
4443ca522641SMatthew G. Knepley /*@C
44448ca92349SMatthew G. Knepley   DMPlexCreateCellVertexFromFile - Create a DMPlex mesh from a simple cell-vertex file.
44458ca92349SMatthew G. Knepley 
44468ca92349SMatthew G. Knepley + comm        - The MPI communicator
44478ca92349SMatthew G. Knepley . filename    - Name of the .dat file
44488ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
44498ca92349SMatthew G. Knepley 
44508ca92349SMatthew G. Knepley   Output Parameter:
44518ca92349SMatthew G. Knepley . dm  - The DM object representing the mesh
44528ca92349SMatthew G. Knepley 
44538ca92349SMatthew G. Knepley   Note: The format is the simplest possible:
44548ca92349SMatthew G. Knepley $ Ne
44558ca92349SMatthew G. Knepley $ v0 v1 ... vk
44568ca92349SMatthew G. Knepley $ Nv
44578ca92349SMatthew G. Knepley $ x y z marker
44588ca92349SMatthew G. Knepley 
44598ca92349SMatthew G. Knepley   Level: beginner
44608ca92349SMatthew G. Knepley 
44618ca92349SMatthew G. Knepley .seealso: DMPlexCreateFromFile(), DMPlexCreateMedFromFile(), DMPlexCreateGmsh(), DMPlexCreate()
44628ca92349SMatthew G. Knepley @*/
44638ca92349SMatthew G. Knepley PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
44648ca92349SMatthew G. Knepley {
44658ca92349SMatthew G. Knepley   DMLabel         marker;
44668ca92349SMatthew G. Knepley   PetscViewer     viewer;
44678ca92349SMatthew G. Knepley   Vec             coordinates;
44688ca92349SMatthew G. Knepley   PetscSection    coordSection;
44698ca92349SMatthew G. Knepley   PetscScalar    *coords;
44708ca92349SMatthew G. Knepley   char            line[PETSC_MAX_PATH_LEN];
44718ca92349SMatthew G. Knepley   PetscInt        dim = 3, cdim = 3, coordSize, v, c, d;
44728ca92349SMatthew G. Knepley   PetscMPIInt     rank;
4473f8d5e320SMatthew G. Knepley   int             snum, Nv, Nc, Ncn, Nl;
44748ca92349SMatthew G. Knepley 
44758ca92349SMatthew G. Knepley   PetscFunctionBegin;
44769566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44779566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
44789566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
44799566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
44809566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
4481dd400576SPatrick Sanan   if (rank == 0) {
44829566063dSJacob Faibussowitsch     PetscCall(PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING));
4483f8d5e320SMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d", &Nc, &Nv, &Ncn, &Nl);
44842c71b3e2SJacob Faibussowitsch     PetscCheckFalse(snum != 4,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
448525ce1634SJed Brown   } else {
4486f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
44878ca92349SMatthew G. Knepley   }
44889566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
44899566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
44909566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc+Nv));
44919566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
44929566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
44938ca92349SMatthew G. Knepley   /* Read topology */
4494dd400576SPatrick Sanan   if (rank == 0) {
4495f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
4496f8d5e320SMatthew G. Knepley     PetscInt cone[8];
44978ca92349SMatthew G. Knepley     int      vbuf[8], v;
44988ca92349SMatthew G. Knepley 
4499f8d5e320SMatthew G. Knepley     for (c = 0; c < Ncn; ++c) {format[c*3+0] = '%'; format[c*3+1] = 'd'; format[c*3+2] = ' ';}
4500f8d5e320SMatthew G. Knepley     format[Ncn*3-1] = '\0';
45019566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
45029566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
45038ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
45049566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
4505f8d5e320SMatthew G. Knepley       switch (Ncn) {
4506f8d5e320SMatthew G. Knepley         case 2: snum = sscanf(line, format, &vbuf[0], &vbuf[1]);break;
4507f8d5e320SMatthew G. Knepley         case 3: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);break;
4508f8d5e320SMatthew G. Knepley         case 4: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);break;
4509f8d5e320SMatthew G. Knepley         case 6: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);break;
4510f8d5e320SMatthew 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;
451198921bdaSJacob Faibussowitsch         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %D vertices", Ncn);
4512f8d5e320SMatthew G. Knepley       }
45132c71b3e2SJacob Faibussowitsch       PetscCheckFalse(snum != Ncn,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
4514f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
45158ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
4516f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
45178ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
45188ca92349SMatthew G. Knepley         cone[1] = cone[3];
45198ca92349SMatthew G. Knepley         cone[3] = tmp;
45208ca92349SMatthew G. Knepley       }
45219566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
45228ca92349SMatthew G. Knepley     }
45238ca92349SMatthew G. Knepley   }
45249566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
45259566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
45268ca92349SMatthew G. Knepley   /* Read coordinates */
45279566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
45289566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
45299566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
45309566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
45318ca92349SMatthew G. Knepley   for (v = Nc; v < Nc+Nv; ++v) {
45329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
45339566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
45348ca92349SMatthew 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, cdim));
45419566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
45429566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4543dd400576SPatrick Sanan   if (rank == 0) {
4544f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
45458ca92349SMatthew G. Knepley     double x[3];
4546f8d5e320SMatthew G. Knepley     int    l, val[3];
45478ca92349SMatthew G. Knepley 
4548f8d5e320SMatthew G. Knepley     if (Nl) {
4549f8d5e320SMatthew G. Knepley       for (l = 0; l < Nl; ++l) {format[l*3+0] = '%'; format[l*3+1] = 'd'; format[l*3+2] = ' ';}
4550f8d5e320SMatthew G. Knepley       format[Nl*3-1] = '\0';
45519566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
45529566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
4553f8d5e320SMatthew G. Knepley     }
45548ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
45559566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3+Nl, NULL, PETSC_STRING));
4556f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
45572c71b3e2SJacob Faibussowitsch       PetscCheckFalse(snum != 3,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
4558f8d5e320SMatthew G. Knepley       switch (Nl) {
4559f8d5e320SMatthew G. Knepley         case 0: snum = 0;break;
4560f8d5e320SMatthew G. Knepley         case 1: snum = sscanf(line, format, &val[0]);break;
4561f8d5e320SMatthew G. Knepley         case 2: snum = sscanf(line, format, &val[0], &val[1]);break;
4562f8d5e320SMatthew G. Knepley         case 3: snum = sscanf(line, format, &val[0], &val[1], &val[2]);break;
456398921bdaSJacob Faibussowitsch         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %D labels", Nl);
4564f8d5e320SMatthew G. Knepley       }
45652c71b3e2SJacob Faibussowitsch       PetscCheckFalse(snum != Nl,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
45668ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v*cdim+d] = x[d];
45679566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v+Nc, val[l]));
45688ca92349SMatthew G. Knepley     }
45698ca92349SMatthew G. Knepley   }
45709566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
45719566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
45729566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
45739566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
45748ca92349SMatthew G. Knepley   if (interpolate) {
45758ca92349SMatthew G. Knepley     DM      idm;
45768ca92349SMatthew G. Knepley     DMLabel bdlabel;
45778ca92349SMatthew G. Knepley 
45789566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
45799566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
45808ca92349SMatthew G. Knepley     *dm  = idm;
45818ca92349SMatthew G. Knepley 
4582f8d5e320SMatthew G. Knepley     if (!Nl) {
45839566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
45849566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
45859566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
45869566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
45878ca92349SMatthew G. Knepley     }
4588f8d5e320SMatthew G. Knepley   }
45898ca92349SMatthew G. Knepley   PetscFunctionReturn(0);
45908ca92349SMatthew G. Knepley }
45918ca92349SMatthew G. Knepley 
45928ca92349SMatthew G. Knepley /*@C
4593ca522641SMatthew G. Knepley   DMPlexCreateFromFile - This takes a filename and produces a DM
4594ca522641SMatthew G. Knepley 
4595ca522641SMatthew G. Knepley   Input Parameters:
4596ca522641SMatthew G. Knepley + comm - The communicator
4597ca522641SMatthew G. Knepley . filename - A file name
4598cd7e8a5eSksagiyam . plexname - The object name of the resulting DM, also used for intra-datafile lookup by some formats
4599ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
4600ca522641SMatthew G. Knepley 
4601ca522641SMatthew G. Knepley   Output Parameter:
4602ca522641SMatthew G. Knepley . dm - The DM
4603ca522641SMatthew G. Knepley 
460402ef0d99SVaclav Hapla   Options Database Keys:
460502ef0d99SVaclav Hapla . -dm_plex_create_from_hdf5_xdmf - use the PETSC_VIEWER_HDF5_XDMF format for reading HDF5
460602ef0d99SVaclav Hapla 
4607bca97951SVaclav Hapla   Use -dm_plex_create_ prefix to pass options to the internal PetscViewer, e.g.
4608bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
4609bca97951SVaclav Hapla 
4610cd7e8a5eSksagiyam   Notes:
4611cd7e8a5eSksagiyam   Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
4612cd7e8a5eSksagiyam   meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
4613cd7e8a5eSksagiyam   before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
4614cd7e8a5eSksagiyam   The input parameter name is thus used to name the DMPlex object when DMPlexCreateFromFile() internally
4615cd7e8a5eSksagiyam   calls DMLoad(). Currently, name is ignored for other viewer types and/or formats.
4616cd7e8a5eSksagiyam 
4617ca522641SMatthew G. Knepley   Level: beginner
4618ca522641SMatthew G. Knepley 
4619cd7e8a5eSksagiyam .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellListPetsc(), DMPlexCreate(), PetscObjectSetName(), DMView(), DMLoad()
4620ca522641SMatthew G. Knepley @*/
4621cd7e8a5eSksagiyam PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
4622ca522641SMatthew G. Knepley {
4623ca522641SMatthew G. Knepley   const char    *extGmsh      = ".msh";
4624de78e4feSLisandro Dalcin   const char    *extGmsh2     = ".msh2";
4625de78e4feSLisandro Dalcin   const char    *extGmsh4     = ".msh4";
4626ca522641SMatthew G. Knepley   const char    *extCGNS      = ".cgns";
4627ca522641SMatthew G. Knepley   const char    *extExodus    = ".exo";
462886a0adb1SJed Brown   const char    *extExodus_e  = ".e";
462990c68965SMatthew G. Knepley   const char    *extGenesis   = ".gen";
46302f0bd6dcSMichael Lange   const char    *extFluent    = ".cas";
4631cc2f8f65SMatthew G. Knepley   const char    *extHDF5      = ".h5";
4632707dd687SMichael Lange   const char    *extMed       = ".med";
4633f2801cd6SMatthew G. Knepley   const char    *extPLY       = ".ply";
4634c1cad2e7SMatthew G. Knepley   const char    *extEGADSLite = ".egadslite";
4635c1cad2e7SMatthew G. Knepley   const char    *extEGADS     = ".egads";
4636c1cad2e7SMatthew G. Knepley   const char    *extIGES      = ".igs";
4637c1cad2e7SMatthew G. Knepley   const char    *extSTEP      = ".stp";
46388ca92349SMatthew G. Knepley   const char    *extCV        = ".dat";
4639ca522641SMatthew G. Knepley   size_t         len;
4640c1cad2e7SMatthew G. Knepley   PetscBool      isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV;
4641ca522641SMatthew G. Knepley   PetscMPIInt    rank;
4642ca522641SMatthew G. Knepley 
4643ca522641SMatthew G. Knepley   PetscFunctionBegin;
46445d80c0bfSVaclav Hapla   PetscValidCharPointer(filename, 2);
46450d862eaeSPierre Jolivet   if (plexname) PetscValidCharPointer(plexname, 3);
4646cd7e8a5eSksagiyam   PetscValidPointer(dm, 5);
46479566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
46489566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile,0,0,0,0));
46499566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
46509566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
465128b400f6SJacob Faibussowitsch   PetscCheck(len,comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
46529566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-4)],  extGmsh,      4, &isGmsh));
46539566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-5)],  extGmsh2,     5, &isGmsh2));
46549566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-5)],  extGmsh4,     5, &isGmsh4));
46559566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-5)],  extCGNS,      5, &isCGNS));
46569566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-4)],  extExodus,    4, &isExodus));
465786a0adb1SJed Brown   if (!isExodus) {
46589566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(&filename[PetscMax(0,len-2)],  extExodus_e,    2, &isExodus));
465986a0adb1SJed Brown   }
46609566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-4)],  extGenesis,   4, &isGenesis));
46619566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-4)],  extFluent,    4, &isFluent));
46629566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-3)],  extHDF5,      3, &isHDF5));
46639566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-4)],  extMed,       4, &isMed));
46649566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-4)],  extPLY,       4, &isPLY));
46659566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-10)], extEGADSLite, 10, &isEGADSLite));
46669566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-6)],  extEGADS,     6, &isEGADS));
46679566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-4)],  extIGES,      4, &isIGES));
46689566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-4)],  extSTEP,      4, &isSTEP));
46699566063dSJacob Faibussowitsch   PetscCall(PetscStrncmp(&filename[PetscMax(0,len-4)],  extCV,        4, &isCV));
4670de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
46719566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
4672ca522641SMatthew G. Knepley   } else if (isCGNS) {
46739566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
467490c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
46759566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
46762f0bd6dcSMichael Lange   } else if (isFluent) {
46779566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
4678cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
46799c48423bSVaclav Hapla     PetscBool      load_hdf5_xdmf = PETSC_FALSE;
4680cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
4681cc2f8f65SMatthew G. Knepley 
468243b242b4SVaclav 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 */
46839566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(&filename[PetscMax(0,len-8)], ".xdmf",  5, &load_hdf5_xdmf));
46849566063dSJacob Faibussowitsch     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &load_hdf5_xdmf, NULL));
46859566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
46869566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
46879566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
46889566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
46899566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
46909566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
4691cd7e8a5eSksagiyam 
46929566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
46939566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
46949566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
46959566063dSJacob Faibussowitsch     if (load_hdf5_xdmf) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
46969566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
46979566063dSJacob Faibussowitsch     if (load_hdf5_xdmf) PetscCall(PetscViewerPopFormat(viewer));
46989566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
46995fd9971aSMatthew G. Knepley 
47005fd9971aSMatthew G. Knepley     if (interpolate) {
47015fd9971aSMatthew G. Knepley       DM idm;
47025fd9971aSMatthew G. Knepley 
47039566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
47049566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
47055fd9971aSMatthew G. Knepley       *dm  = idm;
47065fd9971aSMatthew G. Knepley     }
4707707dd687SMichael Lange   } else if (isMed) {
47089566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateMedFromFile(comm, filename, interpolate, dm));
4709f2801cd6SMatthew G. Knepley   } else if (isPLY) {
47109566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
4711c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
47129566063dSJacob Faibussowitsch     if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm));
47139566063dSJacob Faibussowitsch     else             PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm));
47147bee2925SMatthew Knepley     if (!interpolate) {
47157bee2925SMatthew Knepley       DM udm;
47167bee2925SMatthew Knepley 
47179566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
47189566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
47197bee2925SMatthew Knepley       *dm  = udm;
47207bee2925SMatthew Knepley     }
47218ca92349SMatthew G. Knepley   } else if (isCV) {
47229566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
472398921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
47249566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
47259566063dSJacob Faibussowitsch   if (len) PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
47269566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile,0,0,0,0));
4727ca522641SMatthew G. Knepley   PetscFunctionReturn(0);
4728ca522641SMatthew G. Knepley }
4729