1552f7358SJed Brown #define PETSCDM_DLL 2af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I "petscdmplex.h" I*/ 3cc4c1da9SBarry Smith #include <petsc/private/hashseti.h> 40c312b8eSJed Brown #include <petscsf.h> 5cc4c1da9SBarry Smith #include <petscdmplextransform.h> /*I "petscdmplextransform.h" I*/ 69f6c5813SMatthew G. Knepley #include <petscdmlabelephemeral.h> 7b7f5c055SJed Brown #include <petsc/private/kernels/blockmatmult.h> 8b7f5c055SJed Brown #include <petsc/private/kernels/blockinvert.h> 9552f7358SJed Brown 10d0812dedSMatthew G. Knepley #ifdef PETSC_HAVE_UNISTD_H 11d0812dedSMatthew G. Knepley #include <unistd.h> 12d0812dedSMatthew G. Knepley #endif 13d0812dedSMatthew G. Knepley #include <errno.h> 14d0812dedSMatthew G. Knepley 15708be2fdSJed Brown PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_CreateFromOptions, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList; 1658cd63d5SVaclav Hapla 179318fe57SMatthew G. Knepley /* External function declarations here */ 189318fe57SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm); 199318fe57SMatthew G. Knepley 20e600fa54SMatthew G. Knepley /* This copies internal things in the Plex structure that we generally want when making a new, related Plex */ 21d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, PetscBool copyOverlap, DM dmout) 22d71ae5a4SJacob Faibussowitsch { 234fb89dddSMatthew G. Knepley const PetscReal *maxCell, *Lstart, *L; 2412a88998SMatthew G. Knepley VecType vecType; 2512a88998SMatthew G. Knepley MatType matType; 265962854dSMatthew G. Knepley PetscBool dist, useCeed; 27adc21957SMatthew G. Knepley DMReorderDefaultFlag reorder; 28e600fa54SMatthew G. Knepley 29e600fa54SMatthew G. Knepley PetscFunctionBegin; 3012a88998SMatthew G. Knepley PetscCall(DMGetVecType(dmin, &vecType)); 3112a88998SMatthew G. Knepley PetscCall(DMSetVecType(dmout, vecType)); 3212a88998SMatthew G. Knepley PetscCall(DMGetMatType(dmin, &matType)); 3312a88998SMatthew G. Knepley PetscCall(DMSetMatType(dmout, matType)); 34e600fa54SMatthew G. Knepley if (copyPeriodicity) { 354fb89dddSMatthew G. Knepley PetscCall(DMGetPeriodicity(dmin, &maxCell, &Lstart, &L)); 364fb89dddSMatthew G. Knepley PetscCall(DMSetPeriodicity(dmout, maxCell, Lstart, L)); 37e600fa54SMatthew G. Knepley } 389566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeGetDefault(dmin, &dist)); 399566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeSetDefault(dmout, dist)); 406bc1bd01Sksagiyam PetscCall(DMPlexReorderGetDefault(dmin, &reorder)); 416bc1bd01Sksagiyam PetscCall(DMPlexReorderSetDefault(dmout, reorder)); 425962854dSMatthew G. Knepley PetscCall(DMPlexGetUseCeed(dmin, &useCeed)); 435962854dSMatthew G. Knepley PetscCall(DMPlexSetUseCeed(dmout, useCeed)); 44e600fa54SMatthew G. Knepley ((DM_Plex *)dmout->data)->useHashLocation = ((DM_Plex *)dmin->data)->useHashLocation; 455962854dSMatthew G. Knepley ((DM_Plex *)dmout->data)->printSetValues = ((DM_Plex *)dmin->data)->printSetValues; 465962854dSMatthew G. Knepley ((DM_Plex *)dmout->data)->printFEM = ((DM_Plex *)dmin->data)->printFEM; 475962854dSMatthew G. Knepley ((DM_Plex *)dmout->data)->printFVM = ((DM_Plex *)dmin->data)->printFVM; 485962854dSMatthew G. Knepley ((DM_Plex *)dmout->data)->printL2 = ((DM_Plex *)dmin->data)->printL2; 495962854dSMatthew G. Knepley ((DM_Plex *)dmout->data)->printLocate = ((DM_Plex *)dmin->data)->printLocate; 505962854dSMatthew G. Knepley ((DM_Plex *)dmout->data)->printTol = ((DM_Plex *)dmin->data)->printTol; 511baa6e33SBarry Smith if (copyOverlap) PetscCall(DMPlexSetOverlap_Plex(dmout, dmin, 0)); 523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 53e600fa54SMatthew G. Knepley } 54e600fa54SMatthew G. Knepley 559318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm 569318fe57SMatthew G. Knepley - Share the DM_Plex structure 579318fe57SMatthew G. Knepley - Share the coordinates 589318fe57SMatthew G. Knepley - Share the SF 599318fe57SMatthew G. Knepley */ 60d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReplace_Internal(DM dm, DM *ndm) 61d71ae5a4SJacob Faibussowitsch { 629318fe57SMatthew G. Knepley PetscSF sf; 639318fe57SMatthew G. Knepley DM dmNew = *ndm, coordDM, coarseDM; 649318fe57SMatthew G. Knepley Vec coords; 654fb89dddSMatthew G. Knepley const PetscReal *maxCell, *Lstart, *L; 669318fe57SMatthew G. Knepley PetscInt dim, cdim; 679318fe57SMatthew G. Knepley 689318fe57SMatthew G. Knepley PetscFunctionBegin; 699318fe57SMatthew G. Knepley if (dm == dmNew) { 709566063dSJacob Faibussowitsch PetscCall(DMDestroy(ndm)); 713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 729318fe57SMatthew G. Knepley } 739318fe57SMatthew G. Knepley dm->setupcalled = dmNew->setupcalled; 74d0812dedSMatthew G. Knepley if (!dm->hdr.name) { 75d0812dedSMatthew G. Knepley const char *name; 76d0812dedSMatthew G. Knepley 77d0812dedSMatthew G. Knepley PetscCall(PetscObjectGetName((PetscObject)*ndm, &name)); 78d0812dedSMatthew G. Knepley PetscCall(PetscObjectSetName((PetscObject)dm, name)); 79d0812dedSMatthew G. Knepley } 809566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dmNew, &dim)); 819566063dSJacob Faibussowitsch PetscCall(DMSetDimension(dm, dim)); 829566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dmNew, &cdim)); 839566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(dm, cdim)); 849566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dmNew, &sf)); 859566063dSJacob Faibussowitsch PetscCall(DMSetPointSF(dm, sf)); 869566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dmNew, &coordDM)); 879566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dmNew, &coords)); 889566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDM(dm, coordDM)); 899566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, coords)); 906858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(dmNew, &coordDM)); 916858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinatesLocal(dmNew, &coords)); 926858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinateDM(dm, coordDM)); 936858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinatesLocal(dm, coords)); 949318fe57SMatthew G. Knepley /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */ 956858538eSMatthew G. Knepley PetscCall(DMFieldDestroy(&dm->coordinates[0].field)); 966858538eSMatthew G. Knepley dm->coordinates[0].field = dmNew->coordinates[0].field; 9761a622f3SMatthew G. Knepley ((DM_Plex *)dmNew->data)->coordFunc = ((DM_Plex *)dm->data)->coordFunc; 984fb89dddSMatthew G. Knepley PetscCall(DMGetPeriodicity(dmNew, &maxCell, &Lstart, &L)); 994fb89dddSMatthew G. Knepley PetscCall(DMSetPeriodicity(dm, maxCell, Lstart, L)); 1006925d1c4SMatthew G. Knepley PetscCall(DMPlexGetGlobalToNaturalSF(dmNew, &sf)); 1016925d1c4SMatthew G. Knepley PetscCall(DMPlexSetGlobalToNaturalSF(dm, sf)); 1029566063dSJacob Faibussowitsch PetscCall(DMDestroy_Plex(dm)); 1039566063dSJacob Faibussowitsch PetscCall(DMInitialize_Plex(dm)); 1049318fe57SMatthew G. Knepley dm->data = dmNew->data; 1059318fe57SMatthew G. Knepley ((DM_Plex *)dmNew->data)->refct++; 1061fca310dSJames Wright { 1071fca310dSJames Wright PetscInt num_face_sfs; 1081fca310dSJames Wright const PetscSF *sfs; 1091fca310dSJames Wright PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, &sfs)); 1101fca310dSJames Wright PetscCall(DMPlexSetIsoperiodicFaceSF(dm, num_face_sfs, (PetscSF *)sfs)); // for the compose function effect on dm 1111fca310dSJames Wright } 1129566063dSJacob Faibussowitsch PetscCall(DMDestroyLabelLinkList_Internal(dm)); 1139566063dSJacob Faibussowitsch PetscCall(DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL)); 1149566063dSJacob Faibussowitsch PetscCall(DMGetCoarseDM(dmNew, &coarseDM)); 1159566063dSJacob Faibussowitsch PetscCall(DMSetCoarseDM(dm, coarseDM)); 1169566063dSJacob Faibussowitsch PetscCall(DMDestroy(ndm)); 1173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1189318fe57SMatthew G. Knepley } 1199318fe57SMatthew G. Knepley 1209318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew 1219318fe57SMatthew G. Knepley - Swap the DM_Plex structure 1229318fe57SMatthew G. Knepley - Swap the coordinates 1239318fe57SMatthew G. Knepley - Swap the point PetscSF 1249318fe57SMatthew G. Knepley */ 125d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB) 126d71ae5a4SJacob Faibussowitsch { 1279318fe57SMatthew G. Knepley DM coordDMA, coordDMB; 1289318fe57SMatthew G. Knepley Vec coordsA, coordsB; 1299318fe57SMatthew G. Knepley PetscSF sfA, sfB; 1309318fe57SMatthew G. Knepley DMField fieldTmp; 1319318fe57SMatthew G. Knepley void *tmp; 1329318fe57SMatthew G. Knepley DMLabelLink listTmp; 1339318fe57SMatthew G. Knepley DMLabel depthTmp; 1349318fe57SMatthew G. Knepley PetscInt tmpI; 1359318fe57SMatthew G. Knepley 1369318fe57SMatthew G. Knepley PetscFunctionBegin; 1373ba16761SJacob Faibussowitsch if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS); 1389566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dmA, &sfA)); 1399566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dmB, &sfB)); 1409566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)sfA)); 1419566063dSJacob Faibussowitsch PetscCall(DMSetPointSF(dmA, sfB)); 1429566063dSJacob Faibussowitsch PetscCall(DMSetPointSF(dmB, sfA)); 1439566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)sfA)); 1449318fe57SMatthew G. Knepley 1459566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dmA, &coordDMA)); 1469566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dmB, &coordDMB)); 1479566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)coordDMA)); 1489566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDM(dmA, coordDMB)); 1499566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDM(dmB, coordDMA)); 1509566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)coordDMA)); 1519318fe57SMatthew G. Knepley 1529566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dmA, &coordsA)); 1539566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dmB, &coordsB)); 1549566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)coordsA)); 1559566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dmA, coordsB)); 1569566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dmB, coordsA)); 1579566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)coordsA)); 1589318fe57SMatthew G. Knepley 1596858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(dmA, &coordDMA)); 1606858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(dmB, &coordDMB)); 1616858538eSMatthew G. Knepley PetscCall(PetscObjectReference((PetscObject)coordDMA)); 1626858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinateDM(dmA, coordDMB)); 1636858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinateDM(dmB, coordDMA)); 1646858538eSMatthew G. Knepley PetscCall(PetscObjectDereference((PetscObject)coordDMA)); 1656858538eSMatthew G. Knepley 1666858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinatesLocal(dmA, &coordsA)); 1676858538eSMatthew G. Knepley PetscCall(DMGetCellCoordinatesLocal(dmB, &coordsB)); 1686858538eSMatthew G. Knepley PetscCall(PetscObjectReference((PetscObject)coordsA)); 1696858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinatesLocal(dmA, coordsB)); 1706858538eSMatthew G. Knepley PetscCall(DMSetCellCoordinatesLocal(dmB, coordsA)); 1716858538eSMatthew G. Knepley PetscCall(PetscObjectDereference((PetscObject)coordsA)); 1726858538eSMatthew G. Knepley 1736858538eSMatthew G. Knepley fieldTmp = dmA->coordinates[0].field; 1746858538eSMatthew G. Knepley dmA->coordinates[0].field = dmB->coordinates[0].field; 1756858538eSMatthew G. Knepley dmB->coordinates[0].field = fieldTmp; 1766858538eSMatthew G. Knepley fieldTmp = dmA->coordinates[1].field; 1776858538eSMatthew G. Knepley dmA->coordinates[1].field = dmB->coordinates[1].field; 1786858538eSMatthew G. Knepley dmB->coordinates[1].field = fieldTmp; 1799318fe57SMatthew G. Knepley tmp = dmA->data; 1809318fe57SMatthew G. Knepley dmA->data = dmB->data; 1819318fe57SMatthew G. Knepley dmB->data = tmp; 1829318fe57SMatthew G. Knepley listTmp = dmA->labels; 1839318fe57SMatthew G. Knepley dmA->labels = dmB->labels; 1849318fe57SMatthew G. Knepley dmB->labels = listTmp; 1859318fe57SMatthew G. Knepley depthTmp = dmA->depthLabel; 1869318fe57SMatthew G. Knepley dmA->depthLabel = dmB->depthLabel; 1879318fe57SMatthew G. Knepley dmB->depthLabel = depthTmp; 1889318fe57SMatthew G. Knepley depthTmp = dmA->celltypeLabel; 1899318fe57SMatthew G. Knepley dmA->celltypeLabel = dmB->celltypeLabel; 1909318fe57SMatthew G. Knepley dmB->celltypeLabel = depthTmp; 1919318fe57SMatthew G. Knepley tmpI = dmA->levelup; 1929318fe57SMatthew G. Knepley dmA->levelup = dmB->levelup; 1939318fe57SMatthew G. Knepley dmB->levelup = tmpI; 1943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1959318fe57SMatthew G. Knepley } 1969318fe57SMatthew G. Knepley 1973431e603SJed Brown PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm) 198d71ae5a4SJacob Faibussowitsch { 1999318fe57SMatthew G. Knepley DM idm; 2009318fe57SMatthew G. Knepley 2019318fe57SMatthew G. Knepley PetscFunctionBegin; 2029566063dSJacob Faibussowitsch PetscCall(DMPlexInterpolate(dm, &idm)); 2039566063dSJacob Faibussowitsch PetscCall(DMPlexCopyCoordinates(dm, idm)); 20469d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &idm)); 2053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2069318fe57SMatthew G. Knepley } 2079318fe57SMatthew G. Knepley 2089318fe57SMatthew G. Knepley /*@C 2099318fe57SMatthew G. Knepley DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates 2109318fe57SMatthew G. Knepley 21120f4b53cSBarry Smith Collective 2129318fe57SMatthew G. Knepley 2139318fe57SMatthew G. Knepley Input Parameters: 21460225df5SJacob Faibussowitsch + dm - The `DMPLEX` 21520f4b53cSBarry Smith . degree - The degree of the finite element or `PETSC_DECIDE` 216e44f6aebSMatthew G. Knepley . project - Flag to project current coordinates into the space 2179318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface 2189318fe57SMatthew G. Knepley 2199318fe57SMatthew G. Knepley Level: advanced 2209318fe57SMatthew G. Knepley 2211cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `PetscPointFunc`, `PetscFECreateLagrange()`, `DMGetCoordinateDM()` 2229318fe57SMatthew G. Knepley @*/ 223e44f6aebSMatthew G. Knepley PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscBool project, PetscPointFunc coordFunc) 224d71ae5a4SJacob Faibussowitsch { 2259318fe57SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 226e44f6aebSMatthew G. Knepley PetscFE fe = NULL; 2279318fe57SMatthew G. Knepley DM cdm; 2281df12153SMatthew G. Knepley PetscInt dim, dE, qorder, height; 2299318fe57SMatthew G. Knepley 230e44f6aebSMatthew G. Knepley PetscFunctionBegin; 2319566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 2329566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dE)); 2339318fe57SMatthew G. Knepley qorder = degree; 234e44f6aebSMatthew G. Knepley PetscCall(DMGetCoordinateDM(dm, &cdm)); 235d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)cdm); 236dc431b0cSMatthew G. Knepley PetscCall(PetscOptionsBoundedInt("-default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0)); 237d0609cedSBarry Smith PetscOptionsEnd(); 2381df12153SMatthew G. Knepley PetscCall(DMPlexGetVTKCellHeight(dm, &height)); 239e44f6aebSMatthew G. Knepley if (degree >= 0) { 240e44f6aebSMatthew G. Knepley DMPolytopeType ct = DM_POLYTOPE_UNKNOWN; 241e44f6aebSMatthew G. Knepley PetscInt cStart, cEnd, gct; 242dc431b0cSMatthew G. Knepley 2431df12153SMatthew G. Knepley PetscCall(DMPlexGetHeightStratum(dm, height, &cStart, &cEnd)); 244dc431b0cSMatthew G. Knepley if (cEnd > cStart) PetscCall(DMPlexGetCellType(dm, cStart, &ct)); 245e44f6aebSMatthew G. Knepley gct = (PetscInt)ct; 246e44f6aebSMatthew G. Knepley PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &gct, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm))); 247e44f6aebSMatthew G. Knepley ct = (DMPolytopeType)gct; 248e44f6aebSMatthew G. Knepley // Work around current bug in PetscDualSpaceSetUp_Lagrange() 249e44f6aebSMatthew G. Knepley // Can be seen in plex_tutorials-ex10_1 250e44f6aebSMatthew G. Knepley if (ct != DM_POLYTOPE_SEG_PRISM_TENSOR && ct != DM_POLYTOPE_TRI_PRISM_TENSOR && ct != DM_POLYTOPE_QUAD_PRISM_TENSOR) PetscCall(PetscFECreateLagrangeByCell(PETSC_COMM_SELF, dim, dE, ct, degree, qorder, &fe)); 2514f9ab2b4SJed Brown } 252e44f6aebSMatthew G. Knepley PetscCall(DMSetCoordinateDisc(dm, fe, project)); 2539566063dSJacob Faibussowitsch PetscCall(PetscFEDestroy(&fe)); 2549318fe57SMatthew G. Knepley mesh->coordFunc = coordFunc; 2553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2569318fe57SMatthew G. Knepley } 2579318fe57SMatthew G. Knepley 2581df5d5c5SMatthew G. Knepley /*@ 2591df5d5c5SMatthew G. Knepley DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement. 2601df5d5c5SMatthew G. Knepley 261d083f849SBarry Smith Collective 2621df5d5c5SMatthew G. Knepley 2631df5d5c5SMatthew G. Knepley Input Parameters: 264a1cb98faSBarry Smith + comm - The communicator for the `DM` object 2651df5d5c5SMatthew G. Knepley . dim - The spatial dimension 2661df5d5c5SMatthew G. Knepley . simplex - Flag for simplicial cells, otherwise they are tensor product cells 2671df5d5c5SMatthew G. Knepley . interpolate - Flag to create intermediate mesh pieces (edges, faces) 2681df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell 2691df5d5c5SMatthew G. Knepley 2701df5d5c5SMatthew G. Knepley Output Parameter: 27160225df5SJacob Faibussowitsch . newdm - The `DM` object 2721df5d5c5SMatthew G. Knepley 2731df5d5c5SMatthew G. Knepley Level: beginner 2741df5d5c5SMatthew G. Knepley 2751cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetType()`, `DMCreate()` 2761df5d5c5SMatthew G. Knepley @*/ 277d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm) 278d71ae5a4SJacob Faibussowitsch { 2791df5d5c5SMatthew G. Knepley DM dm; 2801df5d5c5SMatthew G. Knepley PetscMPIInt rank; 2811df5d5c5SMatthew G. Knepley 2821df5d5c5SMatthew G. Knepley PetscFunctionBegin; 2839566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, &dm)); 2849566063dSJacob Faibussowitsch PetscCall(DMSetType(dm, DMPLEX)); 2859566063dSJacob Faibussowitsch PetscCall(DMSetDimension(dm, dim)); 28646139095SJed Brown PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0)); 2879566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 288ce78fa2fSMatthew G. Knepley switch (dim) { 289ce78fa2fSMatthew G. Knepley case 2: 2909566063dSJacob Faibussowitsch if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "triangular")); 2919566063dSJacob Faibussowitsch else PetscCall(PetscObjectSetName((PetscObject)dm, "quadrilateral")); 292ce78fa2fSMatthew G. Knepley break; 293ce78fa2fSMatthew G. Knepley case 3: 2949566063dSJacob Faibussowitsch if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "tetrahedral")); 2959566063dSJacob Faibussowitsch else PetscCall(PetscObjectSetName((PetscObject)dm, "hexahedral")); 296ce78fa2fSMatthew G. Knepley break; 297d71ae5a4SJacob Faibussowitsch default: 298d71ae5a4SJacob Faibussowitsch SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim); 299ce78fa2fSMatthew G. Knepley } 3001df5d5c5SMatthew G. Knepley if (rank) { 3011df5d5c5SMatthew G. Knepley PetscInt numPoints[2] = {0, 0}; 3029566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL)); 3031df5d5c5SMatthew G. Knepley } else { 3041df5d5c5SMatthew G. Knepley switch (dim) { 3051df5d5c5SMatthew G. Knepley case 2: 3061df5d5c5SMatthew G. Knepley if (simplex) { 3071df5d5c5SMatthew G. Knepley PetscInt numPoints[2] = {4, 2}; 3081df5d5c5SMatthew G. Knepley PetscInt coneSize[6] = {3, 3, 0, 0, 0, 0}; 3091df5d5c5SMatthew G. Knepley PetscInt cones[6] = {2, 3, 4, 5, 4, 3}; 3101df5d5c5SMatthew G. Knepley PetscInt coneOrientations[6] = {0, 0, 0, 0, 0, 0}; 3111df5d5c5SMatthew G. Knepley PetscScalar vertexCoords[8] = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5}; 3121df5d5c5SMatthew G. Knepley 3139566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 3141df5d5c5SMatthew G. Knepley } else { 3151df5d5c5SMatthew G. Knepley PetscInt numPoints[2] = {6, 2}; 3161df5d5c5SMatthew G. Knepley PetscInt coneSize[8] = {4, 4, 0, 0, 0, 0, 0, 0}; 3171df5d5c5SMatthew G. Knepley PetscInt cones[8] = {2, 3, 4, 5, 3, 6, 7, 4}; 3181df5d5c5SMatthew G. Knepley PetscInt coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 3191df5d5c5SMatthew 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}; 3201df5d5c5SMatthew G. Knepley 3219566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 3221df5d5c5SMatthew G. Knepley } 3231df5d5c5SMatthew G. Knepley break; 3241df5d5c5SMatthew G. Knepley case 3: 3251df5d5c5SMatthew G. Knepley if (simplex) { 3261df5d5c5SMatthew G. Knepley PetscInt numPoints[2] = {5, 2}; 3271df5d5c5SMatthew G. Knepley PetscInt coneSize[7] = {4, 4, 0, 0, 0, 0, 0}; 3281df5d5c5SMatthew G. Knepley PetscInt cones[8] = {4, 3, 5, 2, 5, 3, 4, 6}; 3291df5d5c5SMatthew G. Knepley PetscInt coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 3301df5d5c5SMatthew 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}; 3311df5d5c5SMatthew G. Knepley 3329566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 3331df5d5c5SMatthew G. Knepley } else { 3341df5d5c5SMatthew G. Knepley PetscInt numPoints[2] = {12, 2}; 3351df5d5c5SMatthew G. Knepley PetscInt coneSize[14] = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 3361df5d5c5SMatthew G. Knepley PetscInt cones[16] = {2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 10, 11, 7, 12, 13, 8}; 3371df5d5c5SMatthew G. Knepley PetscInt coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 3389371c9d4SSatish Balay PetscScalar vertexCoords[36] = {-1.0, -0.5, -0.5, -1.0, 0.5, -0.5, 0.0, 0.5, -0.5, 0.0, -0.5, -0.5, -1.0, -0.5, 0.5, 0.0, -0.5, 0.5, 0.0, 0.5, 0.5, -1.0, 0.5, 0.5, 1.0, 0.5, -0.5, 1.0, -0.5, -0.5, 1.0, -0.5, 0.5, 1.0, 0.5, 0.5}; 3391df5d5c5SMatthew G. Knepley 3409566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 3411df5d5c5SMatthew G. Knepley } 3421df5d5c5SMatthew G. Knepley break; 343d71ae5a4SJacob Faibussowitsch default: 344d71ae5a4SJacob Faibussowitsch SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim); 3451df5d5c5SMatthew G. Knepley } 3461df5d5c5SMatthew G. Knepley } 34746139095SJed Brown PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0)); 3481df5d5c5SMatthew G. Knepley *newdm = dm; 3491df5d5c5SMatthew G. Knepley if (refinementLimit > 0.0) { 3501df5d5c5SMatthew G. Knepley DM rdm; 3511df5d5c5SMatthew G. Knepley const char *name; 3521df5d5c5SMatthew G. Knepley 3539566063dSJacob Faibussowitsch PetscCall(DMPlexSetRefinementUniform(*newdm, PETSC_FALSE)); 3549566063dSJacob Faibussowitsch PetscCall(DMPlexSetRefinementLimit(*newdm, refinementLimit)); 3559566063dSJacob Faibussowitsch PetscCall(DMRefine(*newdm, comm, &rdm)); 3569566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)*newdm, &name)); 3579566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)rdm, name)); 3589566063dSJacob Faibussowitsch PetscCall(DMDestroy(newdm)); 3591df5d5c5SMatthew G. Knepley *newdm = rdm; 3601df5d5c5SMatthew G. Knepley } 3611df5d5c5SMatthew G. Knepley if (interpolate) { 3625fd9971aSMatthew G. Knepley DM idm; 3631df5d5c5SMatthew G. Knepley 3649566063dSJacob Faibussowitsch PetscCall(DMPlexInterpolate(*newdm, &idm)); 3659566063dSJacob Faibussowitsch PetscCall(DMDestroy(newdm)); 3661df5d5c5SMatthew G. Knepley *newdm = idm; 3671df5d5c5SMatthew G. Knepley } 3683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3691df5d5c5SMatthew G. Knepley } 3701df5d5c5SMatthew G. Knepley 371d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[]) 372d71ae5a4SJacob Faibussowitsch { 3739318fe57SMatthew G. Knepley const PetscInt numVertices = 2; 3749318fe57SMatthew G. Knepley PetscInt markerRight = 1; 3759318fe57SMatthew G. Knepley PetscInt markerLeft = 1; 3769318fe57SMatthew G. Knepley PetscBool markerSeparate = PETSC_FALSE; 3779318fe57SMatthew G. Knepley Vec coordinates; 3789318fe57SMatthew G. Knepley PetscSection coordSection; 3799318fe57SMatthew G. Knepley PetscScalar *coords; 3809318fe57SMatthew G. Knepley PetscInt coordSize; 3819318fe57SMatthew G. Knepley PetscMPIInt rank; 3829318fe57SMatthew G. Knepley PetscInt cdim = 1, v; 383552f7358SJed Brown 3849318fe57SMatthew G. Knepley PetscFunctionBegin; 3859566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL)); 3869318fe57SMatthew G. Knepley if (markerSeparate) { 3879318fe57SMatthew G. Knepley markerRight = 2; 3889318fe57SMatthew G. Knepley markerLeft = 1; 3899318fe57SMatthew G. Knepley } 3909566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 391c5853193SPierre Jolivet if (rank == 0) { 3929566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dm, 0, numVertices)); 3939566063dSJacob Faibussowitsch PetscCall(DMSetUp(dm)); /* Allocate space for cones */ 3949566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", 0, markerLeft)); 3959566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", 1, markerRight)); 3969318fe57SMatthew G. Knepley } 3979566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dm)); 3989566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dm)); 3999318fe57SMatthew G. Knepley /* Build coordinates */ 4009566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(dm, cdim)); 4019566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 4029566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 4039566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, 0, numVertices)); 4049566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim)); 4059318fe57SMatthew G. Knepley for (v = 0; v < numVertices; ++v) { 4069566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, cdim)); 4079566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim)); 4089318fe57SMatthew G. Knepley } 4099566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 4109566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 4119566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates)); 4129566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 4139566063dSJacob Faibussowitsch PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 4149566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(coordinates, cdim)); 4159566063dSJacob Faibussowitsch PetscCall(VecSetType(coordinates, VECSTANDARD)); 4169566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 4179318fe57SMatthew G. Knepley coords[0] = lower[0]; 4189318fe57SMatthew G. Knepley coords[1] = upper[0]; 4199566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 4209566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, coordinates)); 4219566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 4223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4239318fe57SMatthew G. Knepley } 42426492d91SMatthew G. Knepley 425d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[]) 426d71ae5a4SJacob Faibussowitsch { 4271df21d24SMatthew G. Knepley const PetscInt numVertices = (edges[0] + 1) * (edges[1] + 1); 4281df21d24SMatthew G. Knepley const PetscInt numEdges = edges[0] * (edges[1] + 1) + (edges[0] + 1) * edges[1]; 429552f7358SJed Brown PetscInt markerTop = 1; 430552f7358SJed Brown PetscInt markerBottom = 1; 431552f7358SJed Brown PetscInt markerRight = 1; 432552f7358SJed Brown PetscInt markerLeft = 1; 433552f7358SJed Brown PetscBool markerSeparate = PETSC_FALSE; 434552f7358SJed Brown Vec coordinates; 435552f7358SJed Brown PetscSection coordSection; 436552f7358SJed Brown PetscScalar *coords; 437552f7358SJed Brown PetscInt coordSize; 438552f7358SJed Brown PetscMPIInt rank; 439552f7358SJed Brown PetscInt v, vx, vy; 440552f7358SJed Brown 441552f7358SJed Brown PetscFunctionBegin; 4429566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL)); 443552f7358SJed Brown if (markerSeparate) { 4441df21d24SMatthew G. Knepley markerTop = 3; 4451df21d24SMatthew G. Knepley markerBottom = 1; 4461df21d24SMatthew G. Knepley markerRight = 2; 4471df21d24SMatthew G. Knepley markerLeft = 4; 448552f7358SJed Brown } 4499566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 450dd400576SPatrick Sanan if (rank == 0) { 451552f7358SJed Brown PetscInt e, ex, ey; 452552f7358SJed Brown 4539566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dm, 0, numEdges + numVertices)); 45448a46eb9SPierre Jolivet for (e = 0; e < numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2)); 4559566063dSJacob Faibussowitsch PetscCall(DMSetUp(dm)); /* Allocate space for cones */ 456552f7358SJed Brown for (vx = 0; vx <= edges[0]; vx++) { 457552f7358SJed Brown for (ey = 0; ey < edges[1]; ey++) { 458552f7358SJed Brown PetscInt edge = vx * edges[1] + ey + edges[0] * (edges[1] + 1); 459552f7358SJed Brown PetscInt vertex = ey * (edges[0] + 1) + vx + numEdges; 460da80777bSKarl Rupp PetscInt cone[2]; 461552f7358SJed Brown 4629371c9d4SSatish Balay cone[0] = vertex; 4639371c9d4SSatish Balay cone[1] = vertex + edges[0] + 1; 4649566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, edge, cone)); 465552f7358SJed Brown if (vx == edges[0]) { 4669566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight)); 4679566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight)); 468552f7358SJed Brown if (ey == edges[1] - 1) { 4699566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight)); 4709566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerRight)); 471552f7358SJed Brown } 472552f7358SJed Brown } else if (vx == 0) { 4739566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft)); 4749566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft)); 475552f7358SJed Brown if (ey == edges[1] - 1) { 4769566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft)); 4779566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft)); 478552f7358SJed Brown } 479552f7358SJed Brown } 480552f7358SJed Brown } 481552f7358SJed Brown } 482552f7358SJed Brown for (vy = 0; vy <= edges[1]; vy++) { 483552f7358SJed Brown for (ex = 0; ex < edges[0]; ex++) { 484552f7358SJed Brown PetscInt edge = vy * edges[0] + ex; 485552f7358SJed Brown PetscInt vertex = vy * (edges[0] + 1) + ex + numEdges; 486da80777bSKarl Rupp PetscInt cone[2]; 487552f7358SJed Brown 4889371c9d4SSatish Balay cone[0] = vertex; 4899371c9d4SSatish Balay cone[1] = vertex + 1; 4909566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, edge, cone)); 491552f7358SJed Brown if (vy == edges[1]) { 4929566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop)); 4939566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop)); 494552f7358SJed Brown if (ex == edges[0] - 1) { 4959566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop)); 4969566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerTop)); 497552f7358SJed Brown } 498552f7358SJed Brown } else if (vy == 0) { 4999566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom)); 5009566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom)); 501552f7358SJed Brown if (ex == edges[0] - 1) { 5029566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom)); 5039566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom)); 504552f7358SJed Brown } 505552f7358SJed Brown } 506552f7358SJed Brown } 507552f7358SJed Brown } 508552f7358SJed Brown } 5099566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dm)); 5109566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dm)); 511552f7358SJed Brown /* Build coordinates */ 5129566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(dm, 2)); 5139566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 5149566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 5159566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices)); 5169566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2)); 517552f7358SJed Brown for (v = numEdges; v < numEdges + numVertices; ++v) { 5189566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, 2)); 5199566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2)); 520552f7358SJed Brown } 5219566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 5229566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 5239566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates)); 5249566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 5259566063dSJacob Faibussowitsch PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 5269566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(coordinates, 2)); 5279566063dSJacob Faibussowitsch PetscCall(VecSetType(coordinates, VECSTANDARD)); 5289566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 529552f7358SJed Brown for (vy = 0; vy <= edges[1]; ++vy) { 530552f7358SJed Brown for (vx = 0; vx <= edges[0]; ++vx) { 531552f7358SJed Brown coords[(vy * (edges[0] + 1) + vx) * 2 + 0] = lower[0] + ((upper[0] - lower[0]) / edges[0]) * vx; 532552f7358SJed Brown coords[(vy * (edges[0] + 1) + vx) * 2 + 1] = lower[1] + ((upper[1] - lower[1]) / edges[1]) * vy; 533552f7358SJed Brown } 534552f7358SJed Brown } 5359566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 5369566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, coordinates)); 5379566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 5383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 539552f7358SJed Brown } 540552f7358SJed Brown 541d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[]) 542d71ae5a4SJacob Faibussowitsch { 5439e8abbc3SMichael Lange PetscInt vertices[3], numVertices; 5447b59f5a9SMichael Lange PetscInt numFaces = 2 * faces[0] * faces[1] + 2 * faces[1] * faces[2] + 2 * faces[0] * faces[2]; 545c2df9bbfSMatthew G. Knepley PetscInt markerTop = 1; 546c2df9bbfSMatthew G. Knepley PetscInt markerBottom = 1; 547c2df9bbfSMatthew G. Knepley PetscInt markerFront = 1; 548c2df9bbfSMatthew G. Knepley PetscInt markerBack = 1; 549c2df9bbfSMatthew G. Knepley PetscInt markerRight = 1; 550c2df9bbfSMatthew G. Knepley PetscInt markerLeft = 1; 551c2df9bbfSMatthew G. Knepley PetscBool markerSeparate = PETSC_FALSE; 552552f7358SJed Brown Vec coordinates; 553552f7358SJed Brown PetscSection coordSection; 554552f7358SJed Brown PetscScalar *coords; 555552f7358SJed Brown PetscInt coordSize; 556552f7358SJed Brown PetscMPIInt rank; 557552f7358SJed Brown PetscInt v, vx, vy, vz; 5587b59f5a9SMichael Lange PetscInt voffset, iface = 0, cone[4]; 559552f7358SJed Brown 560552f7358SJed Brown PetscFunctionBegin; 5611dca8a05SBarry Smith PetscCheck(faces[0] >= 1 && faces[1] >= 1 && faces[2] >= 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Must have at least 1 face per side"); 5629566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 563c2df9bbfSMatthew G. Knepley PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL)); 564c2df9bbfSMatthew G. Knepley if (markerSeparate) { 565c2df9bbfSMatthew G. Knepley markerBottom = 1; 566c2df9bbfSMatthew G. Knepley markerTop = 2; 567c2df9bbfSMatthew G. Knepley markerFront = 3; 568c2df9bbfSMatthew G. Knepley markerBack = 4; 569c2df9bbfSMatthew G. Knepley markerRight = 5; 570c2df9bbfSMatthew G. Knepley markerLeft = 6; 571c2df9bbfSMatthew G. Knepley } 5729371c9d4SSatish Balay vertices[0] = faces[0] + 1; 5739371c9d4SSatish Balay vertices[1] = faces[1] + 1; 5749371c9d4SSatish Balay vertices[2] = faces[2] + 1; 5759e8abbc3SMichael Lange numVertices = vertices[0] * vertices[1] * vertices[2]; 576dd400576SPatrick Sanan if (rank == 0) { 577552f7358SJed Brown PetscInt f; 578552f7358SJed Brown 5799566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dm, 0, numFaces + numVertices)); 58048a46eb9SPierre Jolivet for (f = 0; f < numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4)); 5819566063dSJacob Faibussowitsch PetscCall(DMSetUp(dm)); /* Allocate space for cones */ 5827b59f5a9SMichael Lange 5837b59f5a9SMichael Lange /* Side 0 (Top) */ 5847b59f5a9SMichael Lange for (vy = 0; vy < faces[1]; vy++) { 5857b59f5a9SMichael Lange for (vx = 0; vx < faces[0]; vx++) { 5867b59f5a9SMichael Lange voffset = numFaces + vertices[0] * vertices[1] * (vertices[2] - 1) + vy * vertices[0] + vx; 5879371c9d4SSatish Balay cone[0] = voffset; 5889371c9d4SSatish Balay cone[1] = voffset + 1; 5899371c9d4SSatish Balay cone[2] = voffset + vertices[0] + 1; 5909371c9d4SSatish Balay cone[3] = voffset + vertices[0]; 5919566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, iface, cone)); 592c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", iface, markerTop)); 593c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerTop)); 594c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerTop)); 595c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerTop)); 596c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerTop)); 5977b59f5a9SMichael Lange iface++; 598552f7358SJed Brown } 599552f7358SJed Brown } 6007b59f5a9SMichael Lange 6017b59f5a9SMichael Lange /* Side 1 (Bottom) */ 6027b59f5a9SMichael Lange for (vy = 0; vy < faces[1]; vy++) { 6037b59f5a9SMichael Lange for (vx = 0; vx < faces[0]; vx++) { 6047b59f5a9SMichael Lange voffset = numFaces + vy * (faces[0] + 1) + vx; 6059371c9d4SSatish Balay cone[0] = voffset + 1; 6069371c9d4SSatish Balay cone[1] = voffset; 6079371c9d4SSatish Balay cone[2] = voffset + vertices[0]; 6089371c9d4SSatish Balay cone[3] = voffset + vertices[0] + 1; 6099566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, iface, cone)); 610c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", iface, markerBottom)); 611c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBottom)); 612c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBottom)); 613c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerBottom)); 614c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerBottom)); 6157b59f5a9SMichael Lange iface++; 616552f7358SJed Brown } 617552f7358SJed Brown } 6187b59f5a9SMichael Lange 6197b59f5a9SMichael Lange /* Side 2 (Front) */ 6207b59f5a9SMichael Lange for (vz = 0; vz < faces[2]; vz++) { 6217b59f5a9SMichael Lange for (vx = 0; vx < faces[0]; vx++) { 6227b59f5a9SMichael Lange voffset = numFaces + vz * vertices[0] * vertices[1] + vx; 6239371c9d4SSatish Balay cone[0] = voffset; 6249371c9d4SSatish Balay cone[1] = voffset + 1; 6259371c9d4SSatish Balay cone[2] = voffset + vertices[0] * vertices[1] + 1; 6269371c9d4SSatish Balay cone[3] = voffset + vertices[0] * vertices[1]; 6279566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, iface, cone)); 628c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", iface, markerFront)); 629c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerFront)); 630c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerFront)); 631c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerFront)); 632c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerFront)); 6337b59f5a9SMichael Lange iface++; 634552f7358SJed Brown } 6357b59f5a9SMichael Lange } 6367b59f5a9SMichael Lange 6377b59f5a9SMichael Lange /* Side 3 (Back) */ 6387b59f5a9SMichael Lange for (vz = 0; vz < faces[2]; vz++) { 6397b59f5a9SMichael Lange for (vx = 0; vx < faces[0]; vx++) { 6407b59f5a9SMichael Lange voffset = numFaces + vz * vertices[0] * vertices[1] + vertices[0] * (vertices[1] - 1) + vx; 6419371c9d4SSatish Balay cone[0] = voffset + vertices[0] * vertices[1]; 6429371c9d4SSatish Balay cone[1] = voffset + vertices[0] * vertices[1] + 1; 6439371c9d4SSatish Balay cone[2] = voffset + 1; 6449371c9d4SSatish Balay cone[3] = voffset; 6459566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, iface, cone)); 646c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", iface, markerBack)); 647c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBack)); 648c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBack)); 649c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerBack)); 650c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerBack)); 6517b59f5a9SMichael Lange iface++; 6527b59f5a9SMichael Lange } 6537b59f5a9SMichael Lange } 6547b59f5a9SMichael Lange 6557b59f5a9SMichael Lange /* Side 4 (Left) */ 6567b59f5a9SMichael Lange for (vz = 0; vz < faces[2]; vz++) { 6577b59f5a9SMichael Lange for (vy = 0; vy < faces[1]; vy++) { 6587b59f5a9SMichael Lange voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0]; 6599371c9d4SSatish Balay cone[0] = voffset; 6609371c9d4SSatish Balay cone[1] = voffset + vertices[0] * vertices[1]; 6619371c9d4SSatish Balay cone[2] = voffset + vertices[0] * vertices[1] + vertices[0]; 6629371c9d4SSatish Balay cone[3] = voffset + vertices[0]; 6639566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, iface, cone)); 664c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", iface, markerLeft)); 665c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerLeft)); 666c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerLeft)); 667c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[1] + 0, markerLeft)); 668c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerLeft)); 6697b59f5a9SMichael Lange iface++; 6707b59f5a9SMichael Lange } 6717b59f5a9SMichael Lange } 6727b59f5a9SMichael Lange 6737b59f5a9SMichael Lange /* Side 5 (Right) */ 6747b59f5a9SMichael Lange for (vz = 0; vz < faces[2]; vz++) { 6757b59f5a9SMichael Lange for (vy = 0; vy < faces[1]; vy++) { 676aab5bcd8SJed Brown voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0] + faces[0]; 6779371c9d4SSatish Balay cone[0] = voffset + vertices[0] * vertices[1]; 6789371c9d4SSatish Balay cone[1] = voffset; 6799371c9d4SSatish Balay cone[2] = voffset + vertices[0]; 6809371c9d4SSatish Balay cone[3] = voffset + vertices[0] * vertices[1] + vertices[0]; 6819566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, iface, cone)); 682c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", iface, markerRight)); 683c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerRight)); 684c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerRight)); 685c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerRight)); 686c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerRight)); 6877b59f5a9SMichael Lange iface++; 6887b59f5a9SMichael Lange } 689552f7358SJed Brown } 690552f7358SJed Brown } 6919566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dm)); 6929566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dm)); 693552f7358SJed Brown /* Build coordinates */ 6949566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(dm, 3)); 6959566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 6969566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 6979566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices)); 6989566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 3)); 699552f7358SJed Brown for (v = numFaces; v < numFaces + numVertices; ++v) { 7009566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, 3)); 7019566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 3)); 702552f7358SJed Brown } 7039566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 7049566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 7059566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates)); 7069566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 7079566063dSJacob Faibussowitsch PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 7089566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(coordinates, 3)); 7099566063dSJacob Faibussowitsch PetscCall(VecSetType(coordinates, VECSTANDARD)); 7109566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 711552f7358SJed Brown for (vz = 0; vz <= faces[2]; ++vz) { 712552f7358SJed Brown for (vy = 0; vy <= faces[1]; ++vy) { 713552f7358SJed Brown for (vx = 0; vx <= faces[0]; ++vx) { 714552f7358SJed Brown coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 0] = lower[0] + ((upper[0] - lower[0]) / faces[0]) * vx; 715552f7358SJed Brown coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 1] = lower[1] + ((upper[1] - lower[1]) / faces[1]) * vy; 716552f7358SJed Brown coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 2] = lower[2] + ((upper[2] - lower[2]) / faces[2]) * vz; 717552f7358SJed Brown } 718552f7358SJed Brown } 719552f7358SJed Brown } 7209566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 7219566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, coordinates)); 7229566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 7233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 724552f7358SJed Brown } 725552f7358SJed Brown 726d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate) 727d71ae5a4SJacob Faibussowitsch { 7289318fe57SMatthew G. Knepley PetscFunctionBegin; 7299318fe57SMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, dim, 2); 73046139095SJed Brown PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0)); 7319566063dSJacob Faibussowitsch PetscCall(DMSetDimension(dm, dim - 1)); 7329566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(dm, dim)); 7339318fe57SMatthew G. Knepley switch (dim) { 734d71ae5a4SJacob Faibussowitsch case 1: 735d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces)); 736d71ae5a4SJacob Faibussowitsch break; 737d71ae5a4SJacob Faibussowitsch case 2: 738d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces)); 739d71ae5a4SJacob Faibussowitsch break; 740d71ae5a4SJacob Faibussowitsch case 3: 741d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces)); 742d71ae5a4SJacob Faibussowitsch break; 743d71ae5a4SJacob Faibussowitsch default: 744d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Dimension not supported: %" PetscInt_FMT, dim); 7459318fe57SMatthew G. Knepley } 74646139095SJed Brown PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0)); 7479566063dSJacob Faibussowitsch if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm)); 7483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7499318fe57SMatthew G. Knepley } 7509318fe57SMatthew G. Knepley 7519318fe57SMatthew G. Knepley /*@C 7529318fe57SMatthew G. Knepley DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra). 7539318fe57SMatthew G. Knepley 7549318fe57SMatthew G. Knepley Collective 7559318fe57SMatthew G. Knepley 7569318fe57SMatthew G. Knepley Input Parameters: 757a1cb98faSBarry Smith + comm - The communicator for the `DM` object 75820f4b53cSBarry Smith . dim - The spatial dimension of the box, so the resulting mesh is has dimension `dim`-1 75920f4b53cSBarry Smith . faces - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D 76020f4b53cSBarry Smith . lower - The lower left corner, or `NULL` for (0, 0, 0) 76120f4b53cSBarry Smith . upper - The upper right corner, or `NULL` for (1, 1, 1) 7629318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces) 7639318fe57SMatthew G. Knepley 7649318fe57SMatthew G. Knepley Output Parameter: 765a1cb98faSBarry Smith . dm - The `DM` object 7669318fe57SMatthew G. Knepley 7679318fe57SMatthew G. Knepley Level: beginner 7689318fe57SMatthew G. Knepley 7691cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateBoxMesh()`, `DMPlexCreateFromFile()`, `DMSetType()`, `DMCreate()` 7709318fe57SMatthew G. Knepley @*/ 771d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm) 772d71ae5a4SJacob Faibussowitsch { 7739318fe57SMatthew G. Knepley PetscInt fac[3] = {1, 1, 1}; 7749318fe57SMatthew G. Knepley PetscReal low[3] = {0, 0, 0}; 7759318fe57SMatthew G. Knepley PetscReal upp[3] = {1, 1, 1}; 7769318fe57SMatthew G. Knepley 7779318fe57SMatthew G. Knepley PetscFunctionBegin; 7789566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 7799566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 7809566063dSJacob Faibussowitsch PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate)); 7813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7829318fe57SMatthew G. Knepley } 7839318fe57SMatthew G. Knepley 784d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm, PetscInt segments, PetscReal lower, PetscReal upper, DMBoundaryType bd) 785d71ae5a4SJacob Faibussowitsch { 786fdbf62faSLisandro Dalcin PetscInt i, fStart, fEnd, numCells = 0, numVerts = 0; 787fdbf62faSLisandro Dalcin PetscInt numPoints[2], *coneSize, *cones, *coneOrientations; 788fdbf62faSLisandro Dalcin PetscScalar *vertexCoords; 789fdbf62faSLisandro Dalcin PetscReal L, maxCell; 790fdbf62faSLisandro Dalcin PetscBool markerSeparate = PETSC_FALSE; 791fdbf62faSLisandro Dalcin PetscInt markerLeft = 1, faceMarkerLeft = 1; 792fdbf62faSLisandro Dalcin PetscInt markerRight = 1, faceMarkerRight = 2; 793fdbf62faSLisandro Dalcin PetscBool wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE; 794fdbf62faSLisandro Dalcin PetscMPIInt rank; 795fdbf62faSLisandro Dalcin 796fdbf62faSLisandro Dalcin PetscFunctionBegin; 7974f572ea9SToby Isaac PetscAssertPointer(dm, 1); 798fdbf62faSLisandro Dalcin 7999566063dSJacob Faibussowitsch PetscCall(DMSetDimension(dm, 1)); 8009566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dm, "marker")); 8019566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dm, "Face Sets")); 802fdbf62faSLisandro Dalcin 8039566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 804dd400576SPatrick Sanan if (rank == 0) numCells = segments; 805dd400576SPatrick Sanan if (rank == 0) numVerts = segments + (wrap ? 0 : 1); 806fdbf62faSLisandro Dalcin 8079371c9d4SSatish Balay numPoints[0] = numVerts; 8089371c9d4SSatish Balay numPoints[1] = numCells; 8099566063dSJacob Faibussowitsch PetscCall(PetscMalloc4(numCells + numVerts, &coneSize, numCells * 2, &cones, numCells + numVerts, &coneOrientations, numVerts, &vertexCoords)); 8109566063dSJacob Faibussowitsch PetscCall(PetscArrayzero(coneOrientations, numCells + numVerts)); 811ad540459SPierre Jolivet for (i = 0; i < numCells; ++i) coneSize[i] = 2; 812ad540459SPierre Jolivet for (i = 0; i < numVerts; ++i) coneSize[numCells + i] = 0; 8139371c9d4SSatish Balay for (i = 0; i < numCells; ++i) { 8149371c9d4SSatish Balay cones[2 * i] = numCells + i % numVerts; 8159371c9d4SSatish Balay cones[2 * i + 1] = numCells + (i + 1) % numVerts; 8169371c9d4SSatish Balay } 817ad540459SPierre Jolivet for (i = 0; i < numVerts; ++i) vertexCoords[i] = lower + (upper - lower) * ((PetscReal)i / (PetscReal)numCells); 8189566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 8199566063dSJacob Faibussowitsch PetscCall(PetscFree4(coneSize, cones, coneOrientations, vertexCoords)); 820fdbf62faSLisandro Dalcin 8219566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL)); 8229371c9d4SSatish Balay if (markerSeparate) { 8239371c9d4SSatish Balay markerLeft = faceMarkerLeft; 8249371c9d4SSatish Balay markerRight = faceMarkerRight; 8259371c9d4SSatish Balay } 826dd400576SPatrick Sanan if (!wrap && rank == 0) { 8279566063dSJacob Faibussowitsch PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd)); 8289566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", fStart, markerLeft)); 8299566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", fEnd - 1, markerRight)); 8309566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", fStart, faceMarkerLeft)); 8319566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", fEnd - 1, faceMarkerRight)); 832fdbf62faSLisandro Dalcin } 833fdbf62faSLisandro Dalcin if (wrap) { 834fdbf62faSLisandro Dalcin L = upper - lower; 835fdbf62faSLisandro Dalcin maxCell = (PetscReal)1.1 * (L / (PetscReal)PetscMax(1, segments)); 8364fb89dddSMatthew G. Knepley PetscCall(DMSetPeriodicity(dm, &maxCell, &lower, &L)); 837fdbf62faSLisandro Dalcin } 8389566063dSJacob Faibussowitsch PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE)); 8393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 840fdbf62faSLisandro Dalcin } 841fdbf62faSLisandro Dalcin 842d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate) 843d71ae5a4SJacob Faibussowitsch { 8449318fe57SMatthew G. Knepley DM boundary, vol; 845c22d3578SMatthew G. Knepley DMLabel bdlabel; 846d6218766SMatthew G. Knepley 847d6218766SMatthew G. Knepley PetscFunctionBegin; 8484f572ea9SToby Isaac PetscAssertPointer(dm, 1); 849c22d3578SMatthew G. Knepley for (PetscInt i = 0; i < dim; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity is not supported for simplex meshes"); 8509566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &boundary)); 8519566063dSJacob Faibussowitsch PetscCall(DMSetType(boundary, DMPLEX)); 8529566063dSJacob Faibussowitsch PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE)); 8539566063dSJacob Faibussowitsch PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol)); 854c22d3578SMatthew G. Knepley PetscCall(DMGetLabel(vol, "marker", &bdlabel)); 855c22d3578SMatthew G. Knepley if (bdlabel) PetscCall(DMPlexLabelComplete(vol, bdlabel)); 8565de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol)); 85769d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &vol)); 8589566063dSJacob Faibussowitsch PetscCall(DMDestroy(&boundary)); 8593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 860d6218766SMatthew G. Knepley } 861d6218766SMatthew G. Knepley 862d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ) 863d71ae5a4SJacob Faibussowitsch { 864ed0e4b50SMatthew G. Knepley DMLabel cutLabel = NULL; 865f4eb4c5dSMatthew G. Knepley PetscInt markerTop = 1, faceMarkerTop = 1; 866f4eb4c5dSMatthew G. Knepley PetscInt markerBottom = 1, faceMarkerBottom = 1; 867f4eb4c5dSMatthew G. Knepley PetscInt markerFront = 1, faceMarkerFront = 1; 868f4eb4c5dSMatthew G. Knepley PetscInt markerBack = 1, faceMarkerBack = 1; 869f4eb4c5dSMatthew G. Knepley PetscInt markerRight = 1, faceMarkerRight = 1; 870f4eb4c5dSMatthew G. Knepley PetscInt markerLeft = 1, faceMarkerLeft = 1; 8713dfda0b1SToby Isaac PetscInt dim; 872d8211ee3SMatthew G. Knepley PetscBool markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE; 8733dfda0b1SToby Isaac PetscMPIInt rank; 8743dfda0b1SToby Isaac 8753dfda0b1SToby Isaac PetscFunctionBegin; 8769566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 8779566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 8789566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dm, "marker")); 8799566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dm, "Face Sets")); 8809566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL)); 8819371c9d4SSatish Balay if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST || bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST || bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) { 8829371c9d4SSatish Balay if (cutMarker) { 8839371c9d4SSatish Balay PetscCall(DMCreateLabel(dm, "periodic_cut")); 8849371c9d4SSatish Balay PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel)); 8859371c9d4SSatish Balay } 886d8211ee3SMatthew G. Knepley } 8873dfda0b1SToby Isaac switch (dim) { 8883dfda0b1SToby Isaac case 2: 889f4eb4c5dSMatthew G. Knepley faceMarkerTop = 3; 890f4eb4c5dSMatthew G. Knepley faceMarkerBottom = 1; 891f4eb4c5dSMatthew G. Knepley faceMarkerRight = 2; 892f4eb4c5dSMatthew G. Knepley faceMarkerLeft = 4; 8933dfda0b1SToby Isaac break; 8943dfda0b1SToby Isaac case 3: 895f4eb4c5dSMatthew G. Knepley faceMarkerBottom = 1; 896f4eb4c5dSMatthew G. Knepley faceMarkerTop = 2; 897f4eb4c5dSMatthew G. Knepley faceMarkerFront = 3; 898f4eb4c5dSMatthew G. Knepley faceMarkerBack = 4; 899f4eb4c5dSMatthew G. Knepley faceMarkerRight = 5; 900f4eb4c5dSMatthew G. Knepley faceMarkerLeft = 6; 9013dfda0b1SToby Isaac break; 902d71ae5a4SJacob Faibussowitsch default: 903d71ae5a4SJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim); 9043dfda0b1SToby Isaac } 9059566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL)); 906f4eb4c5dSMatthew G. Knepley if (markerSeparate) { 907f4eb4c5dSMatthew G. Knepley markerBottom = faceMarkerBottom; 908f4eb4c5dSMatthew G. Knepley markerTop = faceMarkerTop; 909f4eb4c5dSMatthew G. Knepley markerFront = faceMarkerFront; 910f4eb4c5dSMatthew G. Knepley markerBack = faceMarkerBack; 911f4eb4c5dSMatthew G. Knepley markerRight = faceMarkerRight; 912f4eb4c5dSMatthew G. Knepley markerLeft = faceMarkerLeft; 9133dfda0b1SToby Isaac } 9143dfda0b1SToby Isaac { 915dd400576SPatrick Sanan const PetscInt numXEdges = rank == 0 ? edges[0] : 0; 916dd400576SPatrick Sanan const PetscInt numYEdges = rank == 0 ? edges[1] : 0; 917dd400576SPatrick Sanan const PetscInt numZEdges = rank == 0 ? edges[2] : 0; 918dd400576SPatrick Sanan const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0] + 1) : 0; 919dd400576SPatrick Sanan const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1] + 1) : 0; 920dd400576SPatrick Sanan const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2] + 1) : 0; 9213dfda0b1SToby Isaac const PetscInt numCells = numXEdges * numYEdges * numZEdges; 9223dfda0b1SToby Isaac const PetscInt numXFaces = numYEdges * numZEdges; 9233dfda0b1SToby Isaac const PetscInt numYFaces = numXEdges * numZEdges; 9243dfda0b1SToby Isaac const PetscInt numZFaces = numXEdges * numYEdges; 9253dfda0b1SToby Isaac const PetscInt numTotXFaces = numXVertices * numXFaces; 9263dfda0b1SToby Isaac const PetscInt numTotYFaces = numYVertices * numYFaces; 9273dfda0b1SToby Isaac const PetscInt numTotZFaces = numZVertices * numZFaces; 9283dfda0b1SToby Isaac const PetscInt numFaces = numTotXFaces + numTotYFaces + numTotZFaces; 9293dfda0b1SToby Isaac const PetscInt numTotXEdges = numXEdges * numYVertices * numZVertices; 9303dfda0b1SToby Isaac const PetscInt numTotYEdges = numYEdges * numXVertices * numZVertices; 9313dfda0b1SToby Isaac const PetscInt numTotZEdges = numZEdges * numXVertices * numYVertices; 9323dfda0b1SToby Isaac const PetscInt numVertices = numXVertices * numYVertices * numZVertices; 9333dfda0b1SToby Isaac const PetscInt numEdges = numTotXEdges + numTotYEdges + numTotZEdges; 9343dfda0b1SToby Isaac const PetscInt firstVertex = (dim == 2) ? numFaces : numCells; 9353dfda0b1SToby Isaac const PetscInt firstXFace = (dim == 2) ? 0 : numCells + numVertices; 9363dfda0b1SToby Isaac const PetscInt firstYFace = firstXFace + numTotXFaces; 9373dfda0b1SToby Isaac const PetscInt firstZFace = firstYFace + numTotYFaces; 9383dfda0b1SToby Isaac const PetscInt firstXEdge = numCells + numFaces + numVertices; 9393dfda0b1SToby Isaac const PetscInt firstYEdge = firstXEdge + numTotXEdges; 9403dfda0b1SToby Isaac const PetscInt firstZEdge = firstYEdge + numTotYEdges; 9413dfda0b1SToby Isaac Vec coordinates; 9423dfda0b1SToby Isaac PetscSection coordSection; 9433dfda0b1SToby Isaac PetscScalar *coords; 9443dfda0b1SToby Isaac PetscInt coordSize; 9453dfda0b1SToby Isaac PetscInt v, vx, vy, vz; 9463dfda0b1SToby Isaac PetscInt c, f, fx, fy, fz, e, ex, ey, ez; 9473dfda0b1SToby Isaac 9489566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dm, 0, numCells + numFaces + numEdges + numVertices)); 94948a46eb9SPierre Jolivet for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6)); 95048a46eb9SPierre Jolivet for (f = firstXFace; f < firstXFace + numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4)); 95148a46eb9SPierre Jolivet for (e = firstXEdge; e < firstXEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2)); 9529566063dSJacob Faibussowitsch PetscCall(DMSetUp(dm)); /* Allocate space for cones */ 9533dfda0b1SToby Isaac /* Build cells */ 9543dfda0b1SToby Isaac for (fz = 0; fz < numZEdges; ++fz) { 9553dfda0b1SToby Isaac for (fy = 0; fy < numYEdges; ++fy) { 9563dfda0b1SToby Isaac for (fx = 0; fx < numXEdges; ++fx) { 9573dfda0b1SToby Isaac PetscInt cell = (fz * numYEdges + fy) * numXEdges + fx; 9583dfda0b1SToby Isaac PetscInt faceB = firstZFace + (fy * numXEdges + fx) * numZVertices + fz; 9593dfda0b1SToby Isaac PetscInt faceT = firstZFace + (fy * numXEdges + fx) * numZVertices + ((fz + 1) % numZVertices); 9603dfda0b1SToby Isaac PetscInt faceF = firstYFace + (fz * numXEdges + fx) * numYVertices + fy; 9613dfda0b1SToby Isaac PetscInt faceK = firstYFace + (fz * numXEdges + fx) * numYVertices + ((fy + 1) % numYVertices); 9623dfda0b1SToby Isaac PetscInt faceL = firstXFace + (fz * numYEdges + fy) * numXVertices + fx; 9633dfda0b1SToby Isaac PetscInt faceR = firstXFace + (fz * numYEdges + fy) * numXVertices + ((fx + 1) % numXVertices); 9643dfda0b1SToby Isaac /* B, T, F, K, R, L */ 965b5a892a1SMatthew G. Knepley PetscInt ornt[6] = {-2, 0, 0, -3, 0, -2}; /* ??? */ 96642206facSLisandro Dalcin PetscInt cone[6]; 9673dfda0b1SToby Isaac 9683dfda0b1SToby Isaac /* no boundary twisting in 3D */ 9699371c9d4SSatish Balay cone[0] = faceB; 9709371c9d4SSatish Balay cone[1] = faceT; 9719371c9d4SSatish Balay cone[2] = faceF; 9729371c9d4SSatish Balay cone[3] = faceK; 9739371c9d4SSatish Balay cone[4] = faceR; 9749371c9d4SSatish Balay cone[5] = faceL; 9759566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, cell, cone)); 9769566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(dm, cell, ornt)); 9779566063dSJacob Faibussowitsch if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2)); 9789566063dSJacob Faibussowitsch if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2)); 9799566063dSJacob Faibussowitsch if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2)); 9803dfda0b1SToby Isaac } 9813dfda0b1SToby Isaac } 9823dfda0b1SToby Isaac } 9833dfda0b1SToby Isaac /* Build x faces */ 9843dfda0b1SToby Isaac for (fz = 0; fz < numZEdges; ++fz) { 9853dfda0b1SToby Isaac for (fy = 0; fy < numYEdges; ++fy) { 9863dfda0b1SToby Isaac for (fx = 0; fx < numXVertices; ++fx) { 9873dfda0b1SToby Isaac PetscInt face = firstXFace + (fz * numYEdges + fy) * numXVertices + fx; 9883dfda0b1SToby Isaac PetscInt edgeL = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz; 9893dfda0b1SToby Isaac PetscInt edgeR = firstZEdge + (((fy + 1) % numYVertices) * numXVertices + fx) * numZEdges + fz; 9903dfda0b1SToby Isaac PetscInt edgeB = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy; 9913dfda0b1SToby Isaac PetscInt edgeT = firstYEdge + (((fz + 1) % numZVertices) * numXVertices + fx) * numYEdges + fy; 992b5a892a1SMatthew G. Knepley PetscInt ornt[4] = {0, 0, -1, -1}; 9933dfda0b1SToby Isaac PetscInt cone[4]; 9943dfda0b1SToby Isaac 9953dfda0b1SToby Isaac if (dim == 3) { 9963dfda0b1SToby Isaac /* markers */ 9973dfda0b1SToby Isaac if (bdX != DM_BOUNDARY_PERIODIC) { 9983dfda0b1SToby Isaac if (fx == numXVertices - 1) { 9999566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight)); 10009566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", face, markerRight)); 10019371c9d4SSatish Balay } else if (fx == 0) { 10029566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft)); 10039566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft)); 10043dfda0b1SToby Isaac } 10053dfda0b1SToby Isaac } 10063dfda0b1SToby Isaac } 10079371c9d4SSatish Balay cone[0] = edgeB; 10089371c9d4SSatish Balay cone[1] = edgeR; 10099371c9d4SSatish Balay cone[2] = edgeT; 10109371c9d4SSatish Balay cone[3] = edgeL; 10119566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, face, cone)); 10129566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(dm, face, ornt)); 10133dfda0b1SToby Isaac } 10143dfda0b1SToby Isaac } 10153dfda0b1SToby Isaac } 10163dfda0b1SToby Isaac /* Build y faces */ 10173dfda0b1SToby Isaac for (fz = 0; fz < numZEdges; ++fz) { 101842206facSLisandro Dalcin for (fx = 0; fx < numXEdges; ++fx) { 10193dfda0b1SToby Isaac for (fy = 0; fy < numYVertices; ++fy) { 10203dfda0b1SToby Isaac PetscInt face = firstYFace + (fz * numXEdges + fx) * numYVertices + fy; 10213dfda0b1SToby Isaac PetscInt edgeL = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz; 10223dfda0b1SToby Isaac PetscInt edgeR = firstZEdge + (fy * numXVertices + ((fx + 1) % numXVertices)) * numZEdges + fz; 10233dfda0b1SToby Isaac PetscInt edgeB = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx; 10243dfda0b1SToby Isaac PetscInt edgeT = firstXEdge + (((fz + 1) % numZVertices) * numYVertices + fy) * numXEdges + fx; 1025b5a892a1SMatthew G. Knepley PetscInt ornt[4] = {0, 0, -1, -1}; 10263dfda0b1SToby Isaac PetscInt cone[4]; 10273dfda0b1SToby Isaac 10283dfda0b1SToby Isaac if (dim == 3) { 10293dfda0b1SToby Isaac /* markers */ 10303dfda0b1SToby Isaac if (bdY != DM_BOUNDARY_PERIODIC) { 10313dfda0b1SToby Isaac if (fy == numYVertices - 1) { 10329566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack)); 10339566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", face, markerBack)); 10349371c9d4SSatish Balay } else if (fy == 0) { 10359566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront)); 10369566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", face, markerFront)); 10373dfda0b1SToby Isaac } 10383dfda0b1SToby Isaac } 10393dfda0b1SToby Isaac } 10409371c9d4SSatish Balay cone[0] = edgeB; 10419371c9d4SSatish Balay cone[1] = edgeR; 10429371c9d4SSatish Balay cone[2] = edgeT; 10439371c9d4SSatish Balay cone[3] = edgeL; 10449566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, face, cone)); 10459566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(dm, face, ornt)); 10463dfda0b1SToby Isaac } 10473dfda0b1SToby Isaac } 10483dfda0b1SToby Isaac } 10493dfda0b1SToby Isaac /* Build z faces */ 10503dfda0b1SToby Isaac for (fy = 0; fy < numYEdges; ++fy) { 10513dfda0b1SToby Isaac for (fx = 0; fx < numXEdges; ++fx) { 10523dfda0b1SToby Isaac for (fz = 0; fz < numZVertices; fz++) { 10533dfda0b1SToby Isaac PetscInt face = firstZFace + (fy * numXEdges + fx) * numZVertices + fz; 10543dfda0b1SToby Isaac PetscInt edgeL = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy; 10553dfda0b1SToby Isaac PetscInt edgeR = firstYEdge + (fz * numXVertices + ((fx + 1) % numXVertices)) * numYEdges + fy; 10563dfda0b1SToby Isaac PetscInt edgeB = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx; 10573dfda0b1SToby Isaac PetscInt edgeT = firstXEdge + (fz * numYVertices + ((fy + 1) % numYVertices)) * numXEdges + fx; 1058b5a892a1SMatthew G. Knepley PetscInt ornt[4] = {0, 0, -1, -1}; 10593dfda0b1SToby Isaac PetscInt cone[4]; 10603dfda0b1SToby Isaac 10613dfda0b1SToby Isaac if (dim == 2) { 10629371c9d4SSatish Balay if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges - 1) { 10639371c9d4SSatish Balay edgeR += numYEdges - 1 - 2 * fy; 10649371c9d4SSatish Balay ornt[1] = -1; 10659371c9d4SSatish Balay } 10669371c9d4SSatish Balay if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges - 1) { 10679371c9d4SSatish Balay edgeT += numXEdges - 1 - 2 * fx; 10689371c9d4SSatish Balay ornt[2] = 0; 10699371c9d4SSatish Balay } 10709566063dSJacob Faibussowitsch if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2)); 10719566063dSJacob Faibussowitsch if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2)); 1072d1c88043SMatthew G. Knepley } else { 10733dfda0b1SToby Isaac /* markers */ 10743dfda0b1SToby Isaac if (bdZ != DM_BOUNDARY_PERIODIC) { 10753dfda0b1SToby Isaac if (fz == numZVertices - 1) { 10769566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop)); 10779566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", face, markerTop)); 10789371c9d4SSatish Balay } else if (fz == 0) { 10799566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom)); 10809566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom)); 10813dfda0b1SToby Isaac } 10823dfda0b1SToby Isaac } 10833dfda0b1SToby Isaac } 10849371c9d4SSatish Balay cone[0] = edgeB; 10859371c9d4SSatish Balay cone[1] = edgeR; 10869371c9d4SSatish Balay cone[2] = edgeT; 10879371c9d4SSatish Balay cone[3] = edgeL; 10889566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, face, cone)); 10899566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(dm, face, ornt)); 10903dfda0b1SToby Isaac } 10913dfda0b1SToby Isaac } 10923dfda0b1SToby Isaac } 10933dfda0b1SToby Isaac /* Build Z edges*/ 10943dfda0b1SToby Isaac for (vy = 0; vy < numYVertices; vy++) { 10953dfda0b1SToby Isaac for (vx = 0; vx < numXVertices; vx++) { 10963dfda0b1SToby Isaac for (ez = 0; ez < numZEdges; ez++) { 10973dfda0b1SToby Isaac const PetscInt edge = firstZEdge + (vy * numXVertices + vx) * numZEdges + ez; 10983dfda0b1SToby Isaac const PetscInt vertexB = firstVertex + (ez * numYVertices + vy) * numXVertices + vx; 10993dfda0b1SToby Isaac const PetscInt vertexT = firstVertex + (((ez + 1) % numZVertices) * numYVertices + vy) * numXVertices + vx; 11003dfda0b1SToby Isaac PetscInt cone[2]; 11013dfda0b1SToby Isaac 11029371c9d4SSatish Balay cone[0] = vertexB; 11039371c9d4SSatish Balay cone[1] = vertexT; 1104c2df9bbfSMatthew G. Knepley PetscCall(DMPlexSetCone(dm, edge, cone)); 11053dfda0b1SToby Isaac if (dim == 3) { 11063dfda0b1SToby Isaac if (bdX != DM_BOUNDARY_PERIODIC) { 11073dfda0b1SToby Isaac if (vx == numXVertices - 1) { 11089566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight)); 1109c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight)); 1110c2df9bbfSMatthew G. Knepley if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight)); 1111c2df9bbfSMatthew G. Knepley } else if (vx == 0) { 11129566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft)); 1113c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft)); 1114c2df9bbfSMatthew G. Knepley if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft)); 11153dfda0b1SToby Isaac } 11163dfda0b1SToby Isaac } 11173dfda0b1SToby Isaac if (bdY != DM_BOUNDARY_PERIODIC) { 11183dfda0b1SToby Isaac if (vy == numYVertices - 1) { 11199566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack)); 1120c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack)); 1121c2df9bbfSMatthew G. Knepley if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack)); 1122c2df9bbfSMatthew G. Knepley } else if (vy == 0) { 11239566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront)); 1124c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront)); 1125c2df9bbfSMatthew G. Knepley if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront)); 11263dfda0b1SToby Isaac } 11273dfda0b1SToby Isaac } 11283dfda0b1SToby Isaac } 11293dfda0b1SToby Isaac } 11303dfda0b1SToby Isaac } 11313dfda0b1SToby Isaac } 11323dfda0b1SToby Isaac /* Build Y edges*/ 11333dfda0b1SToby Isaac for (vz = 0; vz < numZVertices; vz++) { 11343dfda0b1SToby Isaac for (vx = 0; vx < numXVertices; vx++) { 11353dfda0b1SToby Isaac for (ey = 0; ey < numYEdges; ey++) { 11363dfda0b1SToby Isaac const PetscInt nextv = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges - 1) ? (numXVertices - vx - 1) : (vz * numYVertices + ((ey + 1) % numYVertices)) * numXVertices + vx; 11373dfda0b1SToby Isaac const PetscInt edge = firstYEdge + (vz * numXVertices + vx) * numYEdges + ey; 11383dfda0b1SToby Isaac const PetscInt vertexF = firstVertex + (vz * numYVertices + ey) * numXVertices + vx; 11393dfda0b1SToby Isaac const PetscInt vertexK = firstVertex + nextv; 11403dfda0b1SToby Isaac PetscInt cone[2]; 11413dfda0b1SToby Isaac 11429371c9d4SSatish Balay cone[0] = vertexF; 11439371c9d4SSatish Balay cone[1] = vertexK; 11449566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, edge, cone)); 11453dfda0b1SToby Isaac if (dim == 2) { 11463dfda0b1SToby Isaac if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) { 11473dfda0b1SToby Isaac if (vx == numXVertices - 1) { 11489566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight)); 11499566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight)); 11509566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight)); 1151c2df9bbfSMatthew G. Knepley if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight)); 1152d8211ee3SMatthew G. Knepley } else if (vx == 0) { 11539566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft)); 11549566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft)); 11559566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft)); 1156c2df9bbfSMatthew G. Knepley if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft)); 11573dfda0b1SToby Isaac } 1158d8211ee3SMatthew G. Knepley } else { 11594c67ea77SStefano Zampini if (vx == 0 && cutLabel) { 11609566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(cutLabel, edge, 1)); 11619566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(cutLabel, cone[0], 1)); 1162c2df9bbfSMatthew G. Knepley if (ey == numYEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1)); 11633dfda0b1SToby Isaac } 1164d8211ee3SMatthew G. Knepley } 1165d8211ee3SMatthew G. Knepley } else { 11663dfda0b1SToby Isaac if (bdX != DM_BOUNDARY_PERIODIC) { 11673dfda0b1SToby Isaac if (vx == numXVertices - 1) { 11689566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight)); 1169c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight)); 1170c2df9bbfSMatthew G. Knepley if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight)); 1171d8211ee3SMatthew G. Knepley } else if (vx == 0) { 11729566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft)); 1173c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft)); 1174c2df9bbfSMatthew G. Knepley if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft)); 11753dfda0b1SToby Isaac } 11763dfda0b1SToby Isaac } 11773dfda0b1SToby Isaac if (bdZ != DM_BOUNDARY_PERIODIC) { 11783dfda0b1SToby Isaac if (vz == numZVertices - 1) { 11799566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop)); 1180c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop)); 1181c2df9bbfSMatthew G. Knepley if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop)); 1182d8211ee3SMatthew G. Knepley } else if (vz == 0) { 11839566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom)); 1184c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom)); 1185c2df9bbfSMatthew G. Knepley if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom)); 11863dfda0b1SToby Isaac } 11873dfda0b1SToby Isaac } 11883dfda0b1SToby Isaac } 11893dfda0b1SToby Isaac } 11903dfda0b1SToby Isaac } 11913dfda0b1SToby Isaac } 11923dfda0b1SToby Isaac /* Build X edges*/ 11933dfda0b1SToby Isaac for (vz = 0; vz < numZVertices; vz++) { 11943dfda0b1SToby Isaac for (vy = 0; vy < numYVertices; vy++) { 11953dfda0b1SToby Isaac for (ex = 0; ex < numXEdges; ex++) { 11963dfda0b1SToby Isaac const PetscInt nextv = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges - 1) ? (numYVertices - vy - 1) * numXVertices : (vz * numYVertices + vy) * numXVertices + (ex + 1) % numXVertices; 11973dfda0b1SToby Isaac const PetscInt edge = firstXEdge + (vz * numYVertices + vy) * numXEdges + ex; 11983dfda0b1SToby Isaac const PetscInt vertexL = firstVertex + (vz * numYVertices + vy) * numXVertices + ex; 11993dfda0b1SToby Isaac const PetscInt vertexR = firstVertex + nextv; 12003dfda0b1SToby Isaac PetscInt cone[2]; 12013dfda0b1SToby Isaac 12029371c9d4SSatish Balay cone[0] = vertexL; 12039371c9d4SSatish Balay cone[1] = vertexR; 12049566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, edge, cone)); 12053dfda0b1SToby Isaac if (dim == 2) { 12063dfda0b1SToby Isaac if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) { 12073dfda0b1SToby Isaac if (vy == numYVertices - 1) { 12089566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop)); 12099566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop)); 12109566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop)); 1211c2df9bbfSMatthew G. Knepley if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop)); 1212d8211ee3SMatthew G. Knepley } else if (vy == 0) { 12139566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom)); 12149566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom)); 12159566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom)); 1216c2df9bbfSMatthew G. Knepley if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom)); 12173dfda0b1SToby Isaac } 1218d8211ee3SMatthew G. Knepley } else { 12194c67ea77SStefano Zampini if (vy == 0 && cutLabel) { 12209566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(cutLabel, edge, 1)); 12219566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(cutLabel, cone[0], 1)); 1222c2df9bbfSMatthew G. Knepley if (ex == numXEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1)); 12233dfda0b1SToby Isaac } 1224d8211ee3SMatthew G. Knepley } 1225d8211ee3SMatthew G. Knepley } else { 12263dfda0b1SToby Isaac if (bdY != DM_BOUNDARY_PERIODIC) { 12273dfda0b1SToby Isaac if (vy == numYVertices - 1) { 12289566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack)); 1229c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack)); 1230c2df9bbfSMatthew G. Knepley if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack)); 1231c2df9bbfSMatthew G. Knepley } else if (vy == 0) { 12329566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront)); 1233c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront)); 1234c2df9bbfSMatthew G. Knepley if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront)); 12353dfda0b1SToby Isaac } 12363dfda0b1SToby Isaac } 12373dfda0b1SToby Isaac if (bdZ != DM_BOUNDARY_PERIODIC) { 12383dfda0b1SToby Isaac if (vz == numZVertices - 1) { 12399566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop)); 1240c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop)); 1241c2df9bbfSMatthew G. Knepley if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop)); 1242c2df9bbfSMatthew G. Knepley } else if (vz == 0) { 12439566063dSJacob Faibussowitsch PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom)); 1244c2df9bbfSMatthew G. Knepley PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom)); 1245c2df9bbfSMatthew G. Knepley if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom)); 12463dfda0b1SToby Isaac } 12473dfda0b1SToby Isaac } 12483dfda0b1SToby Isaac } 12493dfda0b1SToby Isaac } 12503dfda0b1SToby Isaac } 12513dfda0b1SToby Isaac } 12529566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dm)); 12539566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dm)); 12543dfda0b1SToby Isaac /* Build coordinates */ 12559566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 12569566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 12579566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim)); 12589566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices)); 12593dfda0b1SToby Isaac for (v = firstVertex; v < firstVertex + numVertices; ++v) { 12609566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, dim)); 12619566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim)); 12623dfda0b1SToby Isaac } 12639566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 12649566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 12659566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates)); 12669566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 12679566063dSJacob Faibussowitsch PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 12689566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(coordinates, dim)); 12699566063dSJacob Faibussowitsch PetscCall(VecSetType(coordinates, VECSTANDARD)); 12709566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 12713dfda0b1SToby Isaac for (vz = 0; vz < numZVertices; ++vz) { 12723dfda0b1SToby Isaac for (vy = 0; vy < numYVertices; ++vy) { 12733dfda0b1SToby Isaac for (vx = 0; vx < numXVertices; ++vx) { 12743dfda0b1SToby Isaac coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx; 12753dfda0b1SToby Isaac coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy; 1276ad540459SPierre Jolivet if (dim == 3) coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 2] = lower[2] + ((upper[2] - lower[2]) / numZEdges) * vz; 12773dfda0b1SToby Isaac } 12783dfda0b1SToby Isaac } 12793dfda0b1SToby Isaac } 12809566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 12819566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, coordinates)); 12829566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 12833dfda0b1SToby Isaac } 12843ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 12853dfda0b1SToby Isaac } 12863dfda0b1SToby Isaac 1287d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[]) 1288d71ae5a4SJacob Faibussowitsch { 12899318fe57SMatthew G. Knepley DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE}; 12909318fe57SMatthew G. Knepley PetscInt fac[3] = {0, 0, 0}, d; 1291552f7358SJed Brown 1292552f7358SJed Brown PetscFunctionBegin; 12934f572ea9SToby Isaac PetscAssertPointer(dm, 1); 12949318fe57SMatthew G. Knepley PetscValidLogicalCollectiveInt(dm, dim, 2); 12959566063dSJacob Faibussowitsch PetscCall(DMSetDimension(dm, dim)); 12969371c9d4SSatish Balay for (d = 0; d < dim; ++d) { 12979371c9d4SSatish Balay fac[d] = faces[d]; 12989371c9d4SSatish Balay bdt[d] = periodicity[d]; 12999371c9d4SSatish Balay } 13009566063dSJacob Faibussowitsch PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2])); 13019371c9d4SSatish Balay if (periodicity[0] == DM_BOUNDARY_PERIODIC || periodicity[0] == DM_BOUNDARY_TWIST || periodicity[1] == DM_BOUNDARY_PERIODIC || periodicity[1] == DM_BOUNDARY_TWIST || (dim > 2 && (periodicity[2] == DM_BOUNDARY_PERIODIC || periodicity[2] == DM_BOUNDARY_TWIST))) { 13026858538eSMatthew G. Knepley PetscReal L[3] = {-1., -1., 0.}; 13036858538eSMatthew G. Knepley PetscReal maxCell[3] = {-1., -1., 0.}; 1304552f7358SJed Brown 13059318fe57SMatthew G. Knepley for (d = 0; d < dim; ++d) { 13066858538eSMatthew G. Knepley if (periodicity[d] != DM_BOUNDARY_NONE) { 13079318fe57SMatthew G. Knepley L[d] = upper[d] - lower[d]; 13089318fe57SMatthew G. Knepley maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d])); 1309768d5fceSMatthew G. Knepley } 13106858538eSMatthew G. Knepley } 13114fb89dddSMatthew G. Knepley PetscCall(DMSetPeriodicity(dm, maxCell, lower, L)); 1312768d5fceSMatthew G. Knepley } 13139566063dSJacob Faibussowitsch PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE)); 13143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 13159318fe57SMatthew G. Knepley } 13169318fe57SMatthew G. Knepley 13175dca41c3SJed Brown static PetscErrorCode DMPlexCreateBoxMesh_Internal(DM dm, DMPlexShape shape, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate) 1318d71ae5a4SJacob Faibussowitsch { 13199318fe57SMatthew G. Knepley PetscFunctionBegin; 132046139095SJed Brown PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0)); 13215dca41c3SJed Brown if (shape == DM_SHAPE_ZBOX) PetscCall(DMPlexCreateBoxMesh_Tensor_SFC_Internal(dm, dim, faces, lower, upper, periodicity, interpolate)); 13226725e60dSJed Brown else if (dim == 1) PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0])); 13239566063dSJacob Faibussowitsch else if (simplex) PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate)); 13249566063dSJacob Faibussowitsch else PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity)); 13259318fe57SMatthew G. Knepley if (!interpolate && dim > 1 && !simplex) { 1326768d5fceSMatthew G. Knepley DM udm; 1327768d5fceSMatthew G. Knepley 13289566063dSJacob Faibussowitsch PetscCall(DMPlexUninterpolate(dm, &udm)); 13299566063dSJacob Faibussowitsch PetscCall(DMPlexCopyCoordinates(dm, udm)); 133069d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &udm)); 1331768d5fceSMatthew G. Knepley } 133246139095SJed Brown PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0)); 13333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1334c8c68bd8SToby Isaac } 1335c8c68bd8SToby Isaac 1336768d5fceSMatthew G. Knepley /*@C 1337768d5fceSMatthew G. Knepley DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra). 1338768d5fceSMatthew G. Knepley 1339d083f849SBarry Smith Collective 1340768d5fceSMatthew G. Knepley 1341768d5fceSMatthew G. Knepley Input Parameters: 1342a1cb98faSBarry Smith + comm - The communicator for the `DM` object 1343768d5fceSMatthew G. Knepley . dim - The spatial dimension 1344a1cb98faSBarry Smith . simplex - `PETSC_TRUE` for simplices, `PETSC_FALSE` for tensor cells 134520f4b53cSBarry Smith . faces - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D 134620f4b53cSBarry Smith . lower - The lower left corner, or `NULL` for (0, 0, 0) 134720f4b53cSBarry Smith . upper - The upper right corner, or `NULL` for (1, 1, 1) 134820f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE` 1349768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces) 1350768d5fceSMatthew G. Knepley 1351768d5fceSMatthew G. Knepley Output Parameter: 1352a1cb98faSBarry Smith . dm - The `DM` object 1353768d5fceSMatthew G. Knepley 1354768d5fceSMatthew G. Knepley Level: beginner 1355768d5fceSMatthew G. Knepley 1356a1cb98faSBarry Smith Note: 1357a1cb98faSBarry Smith To customize this mesh using options, use 1358a1cb98faSBarry Smith .vb 1359a1cb98faSBarry Smith DMCreate(comm, &dm); 1360a1cb98faSBarry Smith DMSetType(dm, DMPLEX); 1361a1cb98faSBarry Smith DMSetFromOptions(dm); 1362a1cb98faSBarry Smith .ve 1363a1cb98faSBarry Smith and use the options in `DMSetFromOptions()`. 1364a1cb98faSBarry Smith 1365a4e35b19SJacob Faibussowitsch Here is the numbering returned for 2 faces in each direction for tensor cells\: 1366a1cb98faSBarry Smith .vb 1367a1cb98faSBarry Smith 10---17---11---18----12 1368a1cb98faSBarry Smith | | | 1369a1cb98faSBarry Smith | | | 1370a1cb98faSBarry Smith 20 2 22 3 24 1371a1cb98faSBarry Smith | | | 1372a1cb98faSBarry Smith | | | 1373a1cb98faSBarry Smith 7---15----8---16----9 1374a1cb98faSBarry Smith | | | 1375a1cb98faSBarry Smith | | | 1376a1cb98faSBarry Smith 19 0 21 1 23 1377a1cb98faSBarry Smith | | | 1378a1cb98faSBarry Smith | | | 1379a1cb98faSBarry Smith 4---13----5---14----6 1380a1cb98faSBarry Smith .ve 1381a1cb98faSBarry Smith and for simplicial cells 1382a1cb98faSBarry Smith .vb 1383a1cb98faSBarry Smith 14----8---15----9----16 1384a1cb98faSBarry Smith |\ 5 |\ 7 | 1385a1cb98faSBarry Smith | \ | \ | 1386a1cb98faSBarry Smith 13 2 14 3 15 1387a1cb98faSBarry Smith | 4 \ | 6 \ | 1388a1cb98faSBarry Smith | \ | \ | 1389a1cb98faSBarry Smith 11----6---12----7----13 1390a1cb98faSBarry Smith |\ |\ | 1391a1cb98faSBarry Smith | \ 1 | \ 3 | 1392a1cb98faSBarry Smith 10 0 11 1 12 1393a1cb98faSBarry Smith | 0 \ | 2 \ | 1394a1cb98faSBarry Smith | \ | \ | 1395a1cb98faSBarry Smith 8----4----9----5----10 1396a1cb98faSBarry Smith .ve 1397a1cb98faSBarry Smith 13981cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()` 1399768d5fceSMatthew G. Knepley @*/ 1400d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate, DM *dm) 1401d71ae5a4SJacob Faibussowitsch { 14029318fe57SMatthew G. Knepley PetscInt fac[3] = {1, 1, 1}; 1403fdbf62faSLisandro Dalcin PetscReal low[3] = {0, 0, 0}; 1404fdbf62faSLisandro Dalcin PetscReal upp[3] = {1, 1, 1}; 1405fdbf62faSLisandro Dalcin DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE}; 1406552f7358SJed Brown 1407768d5fceSMatthew G. Knepley PetscFunctionBegin; 14089566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 14099566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 14105dca41c3SJed Brown PetscCall(DMPlexCreateBoxMesh_Internal(*dm, DM_SHAPE_BOX, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate)); 14117ff04441SMatthew G. Knepley if (periodicity) PetscCall(DMLocalizeCoordinates(*dm)); 14123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 14139318fe57SMatthew G. Knepley } 1414fdbf62faSLisandro Dalcin 1415d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[]) 1416d71ae5a4SJacob Faibussowitsch { 14179318fe57SMatthew G. Knepley DM bdm, vol; 14189318fe57SMatthew G. Knepley PetscInt i; 14199318fe57SMatthew G. Knepley 14209318fe57SMatthew G. Knepley PetscFunctionBegin; 14211fcf445aSMatthew G. Knepley // TODO Now we can support periodicity 142208401ef6SPierre Jolivet for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity not yet supported"); 14239566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &bdm)); 14249566063dSJacob Faibussowitsch PetscCall(DMSetType(bdm, DMPLEX)); 14259566063dSJacob Faibussowitsch PetscCall(DMSetDimension(bdm, 2)); 142646139095SJed Brown PetscCall(PetscLogEventBegin(DMPLEX_Generate, bdm, 0, 0, 0)); 14279566063dSJacob Faibussowitsch PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE)); 14281fcf445aSMatthew G. Knepley PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, NULL, NULL, &vol)); 142946139095SJed Brown PetscCall(PetscLogEventEnd(DMPLEX_Generate, bdm, 0, 0, 0)); 14309566063dSJacob Faibussowitsch PetscCall(DMDestroy(&bdm)); 143169d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &vol)); 14329318fe57SMatthew G. Knepley if (lower[2] != 0.0) { 14339318fe57SMatthew G. Knepley Vec v; 14349318fe57SMatthew G. Knepley PetscScalar *x; 14359318fe57SMatthew G. Knepley PetscInt cDim, n; 14369318fe57SMatthew G. Knepley 14379566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &v)); 14389566063dSJacob Faibussowitsch PetscCall(VecGetBlockSize(v, &cDim)); 14399566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(v, &n)); 14409566063dSJacob Faibussowitsch PetscCall(VecGetArray(v, &x)); 14419318fe57SMatthew G. Knepley x += cDim; 14429318fe57SMatthew G. Knepley for (i = 0; i < n; i += cDim) x[i] += lower[2]; 14439566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(v, &x)); 14449566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, v)); 14459318fe57SMatthew G. Knepley } 14463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1447552f7358SJed Brown } 1448552f7358SJed Brown 144900dabe28SStefano Zampini /*@ 145039f4f5dbSPierre Jolivet DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tessellating the (x,y) plane and extruding in the third direction using wedge cells. 145100dabe28SStefano Zampini 1452d083f849SBarry Smith Collective 145300dabe28SStefano Zampini 145400dabe28SStefano Zampini Input Parameters: 1455a1cb98faSBarry Smith + comm - The communicator for the `DM` object 145620f4b53cSBarry Smith . faces - Number of faces per dimension, or `NULL` for (1, 1, 1) 145720f4b53cSBarry Smith . lower - The lower left corner, or `NULL` for (0, 0, 0) 145820f4b53cSBarry Smith . upper - The upper right corner, or `NULL` for (1, 1, 1) 145920f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE` 1460a1cb98faSBarry Smith . orderHeight - If `PETSC_TRUE`, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first 146100dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces) 146200dabe28SStefano Zampini 146300dabe28SStefano Zampini Output Parameter: 1464a1cb98faSBarry Smith . dm - The `DM` object 146500dabe28SStefano Zampini 146600dabe28SStefano Zampini Level: beginner 146700dabe28SStefano Zampini 14681cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()` 146900dabe28SStefano Zampini @*/ 1470d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm) 1471d71ae5a4SJacob Faibussowitsch { 14729318fe57SMatthew G. Knepley PetscInt fac[3] = {1, 1, 1}; 147300dabe28SStefano Zampini PetscReal low[3] = {0, 0, 0}; 147400dabe28SStefano Zampini PetscReal upp[3] = {1, 1, 1}; 147500dabe28SStefano Zampini DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE}; 147600dabe28SStefano Zampini 147700dabe28SStefano Zampini PetscFunctionBegin; 14789566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 14799566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 14809566063dSJacob Faibussowitsch PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt)); 1481d410b0cfSMatthew G. Knepley if (!interpolate) { 1482d410b0cfSMatthew G. Knepley DM udm; 148300dabe28SStefano Zampini 14849566063dSJacob Faibussowitsch PetscCall(DMPlexUninterpolate(*dm, &udm)); 148569d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(*dm, &udm)); 148600dabe28SStefano Zampini } 14877ff04441SMatthew G. Knepley if (periodicity) PetscCall(DMLocalizeCoordinates(*dm)); 14883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 148900dabe28SStefano Zampini } 149000dabe28SStefano Zampini 1491cfb853baSMatthew G. Knepley /* 1492cfb853baSMatthew G. Knepley DMPlexTensorPointLexicographic_Private - Returns all tuples of size 'len' with nonnegative integers that are all less than or equal to 'max' for that dimension. 1493cfb853baSMatthew G. Knepley 1494cfb853baSMatthew G. Knepley Input Parameters: 1495cfb853baSMatthew G. Knepley + len - The length of the tuple 1496cfb853baSMatthew G. Knepley . max - The maximum for each dimension, so values are in [0, max) 1497cfb853baSMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition 1498cfb853baSMatthew G. Knepley 1499cfb853baSMatthew G. Knepley Output Parameter: 150020f4b53cSBarry Smith . tup - A tuple of `len` integers whose entries are at most `max` 1501cfb853baSMatthew G. Knepley 1502cfb853baSMatthew G. Knepley Level: developer 1503cfb853baSMatthew G. Knepley 150420f4b53cSBarry Smith Note: 150520f4b53cSBarry Smith Ordering is lexicographic with lowest index as least significant in ordering. 150620f4b53cSBarry Smith e.g. for len == 2 and max == 2, this will return, in order, {0,0}, {1,0}, {2,0}, {0,1}, {1,1}, {2,1}, {0,2}, {1,2}, {2,2}. 150720f4b53cSBarry Smith 1508cfb853baSMatthew G. Knepley .seealso: PetscDualSpaceTensorPointLexicographic_Internal(), PetscDualSpaceLatticePointLexicographic_Internal() 1509cfb853baSMatthew G. Knepley */ 1510cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexTensorPointLexicographic_Private(PetscInt len, const PetscInt max[], PetscInt tup[]) 1511cfb853baSMatthew G. Knepley { 1512cfb853baSMatthew G. Knepley PetscInt i; 1513cfb853baSMatthew G. Knepley 1514cfb853baSMatthew G. Knepley PetscFunctionBegin; 1515cfb853baSMatthew G. Knepley for (i = 0; i < len; ++i) { 1516cfb853baSMatthew G. Knepley if (tup[i] < max[i] - 1) { 1517cfb853baSMatthew G. Knepley break; 1518cfb853baSMatthew G. Knepley } else { 1519cfb853baSMatthew G. Knepley tup[i] = 0; 1520cfb853baSMatthew G. Knepley } 1521cfb853baSMatthew G. Knepley } 1522cfb853baSMatthew G. Knepley if (i == len) tup[i - 1] = max[i - 1]; 1523cfb853baSMatthew G. Knepley else ++tup[i]; 15243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1525cfb853baSMatthew G. Knepley } 1526cfb853baSMatthew G. Knepley 1527cfb853baSMatthew G. Knepley static PetscInt TupleToIndex_Private(PetscInt len, const PetscInt max[], const PetscInt tup[]) 1528cfb853baSMatthew G. Knepley { 1529cfb853baSMatthew G. Knepley PetscInt i, idx = tup[len - 1]; 1530cfb853baSMatthew G. Knepley 1531cfb853baSMatthew G. Knepley for (i = len - 2; i >= 0; --i) { 1532cfb853baSMatthew G. Knepley idx *= max[i]; 1533cfb853baSMatthew G. Knepley idx += tup[i]; 1534cfb853baSMatthew G. Knepley } 1535cfb853baSMatthew G. Knepley return idx; 1536cfb853baSMatthew G. Knepley } 1537cfb853baSMatthew G. Knepley 1538cfb853baSMatthew G. Knepley static PetscErrorCode DestroyExtent_Private(void *extent) 1539cfb853baSMatthew G. Knepley { 1540cfb853baSMatthew G. Knepley return PetscFree(extent); 1541cfb853baSMatthew G. Knepley } 1542cfb853baSMatthew G. Knepley 1543cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexCreateHypercubicMesh_Internal(DM dm, PetscInt dim, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], const DMBoundaryType bd[]) 1544cfb853baSMatthew G. Knepley { 1545cfb853baSMatthew G. Knepley Vec coordinates; 1546cfb853baSMatthew G. Knepley PetscSection coordSection; 1547cfb853baSMatthew G. Knepley DMLabel cutLabel = NULL; 1548cfb853baSMatthew G. Knepley PetscBool cutMarker = PETSC_FALSE; 1549cfb853baSMatthew G. Knepley PetscBool periodic = PETSC_FALSE; 1550cfb853baSMatthew G. Knepley PetscInt numCells = 1, c; 1551cfb853baSMatthew G. Knepley PetscInt numVertices = 1, v; 1552cfb853baSMatthew G. Knepley PetscScalar *coords; 1553cfb853baSMatthew G. Knepley PetscInt *vertices, *vert, *vtmp, *supp, cone[2]; 1554cfb853baSMatthew G. Knepley PetscInt d, e, cell = 0, coordSize; 1555cfb853baSMatthew G. Knepley PetscMPIInt rank; 1556cfb853baSMatthew G. Knepley 1557cfb853baSMatthew G. Knepley PetscFunctionBegin; 1558cfb853baSMatthew G. Knepley PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 1559cfb853baSMatthew G. Knepley PetscCall(DMSetDimension(dm, dim)); 1560cfb853baSMatthew G. Knepley PetscCall(PetscCalloc4(dim, &vertices, dim, &vert, dim, &vtmp, 2 * dim, &supp)); 1561cfb853baSMatthew G. Knepley PetscCall(DMCreateLabel(dm, "marker")); 1562cfb853baSMatthew G. Knepley PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL)); 1563cfb853baSMatthew G. Knepley for (d = 0; d < dim; ++d) periodic = (periodic || bd[d] == DM_BOUNDARY_PERIODIC) ? PETSC_TRUE : PETSC_FALSE; 1564cfb853baSMatthew G. Knepley if (periodic && cutMarker) { 1565cfb853baSMatthew G. Knepley PetscCall(DMCreateLabel(dm, "periodic_cut")); 1566cfb853baSMatthew G. Knepley PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel)); 1567cfb853baSMatthew G. Knepley } 1568cfb853baSMatthew G. Knepley for (d = 0; d < dim; ++d) PetscCheck(bd[d] == DM_BOUNDARY_PERIODIC, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Hypercubic mesh must be periodic now"); 1569cfb853baSMatthew G. Knepley for (d = 0; d < dim; ++d) { 1570cfb853baSMatthew G. Knepley vertices[d] = edges[d]; 1571cfb853baSMatthew G. Knepley numVertices *= vertices[d]; 1572cfb853baSMatthew G. Knepley } 1573cfb853baSMatthew G. Knepley numCells = numVertices * dim; 1574cfb853baSMatthew G. Knepley PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices)); 1575cfb853baSMatthew G. Knepley for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, 2)); 1576cfb853baSMatthew G. Knepley for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetSupportSize(dm, v, 2 * dim)); 1577cfb853baSMatthew G. Knepley /* TODO Loop over boundary and reset support sizes */ 1578cfb853baSMatthew G. Knepley PetscCall(DMSetUp(dm)); /* Allocate space for cones and supports */ 1579cfb853baSMatthew G. Knepley /* Build cell cones and vertex supports */ 1580cfb853baSMatthew G. Knepley PetscCall(DMCreateLabel(dm, "celltype")); 1581cfb853baSMatthew G. Knepley while (vert[dim - 1] < vertices[dim - 1]) { 1582cfb853baSMatthew G. Knepley const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert) + numCells; 1583cfb853baSMatthew G. Knepley PetscInt s = 0; 1584cfb853baSMatthew G. Knepley 15853ba16761SJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex)); 15863ba16761SJacob Faibussowitsch for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d])); 15873ba16761SJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n")); 1588cfb853baSMatthew G. Knepley PetscCall(DMPlexSetCellType(dm, vertex, DM_POLYTOPE_POINT)); 1589cfb853baSMatthew G. Knepley for (d = 0; d < dim; ++d) { 1590cfb853baSMatthew G. Knepley for (e = 0; e < dim; ++e) vtmp[e] = vert[e]; 1591cfb853baSMatthew G. Knepley vtmp[d] = (vert[d] + 1) % vertices[d]; 1592cfb853baSMatthew G. Knepley cone[0] = vertex; 1593cfb853baSMatthew G. Knepley cone[1] = TupleToIndex_Private(dim, vertices, vtmp) + numCells; 15943ba16761SJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, " Vertex %" PetscInt_FMT ":", cone[1])); 15953ba16761SJacob Faibussowitsch for (e = 0; e < dim; ++e) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vtmp[e])); 15963ba16761SJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n")); 1597cfb853baSMatthew G. Knepley PetscCall(DMPlexSetCone(dm, cell, cone)); 1598cfb853baSMatthew G. Knepley PetscCall(DMPlexSetCellType(dm, cell, DM_POLYTOPE_SEGMENT)); 15993ba16761SJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, " Edge %" PetscInt_FMT " (%" PetscInt_FMT " %" PetscInt_FMT ")\n", cell, cone[0], cone[1])); 1600cfb853baSMatthew G. Knepley ++cell; 1601cfb853baSMatthew G. Knepley } 1602cfb853baSMatthew G. Knepley for (d = 0; d < dim; ++d) { 1603cfb853baSMatthew G. Knepley for (e = 0; e < dim; ++e) vtmp[e] = vert[e]; 1604cfb853baSMatthew G. Knepley vtmp[d] = (vert[d] + vertices[d] - 1) % vertices[d]; 1605cfb853baSMatthew G. Knepley supp[s++] = TupleToIndex_Private(dim, vertices, vtmp) * dim + d; 1606cfb853baSMatthew G. Knepley supp[s++] = (vertex - numCells) * dim + d; 1607cfb853baSMatthew G. Knepley PetscCall(DMPlexSetSupport(dm, vertex, supp)); 1608cfb853baSMatthew G. Knepley } 1609cfb853baSMatthew G. Knepley PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert)); 1610cfb853baSMatthew G. Knepley } 1611cfb853baSMatthew G. Knepley PetscCall(DMPlexStratify(dm)); 1612cfb853baSMatthew G. Knepley /* Build coordinates */ 1613cfb853baSMatthew G. Knepley PetscCall(DMGetCoordinateSection(dm, &coordSection)); 1614cfb853baSMatthew G. Knepley PetscCall(PetscSectionSetNumFields(coordSection, 1)); 1615cfb853baSMatthew G. Knepley PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim)); 1616cfb853baSMatthew G. Knepley PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices)); 1617cfb853baSMatthew G. Knepley for (v = numCells; v < numCells + numVertices; ++v) { 1618cfb853baSMatthew G. Knepley PetscCall(PetscSectionSetDof(coordSection, v, dim)); 1619cfb853baSMatthew G. Knepley PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim)); 1620cfb853baSMatthew G. Knepley } 1621cfb853baSMatthew G. Knepley PetscCall(PetscSectionSetUp(coordSection)); 1622cfb853baSMatthew G. Knepley PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 1623cfb853baSMatthew G. Knepley PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates)); 1624cfb853baSMatthew G. Knepley PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 1625cfb853baSMatthew G. Knepley PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 1626cfb853baSMatthew G. Knepley PetscCall(VecSetBlockSize(coordinates, dim)); 1627cfb853baSMatthew G. Knepley PetscCall(VecSetType(coordinates, VECSTANDARD)); 1628cfb853baSMatthew G. Knepley PetscCall(VecGetArray(coordinates, &coords)); 1629cfb853baSMatthew G. Knepley for (d = 0; d < dim; ++d) vert[d] = 0; 1630cfb853baSMatthew G. Knepley while (vert[dim - 1] < vertices[dim - 1]) { 1631cfb853baSMatthew G. Knepley const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert); 1632cfb853baSMatthew G. Knepley 1633cfb853baSMatthew G. Knepley for (d = 0; d < dim; ++d) coords[vertex * dim + d] = lower[d] + ((upper[d] - lower[d]) / vertices[d]) * vert[d]; 16343ba16761SJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex)); 16353ba16761SJacob Faibussowitsch for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d])); 16363ba16761SJacob Faibussowitsch for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(coords[vertex * dim + d]))); 16373ba16761SJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n")); 1638cfb853baSMatthew G. Knepley PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert)); 1639cfb853baSMatthew G. Knepley } 1640cfb853baSMatthew G. Knepley PetscCall(VecRestoreArray(coordinates, &coords)); 1641cfb853baSMatthew G. Knepley PetscCall(DMSetCoordinatesLocal(dm, coordinates)); 1642cfb853baSMatthew G. Knepley PetscCall(VecDestroy(&coordinates)); 1643cfb853baSMatthew G. Knepley PetscCall(PetscFree4(vertices, vert, vtmp, supp)); 1644cfb853baSMatthew G. Knepley //PetscCall(DMSetPeriodicity(dm, NULL, lower, upper)); 1645cfb853baSMatthew G. Knepley // Attach the extent 1646cfb853baSMatthew G. Knepley { 1647cfb853baSMatthew G. Knepley PetscContainer c; 1648cfb853baSMatthew G. Knepley PetscInt *extent; 1649cfb853baSMatthew G. Knepley 1650cfb853baSMatthew G. Knepley PetscCall(PetscMalloc1(dim, &extent)); 1651cfb853baSMatthew G. Knepley for (PetscInt d = 0; d < dim; ++d) extent[d] = edges[d]; 1652cfb853baSMatthew G. Knepley PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c)); 1653cfb853baSMatthew G. Knepley PetscCall(PetscContainerSetUserDestroy(c, DestroyExtent_Private)); 1654cfb853baSMatthew G. Knepley PetscCall(PetscContainerSetPointer(c, extent)); 1655cfb853baSMatthew G. Knepley PetscCall(PetscObjectCompose((PetscObject)dm, "_extent", (PetscObject)c)); 1656cfb853baSMatthew G. Knepley PetscCall(PetscContainerDestroy(&c)); 1657cfb853baSMatthew G. Knepley } 16583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1659cfb853baSMatthew G. Knepley } 1660cfb853baSMatthew G. Knepley 1661cfb853baSMatthew G. Knepley /*@C 1662aaa8cc7dSPierre Jolivet DMPlexCreateHypercubicMesh - Creates a periodic mesh on the tensor product of unit intervals using only vertices and edges. 1663cfb853baSMatthew G. Knepley 1664cfb853baSMatthew G. Knepley Collective 1665cfb853baSMatthew G. Knepley 1666cfb853baSMatthew G. Knepley Input Parameters: 1667cfb853baSMatthew G. Knepley + comm - The communicator for the DM object 1668cfb853baSMatthew G. Knepley . dim - The spatial dimension 166920f4b53cSBarry Smith . edges - Number of edges per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D 167020f4b53cSBarry Smith . lower - The lower left corner, or `NULL` for (0, 0, 0) 167120f4b53cSBarry Smith - upper - The upper right corner, or `NULL` for (1, 1, 1) 1672cfb853baSMatthew G. Knepley 1673cfb853baSMatthew G. Knepley Output Parameter: 1674cfb853baSMatthew G. Knepley . dm - The DM object 1675cfb853baSMatthew G. Knepley 167620f4b53cSBarry Smith Level: beginner 167720f4b53cSBarry Smith 167820f4b53cSBarry Smith Note: 167920f4b53cSBarry Smith If you want to customize this mesh using options, you just need to 168020f4b53cSBarry Smith .vb 168120f4b53cSBarry Smith DMCreate(comm, &dm); 168220f4b53cSBarry Smith DMSetType(dm, DMPLEX); 168320f4b53cSBarry Smith DMSetFromOptions(dm); 168420f4b53cSBarry Smith .ve 168520f4b53cSBarry Smith and use the options on the `DMSetFromOptions()` page. 1686cfb853baSMatthew G. Knepley 1687cfb853baSMatthew G. Knepley The vertices are numbered is lexicographic order, and the dim edges exiting a vertex in the positive orthant are number consecutively, 168820f4b53cSBarry Smith .vb 168920f4b53cSBarry Smith 18--0-19--2-20--4-18 169020f4b53cSBarry Smith | | | | 169120f4b53cSBarry Smith 13 15 17 13 169220f4b53cSBarry Smith | | | | 169320f4b53cSBarry Smith 24-12-25-14-26-16-24 169420f4b53cSBarry Smith | | | | 169520f4b53cSBarry Smith 7 9 11 7 169620f4b53cSBarry Smith | | | | 169720f4b53cSBarry Smith 21--6-22--8-23-10-21 169820f4b53cSBarry Smith | | | | 169920f4b53cSBarry Smith 1 3 5 1 170020f4b53cSBarry Smith | | | | 170120f4b53cSBarry Smith 18--0-19--2-20--4-18 170220f4b53cSBarry Smith .ve 1703cfb853baSMatthew G. Knepley 170476fbde31SPierre Jolivet .seealso: `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()` 1705cfb853baSMatthew G. Knepley @*/ 1706cfb853baSMatthew G. Knepley PetscErrorCode DMPlexCreateHypercubicMesh(MPI_Comm comm, PetscInt dim, const PetscInt edges[], const PetscReal lower[], const PetscReal upper[], DM *dm) 1707cfb853baSMatthew G. Knepley { 1708cfb853baSMatthew G. Knepley PetscInt *edg; 1709cfb853baSMatthew G. Knepley PetscReal *low, *upp; 1710cfb853baSMatthew G. Knepley DMBoundaryType *bdt; 1711cfb853baSMatthew G. Knepley PetscInt d; 1712cfb853baSMatthew G. Knepley 1713cfb853baSMatthew G. Knepley PetscFunctionBegin; 1714cfb853baSMatthew G. Knepley PetscCall(DMCreate(comm, dm)); 1715cfb853baSMatthew G. Knepley PetscCall(DMSetType(*dm, DMPLEX)); 1716cfb853baSMatthew G. Knepley PetscCall(PetscMalloc4(dim, &edg, dim, &low, dim, &upp, dim, &bdt)); 1717cfb853baSMatthew G. Knepley for (d = 0; d < dim; ++d) { 1718cfb853baSMatthew G. Knepley edg[d] = edges ? edges[d] : 1; 1719cfb853baSMatthew G. Knepley low[d] = lower ? lower[d] : 0.; 1720cfb853baSMatthew G. Knepley upp[d] = upper ? upper[d] : 1.; 1721cfb853baSMatthew G. Knepley bdt[d] = DM_BOUNDARY_PERIODIC; 1722cfb853baSMatthew G. Knepley } 1723cfb853baSMatthew G. Knepley PetscCall(DMPlexCreateHypercubicMesh_Internal(*dm, dim, low, upp, edg, bdt)); 1724cfb853baSMatthew G. Knepley PetscCall(PetscFree4(edg, low, upp, bdt)); 17253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1726cfb853baSMatthew G. Knepley } 1727cfb853baSMatthew G. Knepley 1728cc4c1da9SBarry Smith /*@ 1729a1cb98faSBarry Smith DMPlexSetOptionsPrefix - Sets the prefix used for searching for all `DM` options in the database. 1730a9074c1eSMatthew G. Knepley 173120f4b53cSBarry Smith Logically Collective 1732a9074c1eSMatthew G. Knepley 1733a9074c1eSMatthew G. Knepley Input Parameters: 173420f4b53cSBarry Smith + dm - the `DM` context 1735a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names 1736a9074c1eSMatthew G. Knepley 1737a1cb98faSBarry Smith Level: advanced 1738a1cb98faSBarry Smith 1739a1cb98faSBarry Smith Note: 1740a9074c1eSMatthew G. Knepley A hyphen (-) must NOT be given at the beginning of the prefix name. 1741a9074c1eSMatthew G. Knepley The first character of all runtime options is AUTOMATICALLY the hyphen. 1742a9074c1eSMatthew G. Knepley 17431cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `SNESSetFromOptions()` 1744a9074c1eSMatthew G. Knepley @*/ 1745d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[]) 1746d71ae5a4SJacob Faibussowitsch { 1747a9074c1eSMatthew G. Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 1748a9074c1eSMatthew G. Knepley 1749a9074c1eSMatthew G. Knepley PetscFunctionBegin; 1750a9074c1eSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 17519566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix)); 17529566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mesh->partitioner, prefix)); 17533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1754a9074c1eSMatthew G. Knepley } 1755a9074c1eSMatthew G. Knepley 17569318fe57SMatthew G. Knepley /* Remap geometry to cylinder 175761a622f3SMatthew G. Knepley TODO: This only works for a single refinement, then it is broken 175861a622f3SMatthew G. Knepley 17599318fe57SMatthew G. Knepley Interior square: Linear interpolation is correct 17609318fe57SMatthew G. Knepley The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing 17619318fe57SMatthew G. Knepley such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance 17620510c589SMatthew G. Knepley 17639318fe57SMatthew G. Knepley phi = arctan(y/x) 17649318fe57SMatthew G. Knepley d_close = sqrt(1/8 + 1/4 sin^2(phi)) 17659318fe57SMatthew G. Knepley d_far = sqrt(1/2 + sin^2(phi)) 17660510c589SMatthew G. Knepley 17679318fe57SMatthew G. Knepley so we remap them using 17680510c589SMatthew G. Knepley 17699318fe57SMatthew G. Knepley x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close) 17709318fe57SMatthew G. Knepley y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close) 17710510c589SMatthew G. Knepley 17729318fe57SMatthew G. Knepley If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y. 17739318fe57SMatthew G. Knepley */ 1774d71ae5a4SJacob Faibussowitsch static void snapToCylinder(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[]) 1775d71ae5a4SJacob Faibussowitsch { 17769318fe57SMatthew G. Knepley const PetscReal dis = 1.0 / PetscSqrtReal(2.0); 17779318fe57SMatthew G. Knepley const PetscReal ds2 = 0.5 * dis; 177822cc497dSMatthew G. Knepley 17799318fe57SMatthew G. Knepley if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) { 17809318fe57SMatthew G. Knepley f0[0] = u[0]; 17819318fe57SMatthew G. Knepley f0[1] = u[1]; 17829318fe57SMatthew G. Knepley } else { 17839318fe57SMatthew G. Knepley PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc; 17840510c589SMatthew G. Knepley 17859318fe57SMatthew G. Knepley x = PetscRealPart(u[0]); 17869318fe57SMatthew G. Knepley y = PetscRealPart(u[1]); 17879318fe57SMatthew G. Knepley phi = PetscAtan2Real(y, x); 17889318fe57SMatthew G. Knepley sinp = PetscSinReal(phi); 17899318fe57SMatthew G. Knepley cosp = PetscCosReal(phi); 17909318fe57SMatthew G. Knepley if ((PetscAbsReal(phi) > PETSC_PI / 4.0) && (PetscAbsReal(phi) < 3.0 * PETSC_PI / 4.0)) { 17919318fe57SMatthew G. Knepley dc = PetscAbsReal(ds2 / sinp); 17929318fe57SMatthew G. Knepley df = PetscAbsReal(dis / sinp); 17939318fe57SMatthew G. Knepley xc = ds2 * x / PetscAbsReal(y); 17949318fe57SMatthew G. Knepley yc = ds2 * PetscSignReal(y); 17959318fe57SMatthew G. Knepley } else { 17969318fe57SMatthew G. Knepley dc = PetscAbsReal(ds2 / cosp); 17979318fe57SMatthew G. Knepley df = PetscAbsReal(dis / cosp); 17989318fe57SMatthew G. Knepley xc = ds2 * PetscSignReal(x); 17999318fe57SMatthew G. Knepley yc = ds2 * y / PetscAbsReal(x); 18009318fe57SMatthew G. Knepley } 18019318fe57SMatthew G. Knepley f0[0] = xc + (u[0] - xc) * (1.0 - dc) / (df - dc); 18029318fe57SMatthew G. Knepley f0[1] = yc + (u[1] - yc) * (1.0 - dc) / (df - dc); 18039318fe57SMatthew G. Knepley } 18049318fe57SMatthew G. Knepley f0[2] = u[2]; 18059318fe57SMatthew G. Knepley } 18060510c589SMatthew G. Knepley 1807d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ) 1808d71ae5a4SJacob Faibussowitsch { 18090510c589SMatthew G. Knepley const PetscInt dim = 3; 18109318fe57SMatthew G. Knepley PetscInt numCells, numVertices; 1811d8c47e87SMatthew G. Knepley PetscMPIInt rank; 18120510c589SMatthew G. Knepley 18130510c589SMatthew G. Knepley PetscFunctionBegin; 181446139095SJed Brown PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0)); 18159566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 18169566063dSJacob Faibussowitsch PetscCall(DMSetDimension(dm, dim)); 18170510c589SMatthew G. Knepley /* Create topology */ 18180510c589SMatthew G. Knepley { 18190510c589SMatthew G. Knepley PetscInt cone[8], c; 18200510c589SMatthew G. Knepley 1821dd400576SPatrick Sanan numCells = rank == 0 ? 5 : 0; 1822dd400576SPatrick Sanan numVertices = rank == 0 ? 16 : 0; 1823006a8963SMatthew G. Knepley if (periodicZ == DM_BOUNDARY_PERIODIC) { 1824ae8bcbbbSMatthew G. Knepley numCells *= 3; 1825dd400576SPatrick Sanan numVertices = rank == 0 ? 24 : 0; 1826006a8963SMatthew G. Knepley } 18279566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices)); 18289566063dSJacob Faibussowitsch for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8)); 18299566063dSJacob Faibussowitsch PetscCall(DMSetUp(dm)); 1830dd400576SPatrick Sanan if (rank == 0) { 1831006a8963SMatthew G. Knepley if (periodicZ == DM_BOUNDARY_PERIODIC) { 18329371c9d4SSatish Balay cone[0] = 15; 18339371c9d4SSatish Balay cone[1] = 18; 18349371c9d4SSatish Balay cone[2] = 17; 18359371c9d4SSatish Balay cone[3] = 16; 18369371c9d4SSatish Balay cone[4] = 31; 18379371c9d4SSatish Balay cone[5] = 32; 18389371c9d4SSatish Balay cone[6] = 33; 18399371c9d4SSatish Balay cone[7] = 34; 18409566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 0, cone)); 18419371c9d4SSatish Balay cone[0] = 16; 18429371c9d4SSatish Balay cone[1] = 17; 18439371c9d4SSatish Balay cone[2] = 24; 18449371c9d4SSatish Balay cone[3] = 23; 18459371c9d4SSatish Balay cone[4] = 32; 18469371c9d4SSatish Balay cone[5] = 36; 18479371c9d4SSatish Balay cone[6] = 37; 18489371c9d4SSatish Balay cone[7] = 33; /* 22 25 26 21 */ 18499566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 1, cone)); 18509371c9d4SSatish Balay cone[0] = 18; 18519371c9d4SSatish Balay cone[1] = 27; 18529371c9d4SSatish Balay cone[2] = 24; 18539371c9d4SSatish Balay cone[3] = 17; 18549371c9d4SSatish Balay cone[4] = 34; 18559371c9d4SSatish Balay cone[5] = 33; 18569371c9d4SSatish Balay cone[6] = 37; 18579371c9d4SSatish Balay cone[7] = 38; 18589566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 2, cone)); 18599371c9d4SSatish Balay cone[0] = 29; 18609371c9d4SSatish Balay cone[1] = 27; 18619371c9d4SSatish Balay cone[2] = 18; 18629371c9d4SSatish Balay cone[3] = 15; 18639371c9d4SSatish Balay cone[4] = 35; 18649371c9d4SSatish Balay cone[5] = 31; 18659371c9d4SSatish Balay cone[6] = 34; 18669371c9d4SSatish Balay cone[7] = 38; 18679566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 3, cone)); 18689371c9d4SSatish Balay cone[0] = 29; 18699371c9d4SSatish Balay cone[1] = 15; 18709371c9d4SSatish Balay cone[2] = 16; 18719371c9d4SSatish Balay cone[3] = 23; 18729371c9d4SSatish Balay cone[4] = 35; 18739371c9d4SSatish Balay cone[5] = 36; 18749371c9d4SSatish Balay cone[6] = 32; 18759371c9d4SSatish Balay cone[7] = 31; 18769566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 4, cone)); 1877006a8963SMatthew G. Knepley 18789371c9d4SSatish Balay cone[0] = 31; 18799371c9d4SSatish Balay cone[1] = 34; 18809371c9d4SSatish Balay cone[2] = 33; 18819371c9d4SSatish Balay cone[3] = 32; 18829371c9d4SSatish Balay cone[4] = 19; 18839371c9d4SSatish Balay cone[5] = 22; 18849371c9d4SSatish Balay cone[6] = 21; 18859371c9d4SSatish Balay cone[7] = 20; 18869566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 5, cone)); 18879371c9d4SSatish Balay cone[0] = 32; 18889371c9d4SSatish Balay cone[1] = 33; 18899371c9d4SSatish Balay cone[2] = 37; 18909371c9d4SSatish Balay cone[3] = 36; 18919371c9d4SSatish Balay cone[4] = 22; 18929371c9d4SSatish Balay cone[5] = 25; 18939371c9d4SSatish Balay cone[6] = 26; 18949371c9d4SSatish Balay cone[7] = 21; 18959566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 6, cone)); 18969371c9d4SSatish Balay cone[0] = 34; 18979371c9d4SSatish Balay cone[1] = 38; 18989371c9d4SSatish Balay cone[2] = 37; 18999371c9d4SSatish Balay cone[3] = 33; 19009371c9d4SSatish Balay cone[4] = 20; 19019371c9d4SSatish Balay cone[5] = 21; 19029371c9d4SSatish Balay cone[6] = 26; 19039371c9d4SSatish Balay cone[7] = 28; 19049566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 7, cone)); 19059371c9d4SSatish Balay cone[0] = 35; 19069371c9d4SSatish Balay cone[1] = 38; 19079371c9d4SSatish Balay cone[2] = 34; 19089371c9d4SSatish Balay cone[3] = 31; 19099371c9d4SSatish Balay cone[4] = 30; 19109371c9d4SSatish Balay cone[5] = 19; 19119371c9d4SSatish Balay cone[6] = 20; 19129371c9d4SSatish Balay cone[7] = 28; 19139566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 8, cone)); 19149371c9d4SSatish Balay cone[0] = 35; 19159371c9d4SSatish Balay cone[1] = 31; 19169371c9d4SSatish Balay cone[2] = 32; 19179371c9d4SSatish Balay cone[3] = 36; 19189371c9d4SSatish Balay cone[4] = 30; 19199371c9d4SSatish Balay cone[5] = 25; 19209371c9d4SSatish Balay cone[6] = 22; 19219371c9d4SSatish Balay cone[7] = 19; 19229566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 9, cone)); 1923ae8bcbbbSMatthew G. Knepley 19249371c9d4SSatish Balay cone[0] = 19; 19259371c9d4SSatish Balay cone[1] = 20; 19269371c9d4SSatish Balay cone[2] = 21; 19279371c9d4SSatish Balay cone[3] = 22; 19289371c9d4SSatish Balay cone[4] = 15; 19299371c9d4SSatish Balay cone[5] = 16; 19309371c9d4SSatish Balay cone[6] = 17; 19319371c9d4SSatish Balay cone[7] = 18; 19329566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 10, cone)); 19339371c9d4SSatish Balay cone[0] = 22; 19349371c9d4SSatish Balay cone[1] = 21; 19359371c9d4SSatish Balay cone[2] = 26; 19369371c9d4SSatish Balay cone[3] = 25; 19379371c9d4SSatish Balay cone[4] = 16; 19389371c9d4SSatish Balay cone[5] = 23; 19399371c9d4SSatish Balay cone[6] = 24; 19409371c9d4SSatish Balay cone[7] = 17; 19419566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 11, cone)); 19429371c9d4SSatish Balay cone[0] = 20; 19439371c9d4SSatish Balay cone[1] = 28; 19449371c9d4SSatish Balay cone[2] = 26; 19459371c9d4SSatish Balay cone[3] = 21; 19469371c9d4SSatish Balay cone[4] = 18; 19479371c9d4SSatish Balay cone[5] = 17; 19489371c9d4SSatish Balay cone[6] = 24; 19499371c9d4SSatish Balay cone[7] = 27; 19509566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 12, cone)); 19519371c9d4SSatish Balay cone[0] = 30; 19529371c9d4SSatish Balay cone[1] = 28; 19539371c9d4SSatish Balay cone[2] = 20; 19549371c9d4SSatish Balay cone[3] = 19; 19559371c9d4SSatish Balay cone[4] = 29; 19569371c9d4SSatish Balay cone[5] = 15; 19579371c9d4SSatish Balay cone[6] = 18; 19589371c9d4SSatish Balay cone[7] = 27; 19599566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 13, cone)); 19609371c9d4SSatish Balay cone[0] = 30; 19619371c9d4SSatish Balay cone[1] = 19; 19629371c9d4SSatish Balay cone[2] = 22; 19639371c9d4SSatish Balay cone[3] = 25; 19649371c9d4SSatish Balay cone[4] = 29; 19659371c9d4SSatish Balay cone[5] = 23; 19669371c9d4SSatish Balay cone[6] = 16; 19679371c9d4SSatish Balay cone[7] = 15; 19689566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 14, cone)); 1969006a8963SMatthew G. Knepley } else { 19709371c9d4SSatish Balay cone[0] = 5; 19719371c9d4SSatish Balay cone[1] = 8; 19729371c9d4SSatish Balay cone[2] = 7; 19739371c9d4SSatish Balay cone[3] = 6; 19749371c9d4SSatish Balay cone[4] = 9; 19759371c9d4SSatish Balay cone[5] = 12; 19769371c9d4SSatish Balay cone[6] = 11; 19779371c9d4SSatish Balay cone[7] = 10; 19789566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 0, cone)); 19799371c9d4SSatish Balay cone[0] = 6; 19809371c9d4SSatish Balay cone[1] = 7; 19819371c9d4SSatish Balay cone[2] = 14; 19829371c9d4SSatish Balay cone[3] = 13; 19839371c9d4SSatish Balay cone[4] = 12; 19849371c9d4SSatish Balay cone[5] = 15; 19859371c9d4SSatish Balay cone[6] = 16; 19869371c9d4SSatish Balay cone[7] = 11; 19879566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 1, cone)); 19889371c9d4SSatish Balay cone[0] = 8; 19899371c9d4SSatish Balay cone[1] = 17; 19909371c9d4SSatish Balay cone[2] = 14; 19919371c9d4SSatish Balay cone[3] = 7; 19929371c9d4SSatish Balay cone[4] = 10; 19939371c9d4SSatish Balay cone[5] = 11; 19949371c9d4SSatish Balay cone[6] = 16; 19959371c9d4SSatish Balay cone[7] = 18; 19969566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 2, cone)); 19979371c9d4SSatish Balay cone[0] = 19; 19989371c9d4SSatish Balay cone[1] = 17; 19999371c9d4SSatish Balay cone[2] = 8; 20009371c9d4SSatish Balay cone[3] = 5; 20019371c9d4SSatish Balay cone[4] = 20; 20029371c9d4SSatish Balay cone[5] = 9; 20039371c9d4SSatish Balay cone[6] = 10; 20049371c9d4SSatish Balay cone[7] = 18; 20059566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 3, cone)); 20069371c9d4SSatish Balay cone[0] = 19; 20079371c9d4SSatish Balay cone[1] = 5; 20089371c9d4SSatish Balay cone[2] = 6; 20099371c9d4SSatish Balay cone[3] = 13; 20109371c9d4SSatish Balay cone[4] = 20; 20119371c9d4SSatish Balay cone[5] = 15; 20129371c9d4SSatish Balay cone[6] = 12; 20139371c9d4SSatish Balay cone[7] = 9; 20149566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 4, cone)); 2015006a8963SMatthew G. Knepley } 2016d8c47e87SMatthew G. Knepley } 20179566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dm)); 20189566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dm)); 20190510c589SMatthew G. Knepley } 2020dbc1dc17SMatthew G. Knepley /* Create cube geometry */ 20210510c589SMatthew G. Knepley { 20220510c589SMatthew G. Knepley Vec coordinates; 20230510c589SMatthew G. Knepley PetscSection coordSection; 20240510c589SMatthew G. Knepley PetscScalar *coords; 20250510c589SMatthew G. Knepley PetscInt coordSize, v; 20260510c589SMatthew G. Knepley const PetscReal dis = 1.0 / PetscSqrtReal(2.0); 20270510c589SMatthew G. Knepley const PetscReal ds2 = dis / 2.0; 20280510c589SMatthew G. Knepley 20290510c589SMatthew G. Knepley /* Build coordinates */ 20309566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 20319566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 20329566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim)); 20339566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices)); 20340510c589SMatthew G. Knepley for (v = numCells; v < numCells + numVertices; ++v) { 20359566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, dim)); 20369566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim)); 20370510c589SMatthew G. Knepley } 20389566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 20399566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 20409566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates)); 20419566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 20429566063dSJacob Faibussowitsch PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 20439566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(coordinates, dim)); 20449566063dSJacob Faibussowitsch PetscCall(VecSetType(coordinates, VECSTANDARD)); 20459566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 2046dd400576SPatrick Sanan if (rank == 0) { 20479371c9d4SSatish Balay coords[0 * dim + 0] = -ds2; 20489371c9d4SSatish Balay coords[0 * dim + 1] = -ds2; 20499371c9d4SSatish Balay coords[0 * dim + 2] = 0.0; 20509371c9d4SSatish Balay coords[1 * dim + 0] = ds2; 20519371c9d4SSatish Balay coords[1 * dim + 1] = -ds2; 20529371c9d4SSatish Balay coords[1 * dim + 2] = 0.0; 20539371c9d4SSatish Balay coords[2 * dim + 0] = ds2; 20549371c9d4SSatish Balay coords[2 * dim + 1] = ds2; 20559371c9d4SSatish Balay coords[2 * dim + 2] = 0.0; 20569371c9d4SSatish Balay coords[3 * dim + 0] = -ds2; 20579371c9d4SSatish Balay coords[3 * dim + 1] = ds2; 20589371c9d4SSatish Balay coords[3 * dim + 2] = 0.0; 20599371c9d4SSatish Balay coords[4 * dim + 0] = -ds2; 20609371c9d4SSatish Balay coords[4 * dim + 1] = -ds2; 20619371c9d4SSatish Balay coords[4 * dim + 2] = 1.0; 20629371c9d4SSatish Balay coords[5 * dim + 0] = -ds2; 20639371c9d4SSatish Balay coords[5 * dim + 1] = ds2; 20649371c9d4SSatish Balay coords[5 * dim + 2] = 1.0; 20659371c9d4SSatish Balay coords[6 * dim + 0] = ds2; 20669371c9d4SSatish Balay coords[6 * dim + 1] = ds2; 20679371c9d4SSatish Balay coords[6 * dim + 2] = 1.0; 20689371c9d4SSatish Balay coords[7 * dim + 0] = ds2; 20699371c9d4SSatish Balay coords[7 * dim + 1] = -ds2; 20709371c9d4SSatish Balay coords[7 * dim + 2] = 1.0; 20719371c9d4SSatish Balay coords[8 * dim + 0] = dis; 20729371c9d4SSatish Balay coords[8 * dim + 1] = -dis; 20739371c9d4SSatish Balay coords[8 * dim + 2] = 0.0; 20749371c9d4SSatish Balay coords[9 * dim + 0] = dis; 20759371c9d4SSatish Balay coords[9 * dim + 1] = dis; 20769371c9d4SSatish Balay coords[9 * dim + 2] = 0.0; 20779371c9d4SSatish Balay coords[10 * dim + 0] = dis; 20789371c9d4SSatish Balay coords[10 * dim + 1] = -dis; 20799371c9d4SSatish Balay coords[10 * dim + 2] = 1.0; 20809371c9d4SSatish Balay coords[11 * dim + 0] = dis; 20819371c9d4SSatish Balay coords[11 * dim + 1] = dis; 20829371c9d4SSatish Balay coords[11 * dim + 2] = 1.0; 20839371c9d4SSatish Balay coords[12 * dim + 0] = -dis; 20849371c9d4SSatish Balay coords[12 * dim + 1] = dis; 20859371c9d4SSatish Balay coords[12 * dim + 2] = 0.0; 20869371c9d4SSatish Balay coords[13 * dim + 0] = -dis; 20879371c9d4SSatish Balay coords[13 * dim + 1] = dis; 20889371c9d4SSatish Balay coords[13 * dim + 2] = 1.0; 20899371c9d4SSatish Balay coords[14 * dim + 0] = -dis; 20909371c9d4SSatish Balay coords[14 * dim + 1] = -dis; 20919371c9d4SSatish Balay coords[14 * dim + 2] = 0.0; 20929371c9d4SSatish Balay coords[15 * dim + 0] = -dis; 20939371c9d4SSatish Balay coords[15 * dim + 1] = -dis; 20949371c9d4SSatish Balay coords[15 * dim + 2] = 1.0; 2095ae8bcbbbSMatthew G. Knepley if (periodicZ == DM_BOUNDARY_PERIODIC) { 20969371c9d4SSatish Balay /* 15 31 19 */ coords[16 * dim + 0] = -ds2; 20979371c9d4SSatish Balay coords[16 * dim + 1] = -ds2; 20989371c9d4SSatish Balay coords[16 * dim + 2] = 0.5; 20999371c9d4SSatish Balay /* 16 32 22 */ coords[17 * dim + 0] = ds2; 21009371c9d4SSatish Balay coords[17 * dim + 1] = -ds2; 21019371c9d4SSatish Balay coords[17 * dim + 2] = 0.5; 21029371c9d4SSatish Balay /* 17 33 21 */ coords[18 * dim + 0] = ds2; 21039371c9d4SSatish Balay coords[18 * dim + 1] = ds2; 21049371c9d4SSatish Balay coords[18 * dim + 2] = 0.5; 21059371c9d4SSatish Balay /* 18 34 20 */ coords[19 * dim + 0] = -ds2; 21069371c9d4SSatish Balay coords[19 * dim + 1] = ds2; 21079371c9d4SSatish Balay coords[19 * dim + 2] = 0.5; 21089371c9d4SSatish Balay /* 29 35 30 */ coords[20 * dim + 0] = -dis; 21099371c9d4SSatish Balay coords[20 * dim + 1] = -dis; 21109371c9d4SSatish Balay coords[20 * dim + 2] = 0.5; 21119371c9d4SSatish Balay /* 23 36 25 */ coords[21 * dim + 0] = dis; 21129371c9d4SSatish Balay coords[21 * dim + 1] = -dis; 21139371c9d4SSatish Balay coords[21 * dim + 2] = 0.5; 21149371c9d4SSatish Balay /* 24 37 26 */ coords[22 * dim + 0] = dis; 21159371c9d4SSatish Balay coords[22 * dim + 1] = dis; 21169371c9d4SSatish Balay coords[22 * dim + 2] = 0.5; 21179371c9d4SSatish Balay /* 27 38 28 */ coords[23 * dim + 0] = -dis; 21189371c9d4SSatish Balay coords[23 * dim + 1] = dis; 21199371c9d4SSatish Balay coords[23 * dim + 2] = 0.5; 2120ae8bcbbbSMatthew G. Knepley } 2121d8c47e87SMatthew G. Knepley } 21229566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 21239566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, coordinates)); 21249566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 21250510c589SMatthew G. Knepley } 2126006a8963SMatthew G. Knepley /* Create periodicity */ 2127006a8963SMatthew G. Knepley if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) { 21286858538eSMatthew G. Knepley PetscReal L[3] = {-1., -1., 0.}; 21296858538eSMatthew G. Knepley PetscReal maxCell[3] = {-1., -1., 0.}; 2130006a8963SMatthew G. Knepley PetscReal lower[3] = {0.0, 0.0, 0.0}; 2131ae8bcbbbSMatthew G. Knepley PetscReal upper[3] = {1.0, 1.0, 1.5}; 21326858538eSMatthew G. Knepley PetscInt numZCells = 3; 2133006a8963SMatthew G. Knepley 21346858538eSMatthew G. Knepley L[2] = upper[2] - lower[2]; 21356858538eSMatthew G. Knepley maxCell[2] = 1.1 * (L[2] / numZCells); 21364fb89dddSMatthew G. Knepley PetscCall(DMSetPeriodicity(dm, maxCell, lower, L)); 2137006a8963SMatthew G. Knepley } 2138dbc1dc17SMatthew G. Knepley { 21399318fe57SMatthew G. Knepley DM cdm; 21409318fe57SMatthew G. Knepley PetscDS cds; 21419318fe57SMatthew G. Knepley PetscScalar c[2] = {1.0, 1.0}; 2142dbc1dc17SMatthew G. Knepley 2143e44f6aebSMatthew G. Knepley PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, snapToCylinder)); 21449566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 21459566063dSJacob Faibussowitsch PetscCall(DMGetDS(cdm, &cds)); 21469566063dSJacob Faibussowitsch PetscCall(PetscDSSetConstants(cds, 2, c)); 2147dbc1dc17SMatthew G. Knepley } 214846139095SJed Brown PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0)); 214946139095SJed Brown 21509318fe57SMatthew G. Knepley /* Wait for coordinate creation before doing in-place modification */ 21519566063dSJacob Faibussowitsch PetscCall(DMPlexInterpolateInPlace_Internal(dm)); 21523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 21530510c589SMatthew G. Knepley } 21540510c589SMatthew G. Knepley 215524119c2aSMatthew G. Knepley /*@ 21569318fe57SMatthew G. Knepley DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra. 215724119c2aSMatthew G. Knepley 2158d083f849SBarry Smith Collective 215924119c2aSMatthew G. Knepley 216024119c2aSMatthew G. Knepley Input Parameters: 2161a1cb98faSBarry Smith + comm - The communicator for the `DM` object 21629318fe57SMatthew G. Knepley - periodicZ - The boundary type for the Z direction 216324119c2aSMatthew G. Knepley 216424119c2aSMatthew G. Knepley Output Parameter: 216520f4b53cSBarry Smith . dm - The `DM` object 216624119c2aSMatthew G. Knepley 216724119c2aSMatthew G. Knepley Level: beginner 216824119c2aSMatthew G. Knepley 2169a1cb98faSBarry Smith Note: 2170a4e35b19SJacob Faibussowitsch Here is the output numbering looking from the bottom of the cylinder\: 2171a1cb98faSBarry Smith .vb 2172a1cb98faSBarry Smith 17-----14 2173a1cb98faSBarry Smith | | 2174a1cb98faSBarry Smith | 2 | 2175a1cb98faSBarry Smith | | 2176a1cb98faSBarry Smith 17-----8-----7-----14 2177a1cb98faSBarry Smith | | | | 2178a1cb98faSBarry Smith | 3 | 0 | 1 | 2179a1cb98faSBarry Smith | | | | 2180a1cb98faSBarry Smith 19-----5-----6-----13 2181a1cb98faSBarry Smith | | 2182a1cb98faSBarry Smith | 4 | 2183a1cb98faSBarry Smith | | 2184a1cb98faSBarry Smith 19-----13 2185a1cb98faSBarry Smith 2186a1cb98faSBarry Smith and up through the top 2187a1cb98faSBarry Smith 2188a1cb98faSBarry Smith 18-----16 2189a1cb98faSBarry Smith | | 2190a1cb98faSBarry Smith | 2 | 2191a1cb98faSBarry Smith | | 2192a1cb98faSBarry Smith 18----10----11-----16 2193a1cb98faSBarry Smith | | | | 2194a1cb98faSBarry Smith | 3 | 0 | 1 | 2195a1cb98faSBarry Smith | | | | 2196a1cb98faSBarry Smith 20-----9----12-----15 2197a1cb98faSBarry Smith | | 2198a1cb98faSBarry Smith | 4 | 2199a1cb98faSBarry Smith | | 2200a1cb98faSBarry Smith 20-----15 2201a1cb98faSBarry Smith .ve 2202a1cb98faSBarry Smith 22031cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()` 220424119c2aSMatthew G. Knepley @*/ 2205d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm) 2206d71ae5a4SJacob Faibussowitsch { 22079318fe57SMatthew G. Knepley PetscFunctionBegin; 22084f572ea9SToby Isaac PetscAssertPointer(dm, 3); 22099566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 22109566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 22119566063dSJacob Faibussowitsch PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ)); 22123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22139318fe57SMatthew G. Knepley } 22149318fe57SMatthew G. Knepley 2215d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate) 2216d71ae5a4SJacob Faibussowitsch { 221724119c2aSMatthew G. Knepley const PetscInt dim = 3; 2218412e9a14SMatthew G. Knepley PetscInt numCells, numVertices, v; 22199fe9f049SMatthew G. Knepley PetscMPIInt rank; 222024119c2aSMatthew G. Knepley 222124119c2aSMatthew G. Knepley PetscFunctionBegin; 222263a3b9bcSJacob Faibussowitsch PetscCheck(n >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n); 222346139095SJed Brown PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0)); 22249566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 22259566063dSJacob Faibussowitsch PetscCall(DMSetDimension(dm, dim)); 2226412e9a14SMatthew G. Knepley /* Must create the celltype label here so that we do not automatically try to compute the types */ 22279566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dm, "celltype")); 222824119c2aSMatthew G. Knepley /* Create topology */ 222924119c2aSMatthew G. Knepley { 223024119c2aSMatthew G. Knepley PetscInt cone[6], c; 223124119c2aSMatthew G. Knepley 2232dd400576SPatrick Sanan numCells = rank == 0 ? n : 0; 2233dd400576SPatrick Sanan numVertices = rank == 0 ? 2 * (n + 1) : 0; 22349566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices)); 22359566063dSJacob Faibussowitsch for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6)); 22369566063dSJacob Faibussowitsch PetscCall(DMSetUp(dm)); 223724119c2aSMatthew G. Knepley for (c = 0; c < numCells; c++) { 22389371c9d4SSatish Balay cone[0] = c + n * 1; 22399371c9d4SSatish Balay cone[1] = (c + 1) % n + n * 1; 22409371c9d4SSatish Balay cone[2] = 0 + 3 * n; 22419371c9d4SSatish Balay cone[3] = c + n * 2; 22429371c9d4SSatish Balay cone[4] = (c + 1) % n + n * 2; 22439371c9d4SSatish Balay cone[5] = 1 + 3 * n; 22449566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, c, cone)); 22459566063dSJacob Faibussowitsch PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR)); 224624119c2aSMatthew G. Knepley } 22479566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dm)); 22489566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dm)); 224924119c2aSMatthew G. Knepley } 225048a46eb9SPierre Jolivet for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT)); 225124119c2aSMatthew G. Knepley /* Create cylinder geometry */ 225224119c2aSMatthew G. Knepley { 225324119c2aSMatthew G. Knepley Vec coordinates; 225424119c2aSMatthew G. Knepley PetscSection coordSection; 225524119c2aSMatthew G. Knepley PetscScalar *coords; 2256412e9a14SMatthew G. Knepley PetscInt coordSize, c; 225724119c2aSMatthew G. Knepley 225824119c2aSMatthew G. Knepley /* Build coordinates */ 22599566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 22609566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 22619566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim)); 22629566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices)); 226324119c2aSMatthew G. Knepley for (v = numCells; v < numCells + numVertices; ++v) { 22649566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, dim)); 22659566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim)); 226624119c2aSMatthew G. Knepley } 22679566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 22689566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 22699566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates)); 22709566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 22719566063dSJacob Faibussowitsch PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 22729566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(coordinates, dim)); 22739566063dSJacob Faibussowitsch PetscCall(VecSetType(coordinates, VECSTANDARD)); 22749566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 227524119c2aSMatthew G. Knepley for (c = 0; c < numCells; c++) { 22769371c9d4SSatish Balay coords[(c + 0 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n); 22779371c9d4SSatish Balay coords[(c + 0 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n); 22789371c9d4SSatish Balay coords[(c + 0 * n) * dim + 2] = 1.0; 22799371c9d4SSatish Balay coords[(c + 1 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n); 22809371c9d4SSatish Balay coords[(c + 1 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n); 22819371c9d4SSatish Balay coords[(c + 1 * n) * dim + 2] = 0.0; 228224119c2aSMatthew G. Knepley } 2283dd400576SPatrick Sanan if (rank == 0) { 22849371c9d4SSatish Balay coords[(2 * n + 0) * dim + 0] = 0.0; 22859371c9d4SSatish Balay coords[(2 * n + 0) * dim + 1] = 0.0; 22869371c9d4SSatish Balay coords[(2 * n + 0) * dim + 2] = 1.0; 22879371c9d4SSatish Balay coords[(2 * n + 1) * dim + 0] = 0.0; 22889371c9d4SSatish Balay coords[(2 * n + 1) * dim + 1] = 0.0; 22899371c9d4SSatish Balay coords[(2 * n + 1) * dim + 2] = 0.0; 22909fe9f049SMatthew G. Knepley } 22919566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 22929566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, coordinates)); 22939566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 229424119c2aSMatthew G. Knepley } 229546139095SJed Brown PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0)); 22969318fe57SMatthew G. Knepley /* Interpolate */ 22979566063dSJacob Faibussowitsch if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm)); 22983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 22999318fe57SMatthew G. Knepley } 23009318fe57SMatthew G. Knepley 23019318fe57SMatthew G. Knepley /*@ 23029318fe57SMatthew G. Knepley DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges. 23039318fe57SMatthew G. Knepley 23049318fe57SMatthew G. Knepley Collective 23059318fe57SMatthew G. Knepley 23069318fe57SMatthew G. Knepley Input Parameters: 2307a1cb98faSBarry Smith + comm - The communicator for the `DM` object 23089318fe57SMatthew G. Knepley . n - The number of wedges around the origin 23099318fe57SMatthew G. Knepley - interpolate - Create edges and faces 23109318fe57SMatthew G. Knepley 23119318fe57SMatthew G. Knepley Output Parameter: 2312a1cb98faSBarry Smith . dm - The `DM` object 23139318fe57SMatthew G. Knepley 23149318fe57SMatthew G. Knepley Level: beginner 23159318fe57SMatthew G. Knepley 23161cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()` 23179318fe57SMatthew G. Knepley @*/ 2318d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm) 2319d71ae5a4SJacob Faibussowitsch { 23209318fe57SMatthew G. Knepley PetscFunctionBegin; 23214f572ea9SToby Isaac PetscAssertPointer(dm, 4); 23229566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 23239566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 23249566063dSJacob Faibussowitsch PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate)); 23253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 232624119c2aSMatthew G. Knepley } 232724119c2aSMatthew G. Knepley 2328d71ae5a4SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[]) 2329d71ae5a4SJacob Faibussowitsch { 233065a81367SMatthew G. Knepley PetscReal prod = 0.0; 233165a81367SMatthew G. Knepley PetscInt i; 233265a81367SMatthew G. Knepley for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]); 233365a81367SMatthew G. Knepley return PetscSqrtReal(prod); 233465a81367SMatthew G. Knepley } 2335dd2b43ebSStefano Zampini 2336d71ae5a4SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[]) 2337d71ae5a4SJacob Faibussowitsch { 233865a81367SMatthew G. Knepley PetscReal prod = 0.0; 233965a81367SMatthew G. Knepley PetscInt i; 234065a81367SMatthew G. Knepley for (i = 0; i < dim; ++i) prod += x[i] * y[i]; 234165a81367SMatthew G. Knepley return prod; 234265a81367SMatthew G. Knepley } 234365a81367SMatthew G. Knepley 234451a74b61SMatthew G. Knepley /* The first constant is the sphere radius */ 2345d71ae5a4SJacob Faibussowitsch static void snapToSphere(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[]) 2346d71ae5a4SJacob Faibussowitsch { 234751a74b61SMatthew G. Knepley PetscReal r = PetscRealPart(constants[0]); 234851a74b61SMatthew G. Knepley PetscReal norm2 = 0.0, fac; 234951a74b61SMatthew G. Knepley PetscInt n = uOff[1] - uOff[0], d; 235051a74b61SMatthew G. Knepley 235151a74b61SMatthew G. Knepley for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d])); 235251a74b61SMatthew G. Knepley fac = r / PetscSqrtReal(norm2); 235351a74b61SMatthew G. Knepley for (d = 0; d < n; ++d) f0[d] = u[d] * fac; 235451a74b61SMatthew G. Knepley } 235551a74b61SMatthew G. Knepley 2356d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R) 2357d71ae5a4SJacob Faibussowitsch { 235865a81367SMatthew G. Knepley const PetscInt embedDim = dim + 1; 235965a81367SMatthew G. Knepley PetscSection coordSection; 236065a81367SMatthew G. Knepley Vec coordinates; 236165a81367SMatthew G. Knepley PetscScalar *coords; 236265a81367SMatthew G. Knepley PetscReal *coordsIn; 236307c565c5SJose E. Roman PetscInt numCells, numEdges, numVerts = 0, firstVertex = 0, v, firstEdge, coordSize, d, e; 236465a81367SMatthew G. Knepley PetscMPIInt rank; 236565a81367SMatthew G. Knepley 236665a81367SMatthew G. Knepley PetscFunctionBegin; 23679318fe57SMatthew G. Knepley PetscValidLogicalCollectiveBool(dm, simplex, 3); 236846139095SJed Brown PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0)); 23699566063dSJacob Faibussowitsch PetscCall(DMSetDimension(dm, dim)); 23709566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(dm, dim + 1)); 23719566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 237265a81367SMatthew G. Knepley switch (dim) { 23735c344501SMatthew G. Knepley case 1: 23745c344501SMatthew G. Knepley numCells = 16; 23755c344501SMatthew G. Knepley numVerts = numCells; 23765c344501SMatthew G. Knepley 23775c344501SMatthew G. Knepley // Build Topology 23785c344501SMatthew G. Knepley PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts)); 23795c344501SMatthew G. Knepley for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim)); 23805c344501SMatthew G. Knepley PetscCall(DMSetUp(dm)); 23815c344501SMatthew G. Knepley for (PetscInt c = 0; c < numCells; ++c) { 23825c344501SMatthew G. Knepley PetscInt cone[2]; 23835c344501SMatthew G. Knepley 23845c344501SMatthew G. Knepley cone[0] = c + numCells; 23855c344501SMatthew G. Knepley cone[1] = (c + 1) % numVerts + numCells; 23865c344501SMatthew G. Knepley PetscCall(DMPlexSetCone(dm, c, cone)); 23875c344501SMatthew G. Knepley } 23885c344501SMatthew G. Knepley PetscCall(DMPlexSymmetrize(dm)); 23895c344501SMatthew G. Knepley PetscCall(DMPlexStratify(dm)); 23905c344501SMatthew G. Knepley PetscCall(PetscMalloc1(numVerts * embedDim, &coordsIn)); 23915c344501SMatthew G. Knepley for (PetscInt v = 0; v < numVerts; ++v) { 23925c344501SMatthew G. Knepley const PetscReal rad = 2. * PETSC_PI * v / numVerts; 23935c344501SMatthew G. Knepley 23945c344501SMatthew G. Knepley coordsIn[v * embedDim + 0] = PetscCosReal(rad); 23955c344501SMatthew G. Knepley coordsIn[v * embedDim + 1] = PetscSinReal(rad); 23965c344501SMatthew G. Knepley } 23975c344501SMatthew G. Knepley break; 239865a81367SMatthew G. Knepley case 2: 239965a81367SMatthew G. Knepley if (simplex) { 240051a74b61SMatthew G. Knepley const PetscReal radius = PetscSqrtReal(1 + PETSC_PHI * PETSC_PHI) / (1.0 + PETSC_PHI); 240151a74b61SMatthew G. Knepley const PetscReal edgeLen = 2.0 / (1.0 + PETSC_PHI) * (R / radius); 240265a81367SMatthew G. Knepley const PetscInt degree = 5; 240351a74b61SMatthew G. Knepley PetscReal vertex[3] = {0.0, 1.0 / (1.0 + PETSC_PHI), PETSC_PHI / (1.0 + PETSC_PHI)}; 240465a81367SMatthew G. Knepley PetscInt s[3] = {1, 1, 1}; 240565a81367SMatthew G. Knepley PetscInt cone[3]; 240607c565c5SJose E. Roman PetscInt *graph; 240765a81367SMatthew G. Knepley 24089371c9d4SSatish Balay vertex[0] *= R / radius; 24099371c9d4SSatish Balay vertex[1] *= R / radius; 24109371c9d4SSatish Balay vertex[2] *= R / radius; 2411dd400576SPatrick Sanan numCells = rank == 0 ? 20 : 0; 2412dd400576SPatrick Sanan numVerts = rank == 0 ? 12 : 0; 241365a81367SMatthew G. Knepley firstVertex = numCells; 241451a74b61SMatthew G. Knepley /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of 241565a81367SMatthew G. Knepley 241665a81367SMatthew G. Knepley (0, \pm 1/\phi+1, \pm \phi/\phi+1) 241765a81367SMatthew G. Knepley 241865a81367SMatthew G. Knepley where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge 241951a74b61SMatthew G. Knepley length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393. 242065a81367SMatthew G. Knepley */ 242165a81367SMatthew G. Knepley /* Construct vertices */ 24229566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn)); 2423dd400576SPatrick Sanan if (rank == 0) { 242407c565c5SJose E. Roman for (PetscInt p = 0, i = 0; p < embedDim; ++p) { 242565a81367SMatthew G. Knepley for (s[1] = -1; s[1] < 2; s[1] += 2) { 242665a81367SMatthew G. Knepley for (s[2] = -1; s[2] < 2; s[2] += 2) { 242765a81367SMatthew G. Knepley for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertex[(d + p) % embedDim]; 242865a81367SMatthew G. Knepley ++i; 242965a81367SMatthew G. Knepley } 243065a81367SMatthew G. Knepley } 243165a81367SMatthew G. Knepley } 243245da822fSValeria Barra } 243365a81367SMatthew G. Knepley /* Construct graph */ 24349566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(numVerts * numVerts, &graph)); 243507c565c5SJose E. Roman for (PetscInt i = 0; i < numVerts; ++i) { 243607c565c5SJose E. Roman PetscInt k = 0; 243707c565c5SJose E. Roman for (PetscInt j = 0; j < numVerts; ++j) { 24389371c9d4SSatish Balay if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) { 24399371c9d4SSatish Balay graph[i * numVerts + j] = 1; 24409371c9d4SSatish Balay ++k; 24419371c9d4SSatish Balay } 244265a81367SMatthew G. Knepley } 244363a3b9bcSJacob Faibussowitsch PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree); 244465a81367SMatthew G. Knepley } 244565a81367SMatthew G. Knepley /* Build Topology */ 24469566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts)); 244707c565c5SJose E. Roman for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim)); 24489566063dSJacob Faibussowitsch PetscCall(DMSetUp(dm)); /* Allocate space for cones */ 244965a81367SMatthew G. Knepley /* Cells */ 245007c565c5SJose E. Roman for (PetscInt i = 0, c = 0; i < numVerts; ++i) { 245107c565c5SJose E. Roman for (PetscInt j = 0; j < i; ++j) { 245207c565c5SJose E. Roman for (PetscInt k = 0; k < j; ++k) { 245365a81367SMatthew G. Knepley if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i]) { 24549371c9d4SSatish Balay cone[0] = firstVertex + i; 24559371c9d4SSatish Balay cone[1] = firstVertex + j; 24569371c9d4SSatish Balay cone[2] = firstVertex + k; 245765a81367SMatthew G. Knepley /* Check orientation */ 245865a81367SMatthew G. Knepley { 24599371c9d4SSatish Balay const PetscInt epsilon[3][3][3] = { 24609371c9d4SSatish Balay {{0, 0, 0}, {0, 0, 1}, {0, -1, 0}}, 24619371c9d4SSatish Balay {{0, 0, -1}, {0, 0, 0}, {1, 0, 0} }, 24629371c9d4SSatish Balay {{0, 1, 0}, {-1, 0, 0}, {0, 0, 0} } 24639371c9d4SSatish Balay }; 246465a81367SMatthew G. Knepley PetscReal normal[3]; 246565a81367SMatthew G. Knepley PetscInt e, f; 246665a81367SMatthew G. Knepley 246765a81367SMatthew G. Knepley for (d = 0; d < embedDim; ++d) { 246865a81367SMatthew G. Knepley normal[d] = 0.0; 246965a81367SMatthew G. Knepley for (e = 0; e < embedDim; ++e) { 2470ad540459SPierre Jolivet for (f = 0; f < embedDim; ++f) normal[d] += epsilon[d][e][f] * (coordsIn[j * embedDim + e] - coordsIn[i * embedDim + e]) * (coordsIn[k * embedDim + f] - coordsIn[i * embedDim + f]); 247165a81367SMatthew G. Knepley } 247265a81367SMatthew G. Knepley } 24739371c9d4SSatish Balay if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) { 24749371c9d4SSatish Balay PetscInt tmp = cone[1]; 24759371c9d4SSatish Balay cone[1] = cone[2]; 24769371c9d4SSatish Balay cone[2] = tmp; 247765a81367SMatthew G. Knepley } 247865a81367SMatthew G. Knepley } 24799566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, c++, cone)); 248065a81367SMatthew G. Knepley } 248165a81367SMatthew G. Knepley } 248265a81367SMatthew G. Knepley } 248365a81367SMatthew G. Knepley } 24849566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dm)); 24859566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dm)); 24869566063dSJacob Faibussowitsch PetscCall(PetscFree(graph)); 248765a81367SMatthew G. Knepley } else { 24882829fed8SMatthew G. Knepley /* 24892829fed8SMatthew G. Knepley 12-21--13 24902829fed8SMatthew G. Knepley | | 24912829fed8SMatthew G. Knepley 25 4 24 24922829fed8SMatthew G. Knepley | | 24932829fed8SMatthew G. Knepley 12-25--9-16--8-24--13 24942829fed8SMatthew G. Knepley | | | | 24952829fed8SMatthew G. Knepley 23 5 17 0 15 3 22 24962829fed8SMatthew G. Knepley | | | | 24972829fed8SMatthew G. Knepley 10-20--6-14--7-19--11 24982829fed8SMatthew G. Knepley | | 24992829fed8SMatthew G. Knepley 20 1 19 25002829fed8SMatthew G. Knepley | | 25012829fed8SMatthew G. Knepley 10-18--11 25022829fed8SMatthew G. Knepley | | 25032829fed8SMatthew G. Knepley 23 2 22 25042829fed8SMatthew G. Knepley | | 25052829fed8SMatthew G. Knepley 12-21--13 25062829fed8SMatthew G. Knepley */ 25072829fed8SMatthew G. Knepley PetscInt cone[4], ornt[4]; 25082829fed8SMatthew G. Knepley 2509dd400576SPatrick Sanan numCells = rank == 0 ? 6 : 0; 2510dd400576SPatrick Sanan numEdges = rank == 0 ? 12 : 0; 2511dd400576SPatrick Sanan numVerts = rank == 0 ? 8 : 0; 251265a81367SMatthew G. Knepley firstVertex = numCells; 251365a81367SMatthew G. Knepley firstEdge = numCells + numVerts; 25142829fed8SMatthew G. Knepley /* Build Topology */ 25159566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dm, 0, numCells + numEdges + numVerts)); 251607c565c5SJose E. Roman for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 4)); 251748a46eb9SPierre Jolivet for (e = firstEdge; e < firstEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2)); 25189566063dSJacob Faibussowitsch PetscCall(DMSetUp(dm)); /* Allocate space for cones */ 2519dd400576SPatrick Sanan if (rank == 0) { 25202829fed8SMatthew G. Knepley /* Cell 0 */ 25219371c9d4SSatish Balay cone[0] = 14; 25229371c9d4SSatish Balay cone[1] = 15; 25239371c9d4SSatish Balay cone[2] = 16; 25249371c9d4SSatish Balay cone[3] = 17; 25259566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 0, cone)); 25269371c9d4SSatish Balay ornt[0] = 0; 25279371c9d4SSatish Balay ornt[1] = 0; 25289371c9d4SSatish Balay ornt[2] = 0; 25299371c9d4SSatish Balay ornt[3] = 0; 25309566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(dm, 0, ornt)); 25312829fed8SMatthew G. Knepley /* Cell 1 */ 25329371c9d4SSatish Balay cone[0] = 18; 25339371c9d4SSatish Balay cone[1] = 19; 25349371c9d4SSatish Balay cone[2] = 14; 25359371c9d4SSatish Balay cone[3] = 20; 25369566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 1, cone)); 25379371c9d4SSatish Balay ornt[0] = 0; 25389371c9d4SSatish Balay ornt[1] = 0; 25399371c9d4SSatish Balay ornt[2] = -1; 25409371c9d4SSatish Balay ornt[3] = 0; 25419566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(dm, 1, ornt)); 25422829fed8SMatthew G. Knepley /* Cell 2 */ 25439371c9d4SSatish Balay cone[0] = 21; 25449371c9d4SSatish Balay cone[1] = 22; 25459371c9d4SSatish Balay cone[2] = 18; 25469371c9d4SSatish Balay cone[3] = 23; 25479566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 2, cone)); 25489371c9d4SSatish Balay ornt[0] = 0; 25499371c9d4SSatish Balay ornt[1] = 0; 25509371c9d4SSatish Balay ornt[2] = -1; 25519371c9d4SSatish Balay ornt[3] = 0; 25529566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(dm, 2, ornt)); 25532829fed8SMatthew G. Knepley /* Cell 3 */ 25549371c9d4SSatish Balay cone[0] = 19; 25559371c9d4SSatish Balay cone[1] = 22; 25569371c9d4SSatish Balay cone[2] = 24; 25579371c9d4SSatish Balay cone[3] = 15; 25589566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 3, cone)); 25599371c9d4SSatish Balay ornt[0] = -1; 25609371c9d4SSatish Balay ornt[1] = -1; 25619371c9d4SSatish Balay ornt[2] = 0; 25629371c9d4SSatish Balay ornt[3] = -1; 25639566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(dm, 3, ornt)); 25642829fed8SMatthew G. Knepley /* Cell 4 */ 25659371c9d4SSatish Balay cone[0] = 16; 25669371c9d4SSatish Balay cone[1] = 24; 25679371c9d4SSatish Balay cone[2] = 21; 25689371c9d4SSatish Balay cone[3] = 25; 25699566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 4, cone)); 25709371c9d4SSatish Balay ornt[0] = -1; 25719371c9d4SSatish Balay ornt[1] = -1; 25729371c9d4SSatish Balay ornt[2] = -1; 25739371c9d4SSatish Balay ornt[3] = 0; 25749566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(dm, 4, ornt)); 25752829fed8SMatthew G. Knepley /* Cell 5 */ 25769371c9d4SSatish Balay cone[0] = 20; 25779371c9d4SSatish Balay cone[1] = 17; 25789371c9d4SSatish Balay cone[2] = 25; 25799371c9d4SSatish Balay cone[3] = 23; 25809566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 5, cone)); 25819371c9d4SSatish Balay ornt[0] = -1; 25829371c9d4SSatish Balay ornt[1] = -1; 25839371c9d4SSatish Balay ornt[2] = -1; 25849371c9d4SSatish Balay ornt[3] = -1; 25859566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(dm, 5, ornt)); 25862829fed8SMatthew G. Knepley /* Edges */ 25879371c9d4SSatish Balay cone[0] = 6; 25889371c9d4SSatish Balay cone[1] = 7; 25899566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 14, cone)); 25909371c9d4SSatish Balay cone[0] = 7; 25919371c9d4SSatish Balay cone[1] = 8; 25929566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 15, cone)); 25939371c9d4SSatish Balay cone[0] = 8; 25949371c9d4SSatish Balay cone[1] = 9; 25959566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 16, cone)); 25969371c9d4SSatish Balay cone[0] = 9; 25979371c9d4SSatish Balay cone[1] = 6; 25989566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 17, cone)); 25999371c9d4SSatish Balay cone[0] = 10; 26009371c9d4SSatish Balay cone[1] = 11; 26019566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 18, cone)); 26029371c9d4SSatish Balay cone[0] = 11; 26039371c9d4SSatish Balay cone[1] = 7; 26049566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 19, cone)); 26059371c9d4SSatish Balay cone[0] = 6; 26069371c9d4SSatish Balay cone[1] = 10; 26079566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 20, cone)); 26089371c9d4SSatish Balay cone[0] = 12; 26099371c9d4SSatish Balay cone[1] = 13; 26109566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 21, cone)); 26119371c9d4SSatish Balay cone[0] = 13; 26129371c9d4SSatish Balay cone[1] = 11; 26139566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 22, cone)); 26149371c9d4SSatish Balay cone[0] = 10; 26159371c9d4SSatish Balay cone[1] = 12; 26169566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 23, cone)); 26179371c9d4SSatish Balay cone[0] = 13; 26189371c9d4SSatish Balay cone[1] = 8; 26199566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 24, cone)); 26209371c9d4SSatish Balay cone[0] = 12; 26219371c9d4SSatish Balay cone[1] = 9; 26229566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, 25, cone)); 262345da822fSValeria Barra } 26249566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dm)); 26259566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dm)); 26262829fed8SMatthew G. Knepley /* Build coordinates */ 26279566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn)); 2628dd400576SPatrick Sanan if (rank == 0) { 26299371c9d4SSatish Balay coordsIn[0 * embedDim + 0] = -R; 26309371c9d4SSatish Balay coordsIn[0 * embedDim + 1] = R; 26319371c9d4SSatish Balay coordsIn[0 * embedDim + 2] = -R; 26329371c9d4SSatish Balay coordsIn[1 * embedDim + 0] = R; 26339371c9d4SSatish Balay coordsIn[1 * embedDim + 1] = R; 26349371c9d4SSatish Balay coordsIn[1 * embedDim + 2] = -R; 26359371c9d4SSatish Balay coordsIn[2 * embedDim + 0] = R; 26369371c9d4SSatish Balay coordsIn[2 * embedDim + 1] = -R; 26379371c9d4SSatish Balay coordsIn[2 * embedDim + 2] = -R; 26389371c9d4SSatish Balay coordsIn[3 * embedDim + 0] = -R; 26399371c9d4SSatish Balay coordsIn[3 * embedDim + 1] = -R; 26409371c9d4SSatish Balay coordsIn[3 * embedDim + 2] = -R; 26419371c9d4SSatish Balay coordsIn[4 * embedDim + 0] = -R; 26429371c9d4SSatish Balay coordsIn[4 * embedDim + 1] = R; 26439371c9d4SSatish Balay coordsIn[4 * embedDim + 2] = R; 26449371c9d4SSatish Balay coordsIn[5 * embedDim + 0] = R; 26459371c9d4SSatish Balay coordsIn[5 * embedDim + 1] = R; 26469371c9d4SSatish Balay coordsIn[5 * embedDim + 2] = R; 26479371c9d4SSatish Balay coordsIn[6 * embedDim + 0] = -R; 26489371c9d4SSatish Balay coordsIn[6 * embedDim + 1] = -R; 26499371c9d4SSatish Balay coordsIn[6 * embedDim + 2] = R; 26509371c9d4SSatish Balay coordsIn[7 * embedDim + 0] = R; 26519371c9d4SSatish Balay coordsIn[7 * embedDim + 1] = -R; 26529371c9d4SSatish Balay coordsIn[7 * embedDim + 2] = R; 265365a81367SMatthew G. Knepley } 265445da822fSValeria Barra } 265565a81367SMatthew G. Knepley break; 265665a81367SMatthew G. Knepley case 3: 2657116ded15SMatthew G. Knepley if (simplex) { 2658116ded15SMatthew G. Knepley const PetscReal edgeLen = 1.0 / PETSC_PHI; 265951a74b61SMatthew G. Knepley PetscReal vertexA[4] = {0.5, 0.5, 0.5, 0.5}; 266051a74b61SMatthew G. Knepley PetscReal vertexB[4] = {1.0, 0.0, 0.0, 0.0}; 266151a74b61SMatthew G. Knepley PetscReal vertexC[4] = {0.5, 0.5 * PETSC_PHI, 0.5 / PETSC_PHI, 0.0}; 2662116ded15SMatthew G. Knepley const PetscInt degree = 12; 2663116ded15SMatthew G. Knepley PetscInt s[4] = {1, 1, 1}; 26649371c9d4SSatish Balay PetscInt evenPerm[12][4] = { 26659371c9d4SSatish Balay {0, 1, 2, 3}, 26669371c9d4SSatish Balay {0, 2, 3, 1}, 26679371c9d4SSatish Balay {0, 3, 1, 2}, 26689371c9d4SSatish Balay {1, 0, 3, 2}, 26699371c9d4SSatish Balay {1, 2, 0, 3}, 26709371c9d4SSatish Balay {1, 3, 2, 0}, 26719371c9d4SSatish Balay {2, 0, 1, 3}, 26729371c9d4SSatish Balay {2, 1, 3, 0}, 26739371c9d4SSatish Balay {2, 3, 0, 1}, 26749371c9d4SSatish Balay {3, 0, 2, 1}, 26759371c9d4SSatish Balay {3, 1, 0, 2}, 26769371c9d4SSatish Balay {3, 2, 1, 0} 26779371c9d4SSatish Balay }; 2678116ded15SMatthew G. Knepley PetscInt cone[4]; 2679116ded15SMatthew G. Knepley PetscInt *graph, p, i, j, k, l; 2680116ded15SMatthew G. Knepley 26819371c9d4SSatish Balay vertexA[0] *= R; 26829371c9d4SSatish Balay vertexA[1] *= R; 26839371c9d4SSatish Balay vertexA[2] *= R; 26849371c9d4SSatish Balay vertexA[3] *= R; 26859371c9d4SSatish Balay vertexB[0] *= R; 26869371c9d4SSatish Balay vertexB[1] *= R; 26879371c9d4SSatish Balay vertexB[2] *= R; 26889371c9d4SSatish Balay vertexB[3] *= R; 26899371c9d4SSatish Balay vertexC[0] *= R; 26909371c9d4SSatish Balay vertexC[1] *= R; 26919371c9d4SSatish Balay vertexC[2] *= R; 26929371c9d4SSatish Balay vertexC[3] *= R; 2693dd400576SPatrick Sanan numCells = rank == 0 ? 600 : 0; 2694dd400576SPatrick Sanan numVerts = rank == 0 ? 120 : 0; 2695116ded15SMatthew G. Knepley firstVertex = numCells; 2696116ded15SMatthew G. Knepley /* Use the 600-cell, which for a unit sphere has coordinates which are 2697116ded15SMatthew G. Knepley 2698116ded15SMatthew G. Knepley 1/2 (\pm 1, \pm 1, \pm 1, \pm 1) 16 2699116ded15SMatthew G. Knepley (\pm 1, 0, 0, 0) all cyclic permutations 8 2700116ded15SMatthew G. Knepley 1/2 (\pm 1, \pm phi, \pm 1/phi, 0) all even permutations 96 2701116ded15SMatthew G. Knepley 2702116ded15SMatthew G. Knepley where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge 27036333ae4fSvaleriabarra length is then given by 1/\phi = 0.61803. 2704116ded15SMatthew G. Knepley 2705116ded15SMatthew G. Knepley http://buzzard.pugetsound.edu/sage-practice/ch03s03.html 2706116ded15SMatthew G. Knepley http://mathworld.wolfram.com/600-Cell.html 2707116ded15SMatthew G. Knepley */ 2708116ded15SMatthew G. Knepley /* Construct vertices */ 27099566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn)); 2710116ded15SMatthew G. Knepley i = 0; 2711dd400576SPatrick Sanan if (rank == 0) { 2712116ded15SMatthew G. Knepley for (s[0] = -1; s[0] < 2; s[0] += 2) { 2713116ded15SMatthew G. Knepley for (s[1] = -1; s[1] < 2; s[1] += 2) { 2714116ded15SMatthew G. Knepley for (s[2] = -1; s[2] < 2; s[2] += 2) { 2715116ded15SMatthew G. Knepley for (s[3] = -1; s[3] < 2; s[3] += 2) { 2716116ded15SMatthew G. Knepley for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[d] * vertexA[d]; 2717116ded15SMatthew G. Knepley ++i; 2718116ded15SMatthew G. Knepley } 2719116ded15SMatthew G. Knepley } 2720116ded15SMatthew G. Knepley } 2721116ded15SMatthew G. Knepley } 2722116ded15SMatthew G. Knepley for (p = 0; p < embedDim; ++p) { 2723116ded15SMatthew G. Knepley s[1] = s[2] = s[3] = 1; 2724116ded15SMatthew G. Knepley for (s[0] = -1; s[0] < 2; s[0] += 2) { 2725116ded15SMatthew G. Knepley for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertexB[(d + p) % embedDim]; 2726116ded15SMatthew G. Knepley ++i; 2727116ded15SMatthew G. Knepley } 2728116ded15SMatthew G. Knepley } 2729116ded15SMatthew G. Knepley for (p = 0; p < 12; ++p) { 2730116ded15SMatthew G. Knepley s[3] = 1; 2731116ded15SMatthew G. Knepley for (s[0] = -1; s[0] < 2; s[0] += 2) { 2732116ded15SMatthew G. Knepley for (s[1] = -1; s[1] < 2; s[1] += 2) { 2733116ded15SMatthew G. Knepley for (s[2] = -1; s[2] < 2; s[2] += 2) { 2734116ded15SMatthew G. Knepley for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[evenPerm[p][d]] * vertexC[evenPerm[p][d]]; 2735116ded15SMatthew G. Knepley ++i; 2736116ded15SMatthew G. Knepley } 2737116ded15SMatthew G. Knepley } 2738116ded15SMatthew G. Knepley } 2739116ded15SMatthew G. Knepley } 274045da822fSValeria Barra } 274163a3b9bcSJacob Faibussowitsch PetscCheck(i == numVerts, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts); 2742116ded15SMatthew G. Knepley /* Construct graph */ 27439566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(numVerts * numVerts, &graph)); 2744116ded15SMatthew G. Knepley for (i = 0; i < numVerts; ++i) { 2745116ded15SMatthew G. Knepley for (j = 0, k = 0; j < numVerts; ++j) { 27469371c9d4SSatish Balay if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) { 27479371c9d4SSatish Balay graph[i * numVerts + j] = 1; 27489371c9d4SSatish Balay ++k; 27499371c9d4SSatish Balay } 2750116ded15SMatthew G. Knepley } 275163a3b9bcSJacob Faibussowitsch PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree); 2752116ded15SMatthew G. Knepley } 2753116ded15SMatthew G. Knepley /* Build Topology */ 27549566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts)); 275507c565c5SJose E. Roman for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim)); 27569566063dSJacob Faibussowitsch PetscCall(DMSetUp(dm)); /* Allocate space for cones */ 2757116ded15SMatthew G. Knepley /* Cells */ 2758dd400576SPatrick Sanan if (rank == 0) { 275907c565c5SJose E. Roman for (PetscInt i = 0, c = 0; i < numVerts; ++i) { 2760116ded15SMatthew G. Knepley for (j = 0; j < i; ++j) { 2761116ded15SMatthew G. Knepley for (k = 0; k < j; ++k) { 2762116ded15SMatthew G. Knepley for (l = 0; l < k; ++l) { 27639371c9d4SSatish Balay if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i] && graph[l * numVerts + i] && graph[l * numVerts + j] && graph[l * numVerts + k]) { 27649371c9d4SSatish Balay cone[0] = firstVertex + i; 27659371c9d4SSatish Balay cone[1] = firstVertex + j; 27669371c9d4SSatish Balay cone[2] = firstVertex + k; 27679371c9d4SSatish Balay cone[3] = firstVertex + l; 2768116ded15SMatthew G. Knepley /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */ 2769116ded15SMatthew G. Knepley { 27709371c9d4SSatish Balay const PetscInt epsilon[4][4][4][4] = { 27719371c9d4SSatish Balay {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, -1, 0}}, {{0, 0, 0, 0}, {0, 0, 0, -1}, {0, 0, 0, 0}, {0, 1, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 1, 0}, {0, -1, 0, 0}, {0, 0, 0, 0}}}, 2772116ded15SMatthew G. Knepley 27739371c9d4SSatish Balay {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, -1}, {0, 0, 1, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, {{0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {-1, 0, 0, 0}}, {{0, 0, -1, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}}}, 2774116ded15SMatthew G. Knepley 27759371c9d4SSatish Balay {{{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 0}, {0, -1, 0, 0}}, {{0, 0, 0, -1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, {{0, 1, 0, 0}, {-1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}}, 2776116ded15SMatthew G. Knepley 27779371c9d4SSatish Balay {{{0, 0, 0, 0}, {0, 0, -1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}, {{0, 0, 1, 0}, {0, 0, 0, 0}, {-1, 0, 0, 0}, {0, 0, 0, 0}}, {{0, -1, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}} } 27789371c9d4SSatish Balay }; 2779116ded15SMatthew G. Knepley PetscReal normal[4]; 2780116ded15SMatthew G. Knepley PetscInt e, f, g; 2781116ded15SMatthew G. Knepley 2782116ded15SMatthew G. Knepley for (d = 0; d < embedDim; ++d) { 2783116ded15SMatthew G. Knepley normal[d] = 0.0; 2784116ded15SMatthew G. Knepley for (e = 0; e < embedDim; ++e) { 2785116ded15SMatthew G. Knepley for (f = 0; f < embedDim; ++f) { 2786116ded15SMatthew G. Knepley for (g = 0; g < embedDim; ++g) { 2787116ded15SMatthew 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]); 2788116ded15SMatthew G. Knepley } 2789116ded15SMatthew G. Knepley } 2790116ded15SMatthew G. Knepley } 2791116ded15SMatthew G. Knepley } 27929371c9d4SSatish Balay if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) { 27939371c9d4SSatish Balay PetscInt tmp = cone[1]; 27949371c9d4SSatish Balay cone[1] = cone[2]; 27959371c9d4SSatish Balay cone[2] = tmp; 27969371c9d4SSatish Balay } 2797116ded15SMatthew G. Knepley } 27989566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, c++, cone)); 2799116ded15SMatthew G. Knepley } 2800116ded15SMatthew G. Knepley } 2801116ded15SMatthew G. Knepley } 2802116ded15SMatthew G. Knepley } 2803116ded15SMatthew G. Knepley } 280445da822fSValeria Barra } 28059566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dm)); 28069566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dm)); 28079566063dSJacob Faibussowitsch PetscCall(PetscFree(graph)); 2808116ded15SMatthew G. Knepley } 2809f4d061e9SPierre Jolivet break; 2810d71ae5a4SJacob Faibussowitsch default: 2811d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim); 281265a81367SMatthew G. Knepley } 281365a81367SMatthew G. Knepley /* Create coordinates */ 28149566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 28159566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 28169566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim)); 28179566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVerts)); 28182829fed8SMatthew G. Knepley for (v = firstVertex; v < firstVertex + numVerts; ++v) { 28199566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, embedDim)); 28209566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim)); 28212829fed8SMatthew G. Knepley } 28229566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 28239566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 28249566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates)); 28259566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(coordinates, embedDim)); 28269566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 28279566063dSJacob Faibussowitsch PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 28289566063dSJacob Faibussowitsch PetscCall(VecSetType(coordinates, VECSTANDARD)); 28299566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 28309371c9d4SSatish Balay for (v = 0; v < numVerts; ++v) 2831ad540459SPierre Jolivet for (d = 0; d < embedDim; ++d) coords[v * embedDim + d] = coordsIn[v * embedDim + d]; 28329566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 28339566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, coordinates)); 28349566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 28359566063dSJacob Faibussowitsch PetscCall(PetscFree(coordsIn)); 283651a74b61SMatthew G. Knepley { 283751a74b61SMatthew G. Knepley DM cdm; 283851a74b61SMatthew G. Knepley PetscDS cds; 28399318fe57SMatthew G. Knepley PetscScalar c = R; 284051a74b61SMatthew G. Knepley 2841e44f6aebSMatthew G. Knepley PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, snapToSphere)); 28429566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 28439566063dSJacob Faibussowitsch PetscCall(DMGetDS(cdm, &cds)); 28449566063dSJacob Faibussowitsch PetscCall(PetscDSSetConstants(cds, 1, &c)); 284551a74b61SMatthew G. Knepley } 284646139095SJed Brown PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0)); 28479318fe57SMatthew G. Knepley /* Wait for coordinate creation before doing in-place modification */ 28489566063dSJacob Faibussowitsch if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm)); 28493ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 28509318fe57SMatthew G. Knepley } 28519318fe57SMatthew G. Knepley 2852b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal *, PetscReal[], PetscReal (*)[3]); 2853b7f5c055SJed Brown 2854b7f5c055SJed Brown /* 2855b7f5c055SJed Brown The Schwarz P implicit surface is 2856b7f5c055SJed Brown 2857b7f5c055SJed Brown f(x) = cos(x0) + cos(x1) + cos(x2) = 0 2858b7f5c055SJed Brown */ 2859d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3]) 2860d71ae5a4SJacob Faibussowitsch { 2861b7f5c055SJed Brown PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)}; 2862b7f5c055SJed Brown PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)}; 2863b7f5c055SJed Brown f[0] = c[0] + c[1] + c[2]; 2864b7f5c055SJed Brown for (PetscInt i = 0; i < 3; i++) { 2865b7f5c055SJed Brown grad[i] = PETSC_PI * g[i]; 2866ad540459SPierre Jolivet for (PetscInt j = 0; j < 3; j++) hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.; 2867b7f5c055SJed Brown } 2868b7f5c055SJed Brown } 2869b7f5c055SJed Brown 28704663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction 2871d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx) 2872d71ae5a4SJacob Faibussowitsch { 2873ad540459SPierre Jolivet for (PetscInt i = 0; i < 3; i++) u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI); 28743ba16761SJacob Faibussowitsch return PETSC_SUCCESS; 28754663dae6SJed Brown } 28764663dae6SJed Brown 2877b7f5c055SJed Brown /* 2878b7f5c055SJed Brown The Gyroid implicit surface is 2879b7f5c055SJed Brown 2880b7f5c055SJed 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) 2881b7f5c055SJed Brown 2882b7f5c055SJed Brown */ 2883d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3]) 2884d71ae5a4SJacob Faibussowitsch { 2885b7f5c055SJed Brown PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))}; 2886b7f5c055SJed Brown PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))}; 2887b7f5c055SJed Brown f[0] = s[0] * c[1] + s[1] * c[2] + s[2] * c[0]; 2888b7f5c055SJed Brown grad[0] = PETSC_PI * (c[0] * c[1] - s[2] * s[0]); 2889b7f5c055SJed Brown grad[1] = PETSC_PI * (c[1] * c[2] - s[0] * s[1]); 2890b7f5c055SJed Brown grad[2] = PETSC_PI * (c[2] * c[0] - s[1] * s[2]); 2891b7f5c055SJed Brown hess[0][0] = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]); 2892b7f5c055SJed Brown hess[0][1] = -PetscSqr(PETSC_PI) * (c[0] * s[1]); 2893b7f5c055SJed Brown hess[0][2] = -PetscSqr(PETSC_PI) * (c[2] * s[0]); 2894b7f5c055SJed Brown hess[1][0] = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]); 2895b7f5c055SJed Brown hess[1][1] = -PetscSqr(PETSC_PI) * (c[1] * s[2]); 2896b7f5c055SJed Brown hess[2][2] = -PetscSqr(PETSC_PI) * (c[0] * s[1]); 2897b7f5c055SJed Brown hess[2][0] = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]); 2898b7f5c055SJed Brown hess[2][1] = -PetscSqr(PETSC_PI) * (c[2] * s[0]); 2899b7f5c055SJed Brown hess[2][2] = -PetscSqr(PETSC_PI) * (c[1] * s[2]); 2900b7f5c055SJed Brown } 2901b7f5c055SJed Brown 29024663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction 2903d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx) 2904d71ae5a4SJacob Faibussowitsch { 29054663dae6SJed Brown PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))}; 29064663dae6SJed Brown PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))}; 29074663dae6SJed Brown u[0] = PETSC_PI * (c[0] * c[1] - s[2] * s[0]); 29084663dae6SJed Brown u[1] = PETSC_PI * (c[1] * c[2] - s[0] * s[1]); 29094663dae6SJed Brown u[2] = PETSC_PI * (c[2] * c[0] - s[1] * s[2]); 29103ba16761SJacob Faibussowitsch return PETSC_SUCCESS; 29114663dae6SJed Brown } 29124663dae6SJed Brown 2913b7f5c055SJed Brown /* 2914b7f5c055SJed Brown We wish to solve 2915b7f5c055SJed Brown 2916b7f5c055SJed Brown min_y || y - x ||^2 subject to f(y) = 0 2917b7f5c055SJed Brown 2918b7f5c055SJed Brown Let g(y) = grad(f). The minimization problem is equivalent to asking to satisfy 2919b7f5c055SJed 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 2920b7f5c055SJed Brown tangent space and ask for both components in the tangent space to be zero. 2921b7f5c055SJed Brown 2922b7f5c055SJed Brown Take g to be a column vector and compute the "full QR" factorization Q R = g, 2923b7f5c055SJed Brown where Q = I - 2 n n^T is a symmetric orthogonal matrix. 2924b7f5c055SJed Brown The first column of Q is parallel to g so the remaining two columns span the null space. 2925b7f5c055SJed Brown Let Qn = Q[:,1:] be those remaining columns. Then Qn Qn^T is an orthogonal projector into the tangent space. 2926da81f932SPierre Jolivet Since Q is symmetric, this is equivalent to multiplying by Q and taking the last two entries. 2927b7f5c055SJed Brown In total, we have a system of 3 equations in 3 unknowns: 2928b7f5c055SJed Brown 2929b7f5c055SJed Brown f(y) = 0 1 equation 2930b7f5c055SJed Brown Qn^T (y - x) = 0 2 equations 2931b7f5c055SJed Brown 2932b7f5c055SJed Brown Here, we compute the residual and Jacobian of this system. 2933b7f5c055SJed Brown */ 2934d71ae5a4SJacob Faibussowitsch static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[]) 2935d71ae5a4SJacob Faibussowitsch { 2936b7f5c055SJed Brown PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])}; 2937b7f5c055SJed Brown PetscReal d[3] = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])}; 29382f0490c0SSatish Balay PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign; 29399371c9d4SSatish Balay PetscReal n_y[3][3] = { 29409371c9d4SSatish Balay {0, 0, 0}, 29419371c9d4SSatish Balay {0, 0, 0}, 29429371c9d4SSatish Balay {0, 0, 0} 29439371c9d4SSatish Balay }; 2944b7f5c055SJed Brown 2945b7f5c055SJed Brown feval(yreal, &f, grad, n_y); 2946b7f5c055SJed Brown 2947b7f5c055SJed Brown for (PetscInt i = 0; i < 3; i++) n[i] = grad[i]; 2948b7f5c055SJed Brown norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2])); 2949ad540459SPierre Jolivet for (PetscInt i = 0; i < 3; i++) norm_y[i] = 1. / norm * n[i] * n_y[i][i]; 2950b7f5c055SJed Brown 2951b7f5c055SJed Brown // Define the Householder reflector 2952b7f5c055SJed Brown sign = n[0] >= 0 ? 1. : -1.; 2953b7f5c055SJed Brown n[0] += norm * sign; 2954b7f5c055SJed Brown for (PetscInt i = 0; i < 3; i++) n_y[0][i] += norm_y[i] * sign; 2955b7f5c055SJed Brown 2956b7f5c055SJed Brown norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2])); 2957b7f5c055SJed Brown norm_y[0] = 1. / norm * (n[0] * n_y[0][0]); 2958b7f5c055SJed Brown norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]); 2959b7f5c055SJed Brown norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]); 2960b7f5c055SJed Brown 2961b7f5c055SJed Brown for (PetscInt i = 0; i < 3; i++) { 2962b7f5c055SJed Brown n[i] /= norm; 2963b7f5c055SJed Brown for (PetscInt j = 0; j < 3; j++) { 2964b7f5c055SJed Brown // note that n[i] is n_old[i]/norm when executing the code below 2965b7f5c055SJed Brown n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j]; 2966b7f5c055SJed Brown } 2967b7f5c055SJed Brown } 2968b7f5c055SJed Brown 2969b7f5c055SJed Brown nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2]; 2970b7f5c055SJed 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]; 2971b7f5c055SJed Brown 2972b7f5c055SJed Brown res[0] = f; 2973b7f5c055SJed Brown res[1] = d[1] - 2 * n[1] * nd; 2974b7f5c055SJed Brown res[2] = d[2] - 2 * n[2] * nd; 2975b7f5c055SJed Brown // J[j][i] is J_{ij} (column major) 2976b7f5c055SJed Brown for (PetscInt j = 0; j < 3; j++) { 2977b7f5c055SJed Brown J[0 + j * 3] = grad[j]; 2978b7f5c055SJed Brown J[1 + j * 3] = (j == 1) * 1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]); 2979b7f5c055SJed Brown J[2 + j * 3] = (j == 2) * 1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]); 2980b7f5c055SJed Brown } 2981b7f5c055SJed Brown } 2982b7f5c055SJed Brown 2983b7f5c055SJed Brown /* 2984b7f5c055SJed Brown Project x to the nearest point on the implicit surface using Newton's method. 2985b7f5c055SJed Brown */ 2986d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[]) 2987d71ae5a4SJacob Faibussowitsch { 2988b7f5c055SJed Brown PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess 2989b7f5c055SJed Brown 2990b7f5c055SJed Brown PetscFunctionBegin; 2991b7f5c055SJed Brown for (PetscInt iter = 0; iter < 10; iter++) { 2992b7f5c055SJed Brown PetscScalar res[3], J[9]; 2993b7f5c055SJed Brown PetscReal resnorm; 2994b7f5c055SJed Brown TPSNearestPointResJac(feval, x, y, res, J); 2995b7f5c055SJed Brown resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2]))); 2996b7f5c055SJed Brown if (0) { // Turn on this monitor if you need to confirm quadratic convergence 299763a3b9bcSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%" PetscInt_FMT "] res [%g %g %g]\n", iter, (double)PetscRealPart(res[0]), (double)PetscRealPart(res[1]), (double)PetscRealPart(res[2]))); 2998b7f5c055SJed Brown } 2999b7f5c055SJed Brown if (resnorm < PETSC_SMALL) break; 3000b7f5c055SJed Brown 3001b7f5c055SJed Brown // Take the Newton step 30029566063dSJacob Faibussowitsch PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL)); 3003b7f5c055SJed Brown PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res); 3004b7f5c055SJed Brown } 3005b7f5c055SJed Brown for (PetscInt i = 0; i < 3; i++) x[i] = y[i]; 30063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3007b7f5c055SJed Brown } 3008b7f5c055SJed Brown 3009b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL}; 3010b7f5c055SJed Brown 3011d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness) 3012d71ae5a4SJacob Faibussowitsch { 3013b7f5c055SJed Brown PetscMPIInt rank; 3014b7f5c055SJed Brown PetscInt topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0; 3015b7f5c055SJed Brown PetscInt(*edges)[2] = NULL, *edgeSets = NULL; 3016b7f5c055SJed Brown PetscInt *cells_flat = NULL; 3017b7f5c055SJed Brown PetscReal *vtxCoords = NULL; 3018b7f5c055SJed Brown TPSEvaluateFunc evalFunc = NULL; 30198434afd1SBarry Smith PetscSimplePointFn *normalFunc = NULL; 3020b7f5c055SJed Brown DMLabel label; 3021b7f5c055SJed Brown 3022b7f5c055SJed Brown PetscFunctionBegin; 302346139095SJed Brown PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0)); 30249566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank)); 302563a3b9bcSJacob Faibussowitsch PetscCheck((layers != 0) ^ (thickness == 0.), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_INCOMP, "Layers %" PetscInt_FMT " must be nonzero iff thickness %g is nonzero", layers, (double)thickness); 3026b7f5c055SJed Brown switch (tpstype) { 3027b7f5c055SJed Brown case DMPLEX_TPS_SCHWARZ_P: 3028b7f5c055SJed 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"); 3029c5853193SPierre Jolivet if (rank == 0) { 3030b7f5c055SJed Brown PetscInt(*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn] 3031b7f5c055SJed Brown PetscInt Njunctions = 0, Ncuts = 0, Npipes[3], vcount; 3032b7f5c055SJed Brown PetscReal L = 1; 3033b7f5c055SJed Brown 3034b7f5c055SJed Brown Npipes[0] = (extent[0] + 1) * extent[1] * extent[2]; 3035b7f5c055SJed Brown Npipes[1] = extent[0] * (extent[1] + 1) * extent[2]; 3036b7f5c055SJed Brown Npipes[2] = extent[0] * extent[1] * (extent[2] + 1); 3037b7f5c055SJed Brown Njunctions = extent[0] * extent[1] * extent[2]; 3038b7f5c055SJed Brown Ncuts = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]); 3039b7f5c055SJed Brown numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions; 30409566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(3 * numVertices, &vtxCoords)); 30419566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Njunctions, &cells)); 30429566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Ncuts * 4, &edges)); 30439566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(Ncuts * 4, &edgeSets)); 3044b7f5c055SJed Brown // x-normal pipes 3045b7f5c055SJed Brown vcount = 0; 3046b7f5c055SJed Brown for (PetscInt i = 0; i < extent[0] + 1; i++) { 3047b7f5c055SJed Brown for (PetscInt j = 0; j < extent[1]; j++) { 3048b7f5c055SJed Brown for (PetscInt k = 0; k < extent[2]; k++) { 3049b7f5c055SJed Brown for (PetscInt l = 0; l < 4; l++) { 3050b7f5c055SJed Brown vtxCoords[vcount++] = (2 * i - 1) * L; 3051b7f5c055SJed Brown vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2; 3052b7f5c055SJed Brown vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2; 3053b7f5c055SJed Brown } 3054b7f5c055SJed Brown } 3055b7f5c055SJed Brown } 3056b7f5c055SJed Brown } 3057b7f5c055SJed Brown // y-normal pipes 3058b7f5c055SJed Brown for (PetscInt i = 0; i < extent[0]; i++) { 3059b7f5c055SJed Brown for (PetscInt j = 0; j < extent[1] + 1; j++) { 3060b7f5c055SJed Brown for (PetscInt k = 0; k < extent[2]; k++) { 3061b7f5c055SJed Brown for (PetscInt l = 0; l < 4; l++) { 3062b7f5c055SJed Brown vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2; 3063b7f5c055SJed Brown vtxCoords[vcount++] = (2 * j - 1) * L; 3064b7f5c055SJed Brown vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2; 3065b7f5c055SJed Brown } 3066b7f5c055SJed Brown } 3067b7f5c055SJed Brown } 3068b7f5c055SJed Brown } 3069b7f5c055SJed Brown // z-normal pipes 3070b7f5c055SJed Brown for (PetscInt i = 0; i < extent[0]; i++) { 3071b7f5c055SJed Brown for (PetscInt j = 0; j < extent[1]; j++) { 3072b7f5c055SJed Brown for (PetscInt k = 0; k < extent[2] + 1; k++) { 3073b7f5c055SJed Brown for (PetscInt l = 0; l < 4; l++) { 3074b7f5c055SJed Brown vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2; 3075b7f5c055SJed Brown vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2; 3076b7f5c055SJed Brown vtxCoords[vcount++] = (2 * k - 1) * L; 3077b7f5c055SJed Brown } 3078b7f5c055SJed Brown } 3079b7f5c055SJed Brown } 3080b7f5c055SJed Brown } 3081b7f5c055SJed Brown // junctions 3082b7f5c055SJed Brown for (PetscInt i = 0; i < extent[0]; i++) { 3083b7f5c055SJed Brown for (PetscInt j = 0; j < extent[1]; j++) { 3084b7f5c055SJed Brown for (PetscInt k = 0; k < extent[2]; k++) { 3085b7f5c055SJed Brown const PetscInt J = (i * extent[1] + j) * extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2]) * 4 + J * 8; 3086b7f5c055SJed Brown PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count"); 3087b7f5c055SJed Brown for (PetscInt ii = 0; ii < 2; ii++) { 3088b7f5c055SJed Brown for (PetscInt jj = 0; jj < 2; jj++) { 3089b7f5c055SJed Brown for (PetscInt kk = 0; kk < 2; kk++) { 3090b7f5c055SJed Brown double Ls = (1 - sqrt(2) / 4) * L; 3091b7f5c055SJed Brown vtxCoords[vcount++] = 2 * i * L + (2 * ii - 1) * Ls; 3092b7f5c055SJed Brown vtxCoords[vcount++] = 2 * j * L + (2 * jj - 1) * Ls; 3093b7f5c055SJed Brown vtxCoords[vcount++] = 2 * k * L + (2 * kk - 1) * Ls; 3094b7f5c055SJed Brown } 3095b7f5c055SJed Brown } 3096b7f5c055SJed Brown } 3097b7f5c055SJed Brown const PetscInt jfaces[3][2][4] = { 3098b7f5c055SJed Brown {{3, 1, 0, 2}, {7, 5, 4, 6}}, // x-aligned 3099b7f5c055SJed Brown {{5, 4, 0, 1}, {7, 6, 2, 3}}, // y-aligned 3100b7f5c055SJed Brown {{6, 2, 0, 4}, {7, 3, 1, 5}} // z-aligned 3101b7f5c055SJed Brown }; 3102b7f5c055SJed Brown const PetscInt pipe_lo[3] = {// vertex numbers of pipes 31039371c9d4SSatish Balay ((i * extent[1] + j) * extent[2] + k) * 4, ((i * (extent[1] + 1) + j) * extent[2] + k + Npipes[0]) * 4, ((i * extent[1] + j) * (extent[2] + 1) + k + Npipes[0] + Npipes[1]) * 4}; 3104b7f5c055SJed Brown const PetscInt pipe_hi[3] = {// vertex numbers of pipes 31059371c9d4SSatish Balay (((i + 1) * extent[1] + j) * extent[2] + k) * 4, ((i * (extent[1] + 1) + j + 1) * extent[2] + k + Npipes[0]) * 4, ((i * extent[1] + j) * (extent[2] + 1) + k + 1 + Npipes[0] + Npipes[1]) * 4}; 3106b7f5c055SJed Brown for (PetscInt dir = 0; dir < 3; dir++) { // x,y,z 3107b7f5c055SJed Brown const PetscInt ijk[3] = {i, j, k}; 3108b7f5c055SJed Brown for (PetscInt l = 0; l < 4; l++) { // rotations 3109b7f5c055SJed Brown cells[J][dir * 2 + 0][l][0] = pipe_lo[dir] + l; 3110b7f5c055SJed Brown cells[J][dir * 2 + 0][l][1] = Jvoff + jfaces[dir][0][l]; 3111b7f5c055SJed Brown cells[J][dir * 2 + 0][l][2] = Jvoff + jfaces[dir][0][(l - 1 + 4) % 4]; 3112b7f5c055SJed Brown cells[J][dir * 2 + 0][l][3] = pipe_lo[dir] + (l - 1 + 4) % 4; 3113b7f5c055SJed Brown cells[J][dir * 2 + 1][l][0] = Jvoff + jfaces[dir][1][l]; 3114b7f5c055SJed Brown cells[J][dir * 2 + 1][l][1] = pipe_hi[dir] + l; 3115b7f5c055SJed Brown cells[J][dir * 2 + 1][l][2] = pipe_hi[dir] + (l - 1 + 4) % 4; 3116b7f5c055SJed Brown cells[J][dir * 2 + 1][l][3] = Jvoff + jfaces[dir][1][(l - 1 + 4) % 4]; 3117b7f5c055SJed Brown if (ijk[dir] == 0) { 3118b7f5c055SJed Brown edges[numEdges][0] = pipe_lo[dir] + l; 3119b7f5c055SJed Brown edges[numEdges][1] = pipe_lo[dir] + (l + 1) % 4; 3120b7f5c055SJed Brown edgeSets[numEdges] = dir * 2 + 1; 3121b7f5c055SJed Brown numEdges++; 3122b7f5c055SJed Brown } 3123b7f5c055SJed Brown if (ijk[dir] + 1 == extent[dir]) { 3124b7f5c055SJed Brown edges[numEdges][0] = pipe_hi[dir] + l; 3125b7f5c055SJed Brown edges[numEdges][1] = pipe_hi[dir] + (l + 1) % 4; 3126b7f5c055SJed Brown edgeSets[numEdges] = dir * 2 + 2; 3127b7f5c055SJed Brown numEdges++; 3128b7f5c055SJed Brown } 3129b7f5c055SJed Brown } 3130b7f5c055SJed Brown } 3131b7f5c055SJed Brown } 3132b7f5c055SJed Brown } 3133b7f5c055SJed Brown } 313463a3b9bcSJacob Faibussowitsch PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts); 3135b7f5c055SJed Brown numFaces = 24 * Njunctions; 3136b7f5c055SJed Brown cells_flat = cells[0][0][0]; 3137b7f5c055SJed Brown } 3138b7f5c055SJed Brown evalFunc = TPSEvaluate_SchwarzP; 31394663dae6SJed Brown normalFunc = TPSExtrudeNormalFunc_SchwarzP; 3140b7f5c055SJed Brown break; 3141b7f5c055SJed Brown case DMPLEX_TPS_GYROID: 3142c5853193SPierre Jolivet if (rank == 0) { 3143b7f5c055SJed Brown // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set 3144b7f5c055SJed Brown // 3145b7f5c055SJed 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) 3146b7f5c055SJed Brown // 3147b7f5c055SJed Brown // on the cell [0,2]^3. 3148b7f5c055SJed Brown // 3149b7f5c055SJed Brown // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2]. 3150b7f5c055SJed Brown // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins 3151b7f5c055SJed Brown // like a boomerang: 3152b7f5c055SJed Brown // 3153b7f5c055SJed Brown // z = 0 z = 1/4 z = 1/2 z = 3/4 // 3154b7f5c055SJed Brown // ----- ------- ------- ------- // 3155b7f5c055SJed Brown // // 3156b7f5c055SJed Brown // + + + + + + + \ + // 3157b7f5c055SJed Brown // \ / \ // 3158b7f5c055SJed Brown // \ `-_ _-' / } // 3159b7f5c055SJed Brown // *-_ `-' _-' / // 3160b7f5c055SJed Brown // + `-+ + + +-' + + / + // 3161b7f5c055SJed Brown // // 3162b7f5c055SJed Brown // // 3163b7f5c055SJed Brown // z = 1 z = 5/4 z = 3/2 z = 7/4 // 3164b7f5c055SJed Brown // ----- ------- ------- ------- // 3165b7f5c055SJed Brown // // 3166b7f5c055SJed Brown // +-_ + + + + _-+ + / + // 3167b7f5c055SJed Brown // `-_ _-_ _-` / // 3168b7f5c055SJed Brown // \ _-' `-_ / { // 3169b7f5c055SJed Brown // \ / \ // 3170b7f5c055SJed Brown // + + + + + + + \ + // 3171b7f5c055SJed Brown // 3172b7f5c055SJed Brown // 3173b7f5c055SJed Brown // This course mesh approximates each of these slices by two line segments, 3174b7f5c055SJed Brown // and then connects the segments in consecutive layers with quadrilateral faces. 3175b7f5c055SJed Brown // All of the end points of the segments are multiples of 1/4 except for the 3176b7f5c055SJed Brown // point * in the picture for z = 0 above and the similar points in other layers. 3177b7f5c055SJed Brown // That point is at (gamma, gamma, 0), where gamma is calculated below. 3178b7f5c055SJed Brown // 3179b7f5c055SJed Brown // The column [1,2]x[1,2]x[0,2] looks the same as this column; 3180b7f5c055SJed Brown // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images. 3181b7f5c055SJed Brown // 3182b7f5c055SJed Brown // As for how this method turned into the names given to the vertices: 3183b7f5c055SJed Brown // that was not systematic, it was just the way it worked out in my handwritten notes. 3184b7f5c055SJed Brown 3185b7f5c055SJed Brown PetscInt facesPerBlock = 64; 3186b7f5c055SJed Brown PetscInt vertsPerBlock = 56; 3187b7f5c055SJed Brown PetscInt extentPlus[3]; 3188b7f5c055SJed Brown PetscInt numBlocks, numBlocksPlus; 31899371c9d4SSatish Balay const PetscInt A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7, II = 8, J = 9, K = 10, L = 11, M = 12, N = 13, O = 14, P = 15, Q = 16, R = 17, S = 18, T = 19, U = 20, V = 21, W = 22, X = 23, Y = 24, Z = 25, Ap = 26, Bp = 27, Cp = 28, Dp = 29, Ep = 30, Fp = 31, Gp = 32, Hp = 33, Ip = 34, Jp = 35, Kp = 36, Lp = 37, Mp = 38, Np = 39, Op = 40, Pp = 41, Qp = 42, Rp = 43, Sp = 44, Tp = 45, Up = 46, Vp = 47, Wp = 48, Xp = 49, Yp = 50, Zp = 51, Aq = 52, Bq = 53, Cq = 54, Dq = 55; 31909371c9d4SSatish Balay const PetscInt pattern[64][4] = { 31919371c9d4SSatish Balay /* face to vertex within the coarse discretization of a single gyroid block */ 3192b7f5c055SJed Brown /* layer 0 */ 31939371c9d4SSatish Balay {A, C, K, G }, 31949371c9d4SSatish Balay {C, B, II, K }, 31959371c9d4SSatish Balay {D, A, H, L }, 31969371c9d4SSatish Balay {B + 56 * 1, D, L, J }, 31979371c9d4SSatish Balay {E, B + 56 * 1, J, N }, 31989371c9d4SSatish Balay {A + 56 * 2, E, N, H + 56 * 2 }, 31999371c9d4SSatish Balay {F, A + 56 * 2, G + 56 * 2, M }, 32009371c9d4SSatish Balay {B, F, M, II }, 3201b7f5c055SJed Brown /* layer 1 */ 32029371c9d4SSatish Balay {G, K, Q, O }, 32039371c9d4SSatish Balay {K, II, P, Q }, 32049371c9d4SSatish Balay {L, H, O + 56 * 1, R }, 32059371c9d4SSatish Balay {J, L, R, P }, 32069371c9d4SSatish Balay {N, J, P, S }, 32079371c9d4SSatish Balay {H + 56 * 2, N, S, O + 56 * 3 }, 32089371c9d4SSatish Balay {M, G + 56 * 2, O + 56 * 2, T }, 32099371c9d4SSatish Balay {II, M, T, P }, 3210b7f5c055SJed Brown /* layer 2 */ 32119371c9d4SSatish Balay {O, Q, Y, U }, 32129371c9d4SSatish Balay {Q, P, W, Y }, 32139371c9d4SSatish Balay {R, O + 56 * 1, U + 56 * 1, Ap }, 32149371c9d4SSatish Balay {P, R, Ap, W }, 32159371c9d4SSatish Balay {S, P, X, Bp }, 32169371c9d4SSatish Balay {O + 56 * 3, S, Bp, V + 56 * 1 }, 32179371c9d4SSatish Balay {T, O + 56 * 2, V, Z }, 32189371c9d4SSatish Balay {P, T, Z, X }, 3219b7f5c055SJed Brown /* layer 3 */ 32209371c9d4SSatish Balay {U, Y, Ep, Dp }, 32219371c9d4SSatish Balay {Y, W, Cp, Ep }, 32229371c9d4SSatish Balay {Ap, U + 56 * 1, Dp + 56 * 1, Gp }, 32239371c9d4SSatish Balay {W, Ap, Gp, Cp }, 32249371c9d4SSatish Balay {Bp, X, Cp + 56 * 2, Fp }, 32259371c9d4SSatish Balay {V + 56 * 1, Bp, Fp, Dp + 56 * 1}, 32269371c9d4SSatish Balay {Z, V, Dp, Hp }, 32279371c9d4SSatish Balay {X, Z, Hp, Cp + 56 * 2}, 3228b7f5c055SJed Brown /* layer 4 */ 32299371c9d4SSatish Balay {Dp, Ep, Mp, Kp }, 32309371c9d4SSatish Balay {Ep, Cp, Ip, Mp }, 32319371c9d4SSatish Balay {Gp, Dp + 56 * 1, Lp, Np }, 32329371c9d4SSatish Balay {Cp, Gp, Np, Jp }, 32339371c9d4SSatish Balay {Fp, Cp + 56 * 2, Jp + 56 * 2, Pp }, 32349371c9d4SSatish Balay {Dp + 56 * 1, Fp, Pp, Lp }, 32359371c9d4SSatish Balay {Hp, Dp, Kp, Op }, 32369371c9d4SSatish Balay {Cp + 56 * 2, Hp, Op, Ip + 56 * 2}, 3237b7f5c055SJed Brown /* layer 5 */ 32389371c9d4SSatish Balay {Kp, Mp, Sp, Rp }, 32399371c9d4SSatish Balay {Mp, Ip, Qp, Sp }, 32409371c9d4SSatish Balay {Np, Lp, Rp, Tp }, 32419371c9d4SSatish Balay {Jp, Np, Tp, Qp + 56 * 1}, 32429371c9d4SSatish Balay {Pp, Jp + 56 * 2, Qp + 56 * 3, Up }, 32439371c9d4SSatish Balay {Lp, Pp, Up, Rp }, 32449371c9d4SSatish Balay {Op, Kp, Rp, Vp }, 32459371c9d4SSatish Balay {Ip + 56 * 2, Op, Vp, Qp + 56 * 2}, 3246b7f5c055SJed Brown /* layer 6 */ 32479371c9d4SSatish Balay {Rp, Sp, Aq, Yp }, 32489371c9d4SSatish Balay {Sp, Qp, Wp, Aq }, 32499371c9d4SSatish Balay {Tp, Rp, Yp, Cq }, 32509371c9d4SSatish Balay {Qp + 56 * 1, Tp, Cq, Wp + 56 * 1}, 32519371c9d4SSatish Balay {Up, Qp + 56 * 3, Xp + 56 * 1, Dq }, 32529371c9d4SSatish Balay {Rp, Up, Dq, Zp }, 32539371c9d4SSatish Balay {Vp, Rp, Zp, Bq }, 32549371c9d4SSatish Balay {Qp + 56 * 2, Vp, Bq, Xp }, 3255b7f5c055SJed Brown /* layer 7 (the top is the periodic image of the bottom of layer 0) */ 32569371c9d4SSatish Balay {Yp, Aq, C + 56 * 4, A + 56 * 4 }, 32579371c9d4SSatish Balay {Aq, Wp, B + 56 * 4, C + 56 * 4 }, 32589371c9d4SSatish Balay {Cq, Yp, A + 56 * 4, D + 56 * 4 }, 32599371c9d4SSatish Balay {Wp + 56 * 1, Cq, D + 56 * 4, B + 56 * 5 }, 32609371c9d4SSatish Balay {Dq, Xp + 56 * 1, B + 56 * 5, E + 56 * 4 }, 32619371c9d4SSatish Balay {Zp, Dq, E + 56 * 4, A + 56 * 6 }, 32629371c9d4SSatish Balay {Bq, Zp, A + 56 * 6, F + 56 * 4 }, 32639371c9d4SSatish Balay {Xp, Bq, F + 56 * 4, B + 56 * 4 } 3264b7f5c055SJed Brown }; 3265b7f5c055SJed Brown const PetscReal gamma = PetscAcosReal((PetscSqrtReal(3.) - 1.) / PetscSqrtReal(2.)) / PETSC_PI; 32669371c9d4SSatish Balay const PetscReal patternCoords[56][3] = { 3267bee3fc89SBarry Smith {1., 0., 0. }, /* A */ 3268bee3fc89SBarry Smith {0., 1., 0. }, /* B */ 3269bee3fc89SBarry Smith {gamma, gamma, 0. }, /* C */ 3270bee3fc89SBarry Smith {1 + gamma, 1 - gamma, 0. }, /* D */ 3271bee3fc89SBarry Smith {2 - gamma, 2 - gamma, 0. }, /* E */ 3272bee3fc89SBarry Smith {1 - gamma, 1 + gamma, 0. }, /* F */ 3273b7f5c055SJed Brown 3274bee3fc89SBarry Smith {.5, 0, .25 }, /* G */ 3275bee3fc89SBarry Smith {1.5, 0., .25 }, /* H */ 3276bee3fc89SBarry Smith {.5, 1., .25 }, /* II */ 3277bee3fc89SBarry Smith {1.5, 1., .25 }, /* J */ 3278bee3fc89SBarry Smith {.25, .5, .25 }, /* K */ 3279bee3fc89SBarry Smith {1.25, .5, .25 }, /* L */ 3280bee3fc89SBarry Smith {.75, 1.5, .25 }, /* M */ 3281bee3fc89SBarry Smith {1.75, 1.5, .25 }, /* N */ 3282b7f5c055SJed Brown 3283bee3fc89SBarry Smith {0., 0., .5 }, /* O */ 3284bee3fc89SBarry Smith {1., 1., .5 }, /* P */ 3285bee3fc89SBarry Smith {gamma, 1 - gamma, .5 }, /* Q */ 3286bee3fc89SBarry Smith {1 + gamma, gamma, .5 }, /* R */ 3287bee3fc89SBarry Smith {2 - gamma, 1 + gamma, .5 }, /* S */ 3288bee3fc89SBarry Smith {1 - gamma, 2 - gamma, .5 }, /* T */ 3289b7f5c055SJed Brown 3290bee3fc89SBarry Smith {0., .5, .75 }, /* U */ 3291bee3fc89SBarry Smith {0., 1.5, .75 }, /* V */ 3292bee3fc89SBarry Smith {1., .5, .75 }, /* W */ 3293bee3fc89SBarry Smith {1., 1.5, .75 }, /* X */ 3294bee3fc89SBarry Smith {.5, .75, .75 }, /* Y */ 3295bee3fc89SBarry Smith {.5, 1.75, .75 }, /* Z */ 3296bee3fc89SBarry Smith {1.5, .25, .75 }, /* Ap */ 3297bee3fc89SBarry Smith {1.5, 1.25, .75 }, /* Bp */ 3298b7f5c055SJed Brown 3299bee3fc89SBarry Smith {1., 0., 1. }, /* Cp */ 3300bee3fc89SBarry Smith {0., 1., 1. }, /* Dp */ 3301bee3fc89SBarry Smith {1 - gamma, 1 - gamma, 1. }, /* Ep */ 3302bee3fc89SBarry Smith {1 + gamma, 1 + gamma, 1. }, /* Fp */ 3303bee3fc89SBarry Smith {2 - gamma, gamma, 1. }, /* Gp */ 3304bee3fc89SBarry Smith {gamma, 2 - gamma, 1. }, /* Hp */ 3305b7f5c055SJed Brown 3306bee3fc89SBarry Smith {.5, 0., 1.25}, /* Ip */ 3307bee3fc89SBarry Smith {1.5, 0., 1.25}, /* Jp */ 3308bee3fc89SBarry Smith {.5, 1., 1.25}, /* Kp */ 3309bee3fc89SBarry Smith {1.5, 1., 1.25}, /* Lp */ 3310bee3fc89SBarry Smith {.75, .5, 1.25}, /* Mp */ 3311bee3fc89SBarry Smith {1.75, .5, 1.25}, /* Np */ 3312bee3fc89SBarry Smith {.25, 1.5, 1.25}, /* Op */ 3313bee3fc89SBarry Smith {1.25, 1.5, 1.25}, /* Pp */ 3314b7f5c055SJed Brown 3315bee3fc89SBarry Smith {0., 0., 1.5 }, /* Qp */ 3316bee3fc89SBarry Smith {1., 1., 1.5 }, /* Rp */ 3317bee3fc89SBarry Smith {1 - gamma, gamma, 1.5 }, /* Sp */ 3318bee3fc89SBarry Smith {2 - gamma, 1 - gamma, 1.5 }, /* Tp */ 3319bee3fc89SBarry Smith {1 + gamma, 2 - gamma, 1.5 }, /* Up */ 3320bee3fc89SBarry Smith {gamma, 1 + gamma, 1.5 }, /* Vp */ 3321b7f5c055SJed Brown 3322bee3fc89SBarry Smith {0., .5, 1.75}, /* Wp */ 3323bee3fc89SBarry Smith {0., 1.5, 1.75}, /* Xp */ 3324bee3fc89SBarry Smith {1., .5, 1.75}, /* Yp */ 3325bee3fc89SBarry Smith {1., 1.5, 1.75}, /* Zp */ 3326bee3fc89SBarry Smith {.5, .25, 1.75}, /* Aq */ 3327bee3fc89SBarry Smith {.5, 1.25, 1.75}, /* Bq */ 3328bee3fc89SBarry Smith {1.5, .75, 1.75}, /* Cq */ 3329bee3fc89SBarry Smith {1.5, 1.75, 1.75}, /* Dq */ 3330b7f5c055SJed Brown }; 3331b7f5c055SJed Brown PetscInt(*cells)[64][4] = NULL; 3332b7f5c055SJed Brown PetscBool *seen; 3333b7f5c055SJed Brown PetscInt *vertToTrueVert; 3334b7f5c055SJed Brown PetscInt count; 3335b7f5c055SJed Brown 3336b7f5c055SJed Brown for (PetscInt i = 0; i < 3; i++) extentPlus[i] = extent[i] + 1; 3337b7f5c055SJed Brown numBlocks = 1; 3338b7f5c055SJed Brown for (PetscInt i = 0; i < 3; i++) numBlocks *= extent[i]; 3339b7f5c055SJed Brown numBlocksPlus = 1; 3340b7f5c055SJed Brown for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i]; 3341b7f5c055SJed Brown numFaces = numBlocks * facesPerBlock; 33429566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numBlocks, &cells)); 33439566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock, &seen)); 3344b7f5c055SJed Brown for (PetscInt k = 0; k < extent[2]; k++) { 3345b7f5c055SJed Brown for (PetscInt j = 0; j < extent[1]; j++) { 3346b7f5c055SJed Brown for (PetscInt i = 0; i < extent[0]; i++) { 3347b7f5c055SJed Brown for (PetscInt f = 0; f < facesPerBlock; f++) { 3348b7f5c055SJed Brown for (PetscInt v = 0; v < 4; v++) { 3349b7f5c055SJed Brown PetscInt vertRaw = pattern[f][v]; 3350b7f5c055SJed Brown PetscInt blockidx = vertRaw / 56; 3351b7f5c055SJed Brown PetscInt patternvert = vertRaw % 56; 3352b7f5c055SJed Brown PetscInt xplus = (blockidx & 1); 3353b7f5c055SJed Brown PetscInt yplus = (blockidx & 2) >> 1; 3354b7f5c055SJed Brown PetscInt zplus = (blockidx & 4) >> 2; 3355b7f5c055SJed Brown PetscInt zcoord = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus); 3356b7f5c055SJed Brown PetscInt ycoord = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus); 3357b7f5c055SJed Brown PetscInt xcoord = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus); 3358b7f5c055SJed Brown PetscInt vert = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert; 3359b7f5c055SJed Brown 3360b7f5c055SJed Brown cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert; 3361b7f5c055SJed Brown seen[vert] = PETSC_TRUE; 3362b7f5c055SJed Brown } 3363b7f5c055SJed Brown } 3364b7f5c055SJed Brown } 3365b7f5c055SJed Brown } 3366b7f5c055SJed Brown } 33679371c9d4SSatish Balay for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) 33689371c9d4SSatish Balay if (seen[i]) numVertices++; 3369b7f5c055SJed Brown count = 0; 33709566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert)); 33719566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords)); 3372b7f5c055SJed Brown for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1; 3373b7f5c055SJed Brown for (PetscInt k = 0; k < extentPlus[2]; k++) { 3374b7f5c055SJed Brown for (PetscInt j = 0; j < extentPlus[1]; j++) { 3375b7f5c055SJed Brown for (PetscInt i = 0; i < extentPlus[0]; i++) { 3376b7f5c055SJed Brown for (PetscInt v = 0; v < vertsPerBlock; v++) { 3377b7f5c055SJed Brown PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v; 3378b7f5c055SJed Brown 3379b7f5c055SJed Brown if (seen[vIdx]) { 3380b7f5c055SJed Brown PetscInt thisVert; 3381b7f5c055SJed Brown 3382b7f5c055SJed Brown vertToTrueVert[vIdx] = thisVert = count++; 3383b7f5c055SJed Brown 3384b7f5c055SJed Brown for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d]; 3385b7f5c055SJed Brown vtxCoords[3 * thisVert + 0] += i * 2; 3386b7f5c055SJed Brown vtxCoords[3 * thisVert + 1] += j * 2; 3387b7f5c055SJed Brown vtxCoords[3 * thisVert + 2] += k * 2; 3388b7f5c055SJed Brown } 3389b7f5c055SJed Brown } 3390b7f5c055SJed Brown } 3391b7f5c055SJed Brown } 3392b7f5c055SJed Brown } 3393b7f5c055SJed Brown for (PetscInt i = 0; i < numBlocks; i++) { 3394b7f5c055SJed Brown for (PetscInt f = 0; f < facesPerBlock; f++) { 3395ad540459SPierre Jolivet for (PetscInt v = 0; v < 4; v++) cells[i][f][v] = vertToTrueVert[cells[i][f][v]]; 3396b7f5c055SJed Brown } 3397b7f5c055SJed Brown } 33989566063dSJacob Faibussowitsch PetscCall(PetscFree(vertToTrueVert)); 33999566063dSJacob Faibussowitsch PetscCall(PetscFree(seen)); 3400b7f5c055SJed Brown cells_flat = cells[0][0]; 3401b7f5c055SJed Brown numEdges = 0; 3402b7f5c055SJed Brown for (PetscInt i = 0; i < numFaces; i++) { 3403b7f5c055SJed Brown for (PetscInt e = 0; e < 4; e++) { 3404b7f5c055SJed Brown PetscInt ev[] = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]}; 3405b7f5c055SJed Brown const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]}; 3406b7f5c055SJed Brown 3407b7f5c055SJed Brown for (PetscInt d = 0; d < 3; d++) { 3408b7f5c055SJed Brown if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) { 3409b7f5c055SJed Brown if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++; 3410b7f5c055SJed Brown if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) numEdges++; 3411b7f5c055SJed Brown } 3412b7f5c055SJed Brown } 3413b7f5c055SJed Brown } 3414b7f5c055SJed Brown } 34159566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numEdges, &edges)); 34169566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numEdges, &edgeSets)); 3417b7f5c055SJed Brown for (PetscInt edge = 0, i = 0; i < numFaces; i++) { 3418b7f5c055SJed Brown for (PetscInt e = 0; e < 4; e++) { 3419b7f5c055SJed Brown PetscInt ev[] = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]}; 3420b7f5c055SJed Brown const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]}; 3421b7f5c055SJed Brown 3422b7f5c055SJed Brown for (PetscInt d = 0; d < 3; d++) { 3423b7f5c055SJed Brown if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) { 3424b7f5c055SJed Brown if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) { 3425b7f5c055SJed Brown edges[edge][0] = ev[0]; 3426b7f5c055SJed Brown edges[edge][1] = ev[1]; 3427b7f5c055SJed Brown edgeSets[edge++] = 2 * d; 3428b7f5c055SJed Brown } 3429b7f5c055SJed Brown if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) { 3430b7f5c055SJed Brown edges[edge][0] = ev[0]; 3431b7f5c055SJed Brown edges[edge][1] = ev[1]; 3432b7f5c055SJed Brown edgeSets[edge++] = 2 * d + 1; 3433b7f5c055SJed Brown } 3434b7f5c055SJed Brown } 3435b7f5c055SJed Brown } 3436b7f5c055SJed Brown } 3437b7f5c055SJed Brown } 3438b7f5c055SJed Brown } 3439b7f5c055SJed Brown evalFunc = TPSEvaluate_Gyroid; 34404663dae6SJed Brown normalFunc = TPSExtrudeNormalFunc_Gyroid; 3441b7f5c055SJed Brown break; 3442b7f5c055SJed Brown } 3443b7f5c055SJed Brown 34449566063dSJacob Faibussowitsch PetscCall(DMSetDimension(dm, topoDim)); 3445c5853193SPierre Jolivet if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat)); 34469566063dSJacob Faibussowitsch else PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL)); 34479566063dSJacob Faibussowitsch PetscCall(PetscFree(cells_flat)); 3448b7f5c055SJed Brown { 3449b7f5c055SJed Brown DM idm; 34509566063dSJacob Faibussowitsch PetscCall(DMPlexInterpolate(dm, &idm)); 345169d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &idm)); 3452b7f5c055SJed Brown } 3453c5853193SPierre Jolivet if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords)); 34549566063dSJacob Faibussowitsch else PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL)); 34559566063dSJacob Faibussowitsch PetscCall(PetscFree(vtxCoords)); 3456b7f5c055SJed Brown 34579566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dm, "Face Sets")); 34589566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "Face Sets", &label)); 3459b7f5c055SJed Brown for (PetscInt e = 0; e < numEdges; e++) { 3460b7f5c055SJed Brown PetscInt njoin; 3461b7f5c055SJed Brown const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]}; 34629566063dSJacob Faibussowitsch PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join)); 346363a3b9bcSJacob Faibussowitsch PetscCheck(njoin == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expected unique join of vertices %" PetscInt_FMT " and %" PetscInt_FMT, edges[e][0], edges[e][1]); 34649566063dSJacob Faibussowitsch PetscCall(DMLabelSetValue(label, join[0], edgeSets[e])); 34659566063dSJacob Faibussowitsch PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join)); 3466b7f5c055SJed Brown } 34679566063dSJacob Faibussowitsch PetscCall(PetscFree(edges)); 34689566063dSJacob Faibussowitsch PetscCall(PetscFree(edgeSets)); 34691436d7faSJed Brown if (tps_distribute) { 34701436d7faSJed Brown DM pdm = NULL; 34711436d7faSJed Brown PetscPartitioner part; 34721436d7faSJed Brown 34739566063dSJacob Faibussowitsch PetscCall(DMPlexGetPartitioner(dm, &part)); 34749566063dSJacob Faibussowitsch PetscCall(PetscPartitionerSetFromOptions(part)); 34759566063dSJacob Faibussowitsch PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm)); 347648a46eb9SPierre Jolivet if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm)); 34771436d7faSJed Brown // Do not auto-distribute again 34789566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE)); 34791436d7faSJed Brown } 3480b7f5c055SJed Brown 34819566063dSJacob Faibussowitsch PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE)); 3482b7f5c055SJed Brown for (PetscInt refine = 0; refine < refinements; refine++) { 3483b7f5c055SJed Brown PetscInt m; 3484b7f5c055SJed Brown DM dmf; 3485b7f5c055SJed Brown Vec X; 3486b7f5c055SJed Brown PetscScalar *x; 34879566063dSJacob Faibussowitsch PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf)); 348869d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &dmf)); 3489b7f5c055SJed Brown 34909566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &X)); 34919566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(X, &m)); 34929566063dSJacob Faibussowitsch PetscCall(VecGetArray(X, &x)); 349348a46eb9SPierre Jolivet for (PetscInt i = 0; i < m; i += 3) PetscCall(TPSNearestPoint(evalFunc, &x[i])); 34949566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(X, &x)); 3495b7f5c055SJed Brown } 3496b7f5c055SJed Brown 3497b7f5c055SJed Brown // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices. 34989566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "Face Sets", &label)); 34999566063dSJacob Faibussowitsch PetscCall(DMPlexLabelComplete(dm, label)); 3500b7f5c055SJed Brown 350146139095SJed Brown PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0)); 350246139095SJed Brown 3503b7f5c055SJed Brown if (thickness > 0) { 35044663dae6SJed Brown DM edm, cdm, ecdm; 35054663dae6SJed Brown DMPlexTransform tr; 35064663dae6SJed Brown const char *prefix; 35074663dae6SJed Brown PetscOptions options; 35084663dae6SJed Brown // Code from DMPlexExtrude 35094663dae6SJed Brown PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr)); 35104663dae6SJed Brown PetscCall(DMPlexTransformSetDM(tr, dm)); 35114663dae6SJed Brown PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDE)); 35124663dae6SJed Brown PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix)); 35134663dae6SJed Brown PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tr, prefix)); 35144663dae6SJed Brown PetscCall(PetscObjectGetOptions((PetscObject)dm, &options)); 35154663dae6SJed Brown PetscCall(PetscObjectSetOptions((PetscObject)tr, options)); 35164663dae6SJed Brown PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers)); 35174663dae6SJed Brown PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness)); 35184663dae6SJed Brown PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE)); 35194663dae6SJed Brown PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE)); 35204663dae6SJed Brown PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc)); 35214663dae6SJed Brown PetscCall(DMPlexTransformSetFromOptions(tr)); 35224663dae6SJed Brown PetscCall(PetscObjectSetOptions((PetscObject)tr, NULL)); 35234663dae6SJed Brown PetscCall(DMPlexTransformSetUp(tr)); 35244663dae6SJed Brown PetscCall(PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_tps_transform_view")); 35254663dae6SJed Brown PetscCall(DMPlexTransformApply(tr, dm, &edm)); 35264663dae6SJed Brown PetscCall(DMCopyDisc(dm, edm)); 35274663dae6SJed Brown PetscCall(DMGetCoordinateDM(dm, &cdm)); 35284663dae6SJed Brown PetscCall(DMGetCoordinateDM(edm, &ecdm)); 35294663dae6SJed Brown PetscCall(DMCopyDisc(cdm, ecdm)); 35304663dae6SJed Brown PetscCall(DMPlexTransformCreateDiscLabels(tr, edm)); 35314663dae6SJed Brown PetscCall(DMPlexTransformDestroy(&tr)); 35324663dae6SJed Brown if (edm) { 35334663dae6SJed Brown ((DM_Plex *)edm->data)->printFEM = ((DM_Plex *)dm->data)->printFEM; 35344663dae6SJed Brown ((DM_Plex *)edm->data)->printL2 = ((DM_Plex *)dm->data)->printL2; 3535f5867de0SMatthew G. Knepley ((DM_Plex *)edm->data)->printLocate = ((DM_Plex *)dm->data)->printLocate; 35364663dae6SJed Brown } 353769d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &edm)); 3538b7f5c055SJed Brown } 35393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3540b7f5c055SJed Brown } 3541b7f5c055SJed Brown 3542b7f5c055SJed Brown /*@ 3543b7f5c055SJed Brown DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface 3544b7f5c055SJed Brown 3545b7f5c055SJed Brown Collective 3546b7f5c055SJed Brown 3547b7f5c055SJed Brown Input Parameters: 3548a1cb98faSBarry Smith + comm - The communicator for the `DM` object 3549b7f5c055SJed Brown . tpstype - Type of triply-periodic surface 3550b7f5c055SJed Brown . extent - Array of length 3 containing number of periods in each direction 355120f4b53cSBarry Smith . periodic - array of length 3 with periodicity, or `NULL` for non-periodic 35521436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable) 3553817da375SSatish Balay . refinements - Number of factor-of-2 refinements of 2D manifold mesh 35541436d7faSJed Brown . layers - Number of cell layers extruded in normal direction 3555817da375SSatish Balay - thickness - Thickness in normal direction 3556b7f5c055SJed Brown 3557b7f5c055SJed Brown Output Parameter: 3558a1cb98faSBarry Smith . dm - The `DM` object 3559a1cb98faSBarry Smith 3560a1cb98faSBarry Smith Level: beginner 3561b7f5c055SJed Brown 3562b7f5c055SJed Brown Notes: 356315229ffcSPierre Jolivet This meshes the surface of the Schwarz P or Gyroid surfaces. Schwarz P is the simplest member of the triply-periodic minimal surfaces. 35641d27aa22SBarry Smith <https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22)> and can be cut with "clean" boundaries. 35651d27aa22SBarry Smith The Gyroid <https://en.wikipedia.org/wiki/Gyroid> is another triply-periodic minimal surface with applications in additive manufacturing; it is much more difficult to "cut" since there are no planes of symmetry. 3566b7f5c055SJed Brown Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested. 3567b7f5c055SJed Brown On each refinement, all vertices are projected to their nearest point on the surface. 3568b7f5c055SJed Brown This projection could readily be extended to related surfaces. 3569b7f5c055SJed Brown 35701d27aa22SBarry Smith See {cite}`maskery2018insights` 35711d27aa22SBarry Smith 35721d27aa22SBarry Smith The face (edge) sets for the Schwarz P surface are numbered $1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z)$. 35731d27aa22SBarry Smith When the mesh is refined, "Face Sets" contain the new vertices (created during refinement). 35741d27aa22SBarry Smith Use `DMPlexLabelComplete()` to propagate to coarse-level vertices. 3575b7f5c055SJed Brown 357660225df5SJacob Faibussowitsch Developer Notes: 3577b7f5c055SJed Brown The Gyroid mesh does not currently mark boundary sets. 3578b7f5c055SJed Brown 35791cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()` 3580b7f5c055SJed Brown @*/ 3581d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateTPSMesh(MPI_Comm comm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness, DM *dm) 3582d71ae5a4SJacob Faibussowitsch { 3583b7f5c055SJed Brown PetscFunctionBegin; 35849566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 35859566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 35869566063dSJacob Faibussowitsch PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness)); 35873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3588b7f5c055SJed Brown } 3589b7f5c055SJed Brown 35909318fe57SMatthew G. Knepley /*@ 35919318fe57SMatthew G. Knepley DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d. 35929318fe57SMatthew G. Knepley 35939318fe57SMatthew G. Knepley Collective 35949318fe57SMatthew G. Knepley 35959318fe57SMatthew G. Knepley Input Parameters: 3596a1cb98faSBarry Smith + comm - The communicator for the `DM` object 35979318fe57SMatthew G. Knepley . dim - The dimension 35989318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells 35999318fe57SMatthew G. Knepley - R - The radius 36009318fe57SMatthew G. Knepley 36019318fe57SMatthew G. Knepley Output Parameter: 3602a1cb98faSBarry Smith . dm - The `DM` object 36039318fe57SMatthew G. Knepley 36049318fe57SMatthew G. Knepley Level: beginner 36059318fe57SMatthew G. Knepley 36061cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()` 36079318fe57SMatthew G. Knepley @*/ 3608d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm) 3609d71ae5a4SJacob Faibussowitsch { 36109318fe57SMatthew G. Knepley PetscFunctionBegin; 36114f572ea9SToby Isaac PetscAssertPointer(dm, 5); 36129566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 36139566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 36149566063dSJacob Faibussowitsch PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R)); 36153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 36169318fe57SMatthew G. Knepley } 36179318fe57SMatthew G. Knepley 3618d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R) 3619d71ae5a4SJacob Faibussowitsch { 36209318fe57SMatthew G. Knepley DM sdm, vol; 36219318fe57SMatthew G. Knepley DMLabel bdlabel; 3622dd2b43ebSStefano Zampini const char *prefix; 36239318fe57SMatthew G. Knepley 36249318fe57SMatthew G. Knepley PetscFunctionBegin; 36259566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm)); 36269566063dSJacob Faibussowitsch PetscCall(DMSetType(sdm, DMPLEX)); 3627dd2b43ebSStefano Zampini PetscCall(DMGetOptionsPrefix(dm, &prefix)); 3628dd2b43ebSStefano Zampini PetscCall(DMSetOptionsPrefix(sdm, prefix)); 3629dd2b43ebSStefano Zampini PetscCall(DMAppendOptionsPrefix(sdm, "bd_")); 3630dd2b43ebSStefano Zampini PetscCall(DMPlexDistributeSetDefault(sdm, PETSC_FALSE)); 36319566063dSJacob Faibussowitsch PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim - 1, PETSC_TRUE, R)); 36329566063dSJacob Faibussowitsch PetscCall(DMSetFromOptions(sdm)); 36339566063dSJacob Faibussowitsch PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view")); 36349566063dSJacob Faibussowitsch PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol)); 36359566063dSJacob Faibussowitsch PetscCall(DMDestroy(&sdm)); 363669d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &vol)); 36379566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dm, "marker")); 36389566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, "marker", &bdlabel)); 36399566063dSJacob Faibussowitsch PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel)); 36409566063dSJacob Faibussowitsch PetscCall(DMPlexLabelComplete(dm, bdlabel)); 36413ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 364251a74b61SMatthew G. Knepley } 364351a74b61SMatthew G. Knepley 364451a74b61SMatthew G. Knepley /*@ 364551a74b61SMatthew G. Knepley DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d. 364651a74b61SMatthew G. Knepley 364751a74b61SMatthew G. Knepley Collective 364851a74b61SMatthew G. Knepley 364951a74b61SMatthew G. Knepley Input Parameters: 3650a1cb98faSBarry Smith + comm - The communicator for the `DM` object 365151a74b61SMatthew G. Knepley . dim - The dimension 365251a74b61SMatthew G. Knepley - R - The radius 365351a74b61SMatthew G. Knepley 365451a74b61SMatthew G. Knepley Output Parameter: 3655a1cb98faSBarry Smith . dm - The `DM` object 365651a74b61SMatthew G. Knepley 3657a1cb98faSBarry Smith Options Database Key: 365860225df5SJacob Faibussowitsch . bd_dm_refine - This will refine the surface mesh preserving the sphere geometry 365951a74b61SMatthew G. Knepley 366051a74b61SMatthew G. Knepley Level: beginner 366151a74b61SMatthew G. Knepley 36621cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()` 366351a74b61SMatthew G. Knepley @*/ 3664d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm) 3665d71ae5a4SJacob Faibussowitsch { 366651a74b61SMatthew G. Knepley PetscFunctionBegin; 36679566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 36689566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 36699566063dSJacob Faibussowitsch PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R)); 36703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 36712829fed8SMatthew G. Knepley } 36722829fed8SMatthew G. Knepley 3673d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct) 3674d71ae5a4SJacob Faibussowitsch { 36750a6ba040SMatthew G. Knepley PetscFunctionBegin; 36769318fe57SMatthew G. Knepley switch (ct) { 36779371c9d4SSatish Balay case DM_POLYTOPE_POINT: { 36789318fe57SMatthew G. Knepley PetscInt numPoints[1] = {1}; 36799318fe57SMatthew G. Knepley PetscInt coneSize[1] = {0}; 36809318fe57SMatthew G. Knepley PetscInt cones[1] = {0}; 36819318fe57SMatthew G. Knepley PetscInt coneOrientations[1] = {0}; 36829318fe57SMatthew G. Knepley PetscScalar vertexCoords[1] = {0.0}; 36839318fe57SMatthew G. Knepley 36849566063dSJacob Faibussowitsch PetscCall(DMSetDimension(rdm, 0)); 36859566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 36869371c9d4SSatish Balay } break; 36879371c9d4SSatish Balay case DM_POLYTOPE_SEGMENT: { 36889318fe57SMatthew G. Knepley PetscInt numPoints[2] = {2, 1}; 36899318fe57SMatthew G. Knepley PetscInt coneSize[3] = {2, 0, 0}; 36909318fe57SMatthew G. Knepley PetscInt cones[2] = {1, 2}; 36919318fe57SMatthew G. Knepley PetscInt coneOrientations[2] = {0, 0}; 36929318fe57SMatthew G. Knepley PetscScalar vertexCoords[2] = {-1.0, 1.0}; 36939318fe57SMatthew G. Knepley 36949566063dSJacob Faibussowitsch PetscCall(DMSetDimension(rdm, 1)); 36959566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 36969371c9d4SSatish Balay } break; 36979371c9d4SSatish Balay case DM_POLYTOPE_POINT_PRISM_TENSOR: { 3698b5a892a1SMatthew G. Knepley PetscInt numPoints[2] = {2, 1}; 3699b5a892a1SMatthew G. Knepley PetscInt coneSize[3] = {2, 0, 0}; 3700b5a892a1SMatthew G. Knepley PetscInt cones[2] = {1, 2}; 3701b5a892a1SMatthew G. Knepley PetscInt coneOrientations[2] = {0, 0}; 3702b5a892a1SMatthew G. Knepley PetscScalar vertexCoords[2] = {-1.0, 1.0}; 3703b5a892a1SMatthew G. Knepley 37049566063dSJacob Faibussowitsch PetscCall(DMSetDimension(rdm, 1)); 37059566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 37069371c9d4SSatish Balay } break; 37079371c9d4SSatish Balay case DM_POLYTOPE_TRIANGLE: { 37089318fe57SMatthew G. Knepley PetscInt numPoints[2] = {3, 1}; 37099318fe57SMatthew G. Knepley PetscInt coneSize[4] = {3, 0, 0, 0}; 37109318fe57SMatthew G. Knepley PetscInt cones[3] = {1, 2, 3}; 37119318fe57SMatthew G. Knepley PetscInt coneOrientations[3] = {0, 0, 0}; 37129318fe57SMatthew G. Knepley PetscScalar vertexCoords[6] = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0}; 37139318fe57SMatthew G. Knepley 37149566063dSJacob Faibussowitsch PetscCall(DMSetDimension(rdm, 2)); 37159566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 37169371c9d4SSatish Balay } break; 37179371c9d4SSatish Balay case DM_POLYTOPE_QUADRILATERAL: { 37189318fe57SMatthew G. Knepley PetscInt numPoints[2] = {4, 1}; 37199318fe57SMatthew G. Knepley PetscInt coneSize[5] = {4, 0, 0, 0, 0}; 37209318fe57SMatthew G. Knepley PetscInt cones[4] = {1, 2, 3, 4}; 37219318fe57SMatthew G. Knepley PetscInt coneOrientations[4] = {0, 0, 0, 0}; 37229318fe57SMatthew G. Knepley PetscScalar vertexCoords[8] = {-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0}; 37239318fe57SMatthew G. Knepley 37249566063dSJacob Faibussowitsch PetscCall(DMSetDimension(rdm, 2)); 37259566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 37269371c9d4SSatish Balay } break; 37279371c9d4SSatish Balay case DM_POLYTOPE_SEG_PRISM_TENSOR: { 37289318fe57SMatthew G. Knepley PetscInt numPoints[2] = {4, 1}; 37299318fe57SMatthew G. Knepley PetscInt coneSize[5] = {4, 0, 0, 0, 0}; 37309318fe57SMatthew G. Knepley PetscInt cones[4] = {1, 2, 3, 4}; 37319318fe57SMatthew G. Knepley PetscInt coneOrientations[4] = {0, 0, 0, 0}; 37329318fe57SMatthew G. Knepley PetscScalar vertexCoords[8] = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0}; 37339318fe57SMatthew G. Knepley 37349566063dSJacob Faibussowitsch PetscCall(DMSetDimension(rdm, 2)); 37359566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 37369371c9d4SSatish Balay } break; 37379371c9d4SSatish Balay case DM_POLYTOPE_TETRAHEDRON: { 37389318fe57SMatthew G. Knepley PetscInt numPoints[2] = {4, 1}; 37399318fe57SMatthew G. Knepley PetscInt coneSize[5] = {4, 0, 0, 0, 0}; 3740f0edb160SMatthew G. Knepley PetscInt cones[4] = {1, 2, 3, 4}; 37419318fe57SMatthew G. Knepley PetscInt coneOrientations[4] = {0, 0, 0, 0}; 3742f0edb160SMatthew 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}; 37439318fe57SMatthew G. Knepley 37449566063dSJacob Faibussowitsch PetscCall(DMSetDimension(rdm, 3)); 37459566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 37469371c9d4SSatish Balay } break; 37479371c9d4SSatish Balay case DM_POLYTOPE_HEXAHEDRON: { 37489318fe57SMatthew G. Knepley PetscInt numPoints[2] = {8, 1}; 37499318fe57SMatthew G. Knepley PetscInt coneSize[9] = {8, 0, 0, 0, 0, 0, 0, 0, 0}; 3750f0edb160SMatthew G. Knepley PetscInt cones[8] = {1, 2, 3, 4, 5, 6, 7, 8}; 37519318fe57SMatthew G. Knepley PetscInt coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 37529371c9d4SSatish Balay PetscScalar vertexCoords[24] = {-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0}; 37539318fe57SMatthew G. Knepley 37549566063dSJacob Faibussowitsch PetscCall(DMSetDimension(rdm, 3)); 37559566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 37569371c9d4SSatish Balay } break; 37579371c9d4SSatish Balay case DM_POLYTOPE_TRI_PRISM: { 37589318fe57SMatthew G. Knepley PetscInt numPoints[2] = {6, 1}; 37599318fe57SMatthew G. Knepley PetscInt coneSize[7] = {6, 0, 0, 0, 0, 0, 0}; 3760f0edb160SMatthew G. Knepley PetscInt cones[6] = {1, 2, 3, 4, 5, 6}; 37619318fe57SMatthew G. Knepley PetscInt coneOrientations[6] = {0, 0, 0, 0, 0, 0}; 37629371c9d4SSatish Balay PetscScalar vertexCoords[18] = {-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0}; 37639318fe57SMatthew G. Knepley 37649566063dSJacob Faibussowitsch PetscCall(DMSetDimension(rdm, 3)); 37659566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 37669371c9d4SSatish Balay } break; 37679371c9d4SSatish Balay case DM_POLYTOPE_TRI_PRISM_TENSOR: { 37689318fe57SMatthew G. Knepley PetscInt numPoints[2] = {6, 1}; 37699318fe57SMatthew G. Knepley PetscInt coneSize[7] = {6, 0, 0, 0, 0, 0, 0}; 37709318fe57SMatthew G. Knepley PetscInt cones[6] = {1, 2, 3, 4, 5, 6}; 37719318fe57SMatthew G. Knepley PetscInt coneOrientations[6] = {0, 0, 0, 0, 0, 0}; 37729371c9d4SSatish Balay PetscScalar vertexCoords[18] = {-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0}; 37739318fe57SMatthew G. Knepley 37749566063dSJacob Faibussowitsch PetscCall(DMSetDimension(rdm, 3)); 37759566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 37769371c9d4SSatish Balay } break; 37779371c9d4SSatish Balay case DM_POLYTOPE_QUAD_PRISM_TENSOR: { 37789318fe57SMatthew G. Knepley PetscInt numPoints[2] = {8, 1}; 37799318fe57SMatthew G. Knepley PetscInt coneSize[9] = {8, 0, 0, 0, 0, 0, 0, 0, 0}; 37809318fe57SMatthew G. Knepley PetscInt cones[8] = {1, 2, 3, 4, 5, 6, 7, 8}; 37819318fe57SMatthew G. Knepley PetscInt coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 37829371c9d4SSatish Balay PetscScalar vertexCoords[24] = {-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0}; 37839318fe57SMatthew G. Knepley 37849566063dSJacob Faibussowitsch PetscCall(DMSetDimension(rdm, 3)); 37859566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 37869371c9d4SSatish Balay } break; 37879371c9d4SSatish Balay case DM_POLYTOPE_PYRAMID: { 37889318fe57SMatthew G. Knepley PetscInt numPoints[2] = {5, 1}; 37899318fe57SMatthew G. Knepley PetscInt coneSize[6] = {5, 0, 0, 0, 0, 0}; 3790f0edb160SMatthew G. Knepley PetscInt cones[5] = {1, 2, 3, 4, 5}; 37919318fe57SMatthew G. Knepley PetscInt coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0}; 37929371c9d4SSatish Balay PetscScalar vertexCoords[24] = {-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 0.0, 0.0, 1.0}; 37939318fe57SMatthew G. Knepley 37949566063dSJacob Faibussowitsch PetscCall(DMSetDimension(rdm, 3)); 37959566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords)); 37969371c9d4SSatish Balay } break; 3797d71ae5a4SJacob Faibussowitsch default: 3798d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]); 37999318fe57SMatthew G. Knepley } 38009318fe57SMatthew G. Knepley { 38019318fe57SMatthew G. Knepley PetscInt Nv, v; 38029318fe57SMatthew G. Knepley 38039318fe57SMatthew G. Knepley /* Must create the celltype label here so that we do not automatically try to compute the types */ 38049566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(rdm, "celltype")); 38059566063dSJacob Faibussowitsch PetscCall(DMPlexSetCellType(rdm, 0, ct)); 38069566063dSJacob Faibussowitsch PetscCall(DMPlexGetChart(rdm, NULL, &Nv)); 38079566063dSJacob Faibussowitsch for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT)); 38089318fe57SMatthew G. Knepley } 38099566063dSJacob Faibussowitsch PetscCall(DMPlexInterpolateInPlace_Internal(rdm)); 38109566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)rdm, DMPolytopeTypes[ct])); 38113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 38120a6ba040SMatthew G. Knepley } 38130a6ba040SMatthew G. Knepley 38149318fe57SMatthew G. Knepley /*@ 3815a1cb98faSBarry Smith DMPlexCreateReferenceCell - Create a `DMPLEX` with the appropriate FEM reference cell 38169318fe57SMatthew G. Knepley 38179318fe57SMatthew G. Knepley Collective 38189318fe57SMatthew G. Knepley 38199318fe57SMatthew G. Knepley Input Parameters: 38209318fe57SMatthew G. Knepley + comm - The communicator 38219318fe57SMatthew G. Knepley - ct - The cell type of the reference cell 38229318fe57SMatthew G. Knepley 38239318fe57SMatthew G. Knepley Output Parameter: 38249318fe57SMatthew G. Knepley . refdm - The reference cell 38259318fe57SMatthew G. Knepley 38269318fe57SMatthew G. Knepley Level: intermediate 38279318fe57SMatthew G. Knepley 382842747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()` 38299318fe57SMatthew G. Knepley @*/ 3830d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm) 3831d71ae5a4SJacob Faibussowitsch { 38320a6ba040SMatthew G. Knepley PetscFunctionBegin; 38339566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, refdm)); 38349566063dSJacob Faibussowitsch PetscCall(DMSetType(*refdm, DMPLEX)); 38359566063dSJacob Faibussowitsch PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct)); 38363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 38379318fe57SMatthew G. Knepley } 383879a015ccSMatthew G. Knepley 3839d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[]) 3840d71ae5a4SJacob Faibussowitsch { 38419318fe57SMatthew G. Knepley DM plex; 38429318fe57SMatthew G. Knepley DMLabel label; 38439318fe57SMatthew G. Knepley PetscBool hasLabel; 38440a6ba040SMatthew G. Knepley 3845c22d3578SMatthew G. Knepley PetscFunctionBegin; 38469566063dSJacob Faibussowitsch PetscCall(DMHasLabel(dm, name, &hasLabel)); 38473ba16761SJacob Faibussowitsch if (hasLabel) PetscFunctionReturn(PETSC_SUCCESS); 38489566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(dm, name)); 38499566063dSJacob Faibussowitsch PetscCall(DMGetLabel(dm, name, &label)); 38509566063dSJacob Faibussowitsch PetscCall(DMConvert(dm, DMPLEX, &plex)); 38519566063dSJacob Faibussowitsch PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label)); 38521c8afea9SMatthew G. Knepley PetscCall(DMPlexLabelComplete(plex, label)); 38539566063dSJacob Faibussowitsch PetscCall(DMDestroy(&plex)); 38543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 38559318fe57SMatthew G. Knepley } 3856acdc6f61SToby Isaac 3857669647acSMatthew G. Knepley /* 3858669647acSMatthew G. Knepley We use the last coordinate as the radius, the inner radius is lower[dim-1] and the outer radius is upper[dim-1]. Then we map the first coordinate around the circle. 3859669647acSMatthew G. Knepley 3860669647acSMatthew G. Knepley (x, y) -> (r, theta) = (x[1], (x[0] - lower[0]) * 2\pi/(upper[0] - lower[0])) 3861669647acSMatthew G. Knepley */ 3862d71ae5a4SJacob Faibussowitsch static void boxToAnnulus(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[]) 3863d71ae5a4SJacob Faibussowitsch { 3864669647acSMatthew G. Knepley const PetscReal low = PetscRealPart(constants[0]); 3865669647acSMatthew G. Knepley const PetscReal upp = PetscRealPart(constants[1]); 3866669647acSMatthew G. Knepley const PetscReal r = PetscRealPart(u[1]); 3867669647acSMatthew G. Knepley const PetscReal th = 2. * PETSC_PI * (PetscRealPart(u[0]) - low) / (upp - low); 3868669647acSMatthew G. Knepley 3869669647acSMatthew G. Knepley f0[0] = r * PetscCosReal(th); 3870669647acSMatthew G. Knepley f0[1] = r * PetscSinReal(th); 3871669647acSMatthew G. Knepley } 3872669647acSMatthew G. Knepley 38734e22dd4cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions, const char pre[], const char name[], const char *option[], const char *value[], PetscBool *flg); 38744e22dd4cSMatthew G. Knepley 38755dca41c3SJed Brown const char *const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "annulus", "hypercubic", "zbox", "unknown", "DMPlexShape", "DM_SHAPE_", NULL}; 38769318fe57SMatthew G. Knepley 3877d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm) 3878d71ae5a4SJacob Faibussowitsch { 38799318fe57SMatthew G. Knepley DMPlexShape shape = DM_SHAPE_BOX; 38809318fe57SMatthew G. Knepley DMPolytopeType cell = DM_POLYTOPE_TRIANGLE; 38819318fe57SMatthew G. Knepley PetscInt dim = 2; 38829318fe57SMatthew G. Knepley PetscBool simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE; 3883d0812dedSMatthew G. Knepley PetscBool flg, flg2, fflg, strflg, bdfflg, nameflg; 38849318fe57SMatthew G. Knepley MPI_Comm comm; 3885ed5e4e85SVaclav Hapla char filename[PETSC_MAX_PATH_LEN] = "<unspecified>"; 3886ed5e4e85SVaclav Hapla char bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>"; 3887ed5e4e85SVaclav Hapla char plexname[PETSC_MAX_PATH_LEN] = ""; 38884e22dd4cSMatthew G. Knepley const char *option; 38899318fe57SMatthew G. Knepley 38909318fe57SMatthew G. Knepley PetscFunctionBegin; 3891708be2fdSJed Brown PetscCall(PetscLogEventBegin(DMPLEX_CreateFromOptions, dm, 0, 0, 0)); 38929566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 38939318fe57SMatthew G. Knepley /* TODO Turn this into a registration interface */ 38949566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg)); 3895d0812dedSMatthew G. Knepley PetscCall(PetscOptionsString("-dm_plex_file_contents", "Contents of a file format in a string", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &strflg)); 38969566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg)); 38979566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg)); 38989566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum)cell, (PetscEnum *)&cell, NULL)); 38999566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL)); 39009566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum)shape, (PetscEnum *)&shape, &flg)); 39019566063dSJacob Faibussowitsch PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0)); 39029566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex, &simplex, &flg)); 39039566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg)); 39049566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone, &adjCone, &flg)); 39059566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure, &adjClosure, &flg2)); 39069566063dSJacob Faibussowitsch if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure)); 39079318fe57SMatthew G. Knepley 390861a622f3SMatthew G. Knepley switch (cell) { 390961a622f3SMatthew G. Knepley case DM_POLYTOPE_POINT: 391061a622f3SMatthew G. Knepley case DM_POLYTOPE_SEGMENT: 391161a622f3SMatthew G. Knepley case DM_POLYTOPE_POINT_PRISM_TENSOR: 391261a622f3SMatthew G. Knepley case DM_POLYTOPE_TRIANGLE: 391361a622f3SMatthew G. Knepley case DM_POLYTOPE_QUADRILATERAL: 391461a622f3SMatthew G. Knepley case DM_POLYTOPE_TETRAHEDRON: 3915d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_HEXAHEDRON: 3916d71ae5a4SJacob Faibussowitsch *useCoordSpace = PETSC_TRUE; 3917d71ae5a4SJacob Faibussowitsch break; 3918d71ae5a4SJacob Faibussowitsch default: 3919d71ae5a4SJacob Faibussowitsch *useCoordSpace = PETSC_FALSE; 3920d71ae5a4SJacob Faibussowitsch break; 392161a622f3SMatthew G. Knepley } 392261a622f3SMatthew G. Knepley 39239318fe57SMatthew G. Knepley if (fflg) { 39249318fe57SMatthew G. Knepley DM dmnew; 3925*1e4a82c4SMatthew G. Knepley const char *name; 39269318fe57SMatthew G. Knepley 3927*1e4a82c4SMatthew G. Knepley PetscCall(PetscObjectGetName((PetscObject)dm, &name)); 3928*1e4a82c4SMatthew G. Knepley PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), filename, nameflg ? plexname : name, interpolate, &dmnew)); 39295de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew)); 393069d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &dmnew)); 39319318fe57SMatthew G. Knepley } else if (refDomain) { 39329566063dSJacob Faibussowitsch PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell)); 39339318fe57SMatthew G. Knepley } else if (bdfflg) { 39349318fe57SMatthew G. Knepley DM bdm, dmnew; 3935*1e4a82c4SMatthew G. Knepley const char *name; 39369318fe57SMatthew G. Knepley 3937*1e4a82c4SMatthew G. Knepley PetscCall(PetscObjectGetName((PetscObject)dm, &name)); 3938*1e4a82c4SMatthew G. Knepley PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), bdFilename, nameflg ? plexname : name, interpolate, &bdm)); 39399566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)bdm, "bd_")); 39409566063dSJacob Faibussowitsch PetscCall(DMSetFromOptions(bdm)); 39419566063dSJacob Faibussowitsch PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew)); 39429566063dSJacob Faibussowitsch PetscCall(DMDestroy(&bdm)); 39435de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew)); 394469d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &dmnew)); 3945d0812dedSMatthew G. Knepley } else if (strflg) { 3946d0812dedSMatthew G. Knepley DM dmnew; 3947d0812dedSMatthew G. Knepley PetscViewer viewer; 3948d0812dedSMatthew G. Knepley const char *contents; 3949d0812dedSMatthew G. Knepley char *strname; 3950d0812dedSMatthew G. Knepley char tmpdir[PETSC_MAX_PATH_LEN]; 3951d0812dedSMatthew G. Knepley char tmpfilename[PETSC_MAX_PATH_LEN]; 3952d0812dedSMatthew G. Knepley char name[PETSC_MAX_PATH_LEN]; 3953d0812dedSMatthew G. Knepley MPI_Comm comm; 3954d0812dedSMatthew G. Knepley PetscMPIInt rank; 3955d0812dedSMatthew G. Knepley 3956d0812dedSMatthew G. Knepley PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 3957d0812dedSMatthew G. Knepley PetscCallMPI(MPI_Comm_rank(comm, &rank)); 3958d0812dedSMatthew G. Knepley PetscCall(PetscStrchr(filename, ':', &strname)); 3959d0812dedSMatthew G. Knepley PetscCheck(strname, comm, PETSC_ERR_ARG_WRONG, "File contents must have the form \"ext:string_name\", not %s", filename); 3960d0812dedSMatthew G. Knepley strname[0] = '\0'; 3961d0812dedSMatthew G. Knepley ++strname; 3962d0812dedSMatthew G. Knepley PetscCall(PetscDLSym(NULL, strname, (void **)&contents)); 3963d0812dedSMatthew G. Knepley PetscCheck(contents, comm, PETSC_ERR_ARG_WRONG, "Could not locate mesh string %s", strname); 3964d0812dedSMatthew G. Knepley PetscCall(PetscGetTmp(comm, tmpdir, PETSC_MAX_PATH_LEN)); 3965ed32af8cSMatthew G. Knepley PetscCall(PetscStrlcat(tmpdir, "/meshXXXXXX", PETSC_MAX_PATH_LEN)); 3966ed32af8cSMatthew G. Knepley PetscCall(PetscMkdtemp(tmpdir)); 3967ed32af8cSMatthew G. Knepley PetscCall(PetscSNPrintf(tmpfilename, PETSC_MAX_PATH_LEN, "%s/mesh.%s", tmpdir, filename)); 3968d0812dedSMatthew G. Knepley PetscCall(PetscViewerASCIIOpen(comm, tmpfilename, &viewer)); 3969d0812dedSMatthew G. Knepley PetscCall(PetscViewerASCIIPrintf(viewer, "%s\n", contents)); 3970d0812dedSMatthew G. Knepley PetscCall(PetscViewerDestroy(&viewer)); 3971d0812dedSMatthew G. Knepley PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), tmpfilename, plexname, interpolate, &dmnew)); 3972ed32af8cSMatthew G. Knepley PetscCall(PetscRMTree(tmpdir)); 3973d0812dedSMatthew G. Knepley PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "%s Mesh", strname)); 3974d0812dedSMatthew G. Knepley PetscCall(PetscObjectSetName((PetscObject)dm, name)); 3975d0812dedSMatthew G. Knepley PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew)); 3976d0812dedSMatthew G. Knepley PetscCall(DMPlexReplace_Internal(dm, &dmnew)); 39779318fe57SMatthew G. Knepley } else { 39789566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)dm, DMPlexShapes[shape])); 39799318fe57SMatthew G. Knepley switch (shape) { 3980669647acSMatthew G. Knepley case DM_SHAPE_BOX: 39815dca41c3SJed Brown case DM_SHAPE_ZBOX: 3982669647acSMatthew G. Knepley case DM_SHAPE_ANNULUS: { 39839318fe57SMatthew G. Knepley PetscInt faces[3] = {0, 0, 0}; 39849318fe57SMatthew G. Knepley PetscReal lower[3] = {0, 0, 0}; 39859318fe57SMatthew G. Knepley PetscReal upper[3] = {1, 1, 1}; 39869318fe57SMatthew G. Knepley DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE}; 3987669647acSMatthew G. Knepley PetscBool isAnnular = shape == DM_SHAPE_ANNULUS ? PETSC_TRUE : PETSC_FALSE; 39889318fe57SMatthew G. Knepley PetscInt i, n; 39899318fe57SMatthew G. Knepley 39909318fe57SMatthew G. Knepley n = dim; 39919318fe57SMatthew G. Knepley for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4 - dim); 39929566063dSJacob Faibussowitsch PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg)); 39939318fe57SMatthew G. Knepley n = 3; 39949566063dSJacob Faibussowitsch PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg)); 399563a3b9bcSJacob Faibussowitsch PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim); 39969318fe57SMatthew G. Knepley n = 3; 39979566063dSJacob Faibussowitsch PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg)); 399863a3b9bcSJacob Faibussowitsch PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim); 39999318fe57SMatthew G. Knepley n = 3; 40009566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg)); 400163a3b9bcSJacob Faibussowitsch PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim); 4002669647acSMatthew G. Knepley 4003669647acSMatthew G. Knepley PetscCheck(!isAnnular || dim == 2, comm, PETSC_ERR_ARG_OUTOFRANGE, "Only two dimensional annuli have been implemented"); 4004669647acSMatthew G. Knepley if (isAnnular) 4005669647acSMatthew G. Knepley for (i = 0; i < dim - 1; ++i) bdt[i] = DM_BOUNDARY_PERIODIC; 4006669647acSMatthew G. Knepley 40079318fe57SMatthew G. Knepley switch (cell) { 400861a622f3SMatthew G. Knepley case DM_POLYTOPE_TRI_PRISM_TENSOR: 40099566063dSJacob Faibussowitsch PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt)); 4010d410b0cfSMatthew G. Knepley if (!interpolate) { 4011d410b0cfSMatthew G. Knepley DM udm; 4012d410b0cfSMatthew G. Knepley 40139566063dSJacob Faibussowitsch PetscCall(DMPlexUninterpolate(dm, &udm)); 401469d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &udm)); 4015d410b0cfSMatthew G. Knepley } 40169318fe57SMatthew G. Knepley break; 4017d71ae5a4SJacob Faibussowitsch default: 40185dca41c3SJed Brown PetscCall(DMPlexCreateBoxMesh_Internal(dm, shape, dim, simplex, faces, lower, upper, bdt, interpolate)); 4019d71ae5a4SJacob Faibussowitsch break; 40209318fe57SMatthew G. Knepley } 4021669647acSMatthew G. Knepley if (isAnnular) { 4022669647acSMatthew G. Knepley DM cdm; 4023669647acSMatthew G. Knepley PetscDS cds; 4024669647acSMatthew G. Knepley PetscScalar bounds[2] = {lower[0], upper[0]}; 4025669647acSMatthew G. Knepley 4026669647acSMatthew G. Knepley // Fix coordinates for annular region 4027669647acSMatthew G. Knepley PetscCall(DMSetPeriodicity(dm, NULL, NULL, NULL)); 4028669647acSMatthew G. Knepley PetscCall(DMSetCellCoordinatesLocal(dm, NULL)); 4029669647acSMatthew G. Knepley PetscCall(DMSetCellCoordinates(dm, NULL)); 4030e44f6aebSMatthew G. Knepley PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL)); 4031669647acSMatthew G. Knepley PetscCall(DMGetCoordinateDM(dm, &cdm)); 4032669647acSMatthew G. Knepley PetscCall(DMGetDS(cdm, &cds)); 4033669647acSMatthew G. Knepley PetscCall(PetscDSSetConstants(cds, 2, bounds)); 4034669647acSMatthew G. Knepley PetscCall(DMPlexRemapGeometry(dm, 0.0, boxToAnnulus)); 4035669647acSMatthew G. Knepley } 40369371c9d4SSatish Balay } break; 40379371c9d4SSatish Balay case DM_SHAPE_BOX_SURFACE: { 40389318fe57SMatthew G. Knepley PetscInt faces[3] = {0, 0, 0}; 40399318fe57SMatthew G. Knepley PetscReal lower[3] = {0, 0, 0}; 40409318fe57SMatthew G. Knepley PetscReal upper[3] = {1, 1, 1}; 40419318fe57SMatthew G. Knepley PetscInt i, n; 40429318fe57SMatthew G. Knepley 40439318fe57SMatthew G. Knepley n = dim + 1; 40449318fe57SMatthew G. Knepley for (i = 0; i < dim + 1; ++i) faces[i] = (dim + 1 == 1 ? 1 : 4 - (dim + 1)); 40459566063dSJacob Faibussowitsch PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg)); 40469318fe57SMatthew G. Knepley n = 3; 40479566063dSJacob Faibussowitsch PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg)); 404863a3b9bcSJacob Faibussowitsch PetscCheck(!flg || !(n != dim + 1), comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim + 1); 40499318fe57SMatthew G. Knepley n = 3; 40509566063dSJacob Faibussowitsch PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg)); 405163a3b9bcSJacob Faibussowitsch PetscCheck(!flg || !(n != dim + 1), comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim + 1); 40529566063dSJacob Faibussowitsch PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim + 1, faces, lower, upper, interpolate)); 40539371c9d4SSatish Balay } break; 40549371c9d4SSatish Balay case DM_SHAPE_SPHERE: { 40559318fe57SMatthew G. Knepley PetscReal R = 1.0; 40569318fe57SMatthew G. Knepley 40579566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R, &R, &flg)); 40589566063dSJacob Faibussowitsch PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R)); 40599371c9d4SSatish Balay } break; 40609371c9d4SSatish Balay case DM_SHAPE_BALL: { 40619318fe57SMatthew G. Knepley PetscReal R = 1.0; 40629318fe57SMatthew G. Knepley 40639566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R, &R, &flg)); 40649566063dSJacob Faibussowitsch PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R)); 40659371c9d4SSatish Balay } break; 40669371c9d4SSatish Balay case DM_SHAPE_CYLINDER: { 40679318fe57SMatthew G. Knepley DMBoundaryType bdt = DM_BOUNDARY_NONE; 40689318fe57SMatthew G. Knepley PetscInt Nw = 6; 40699318fe57SMatthew G. Knepley 40709566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum)bdt, (PetscEnum *)&bdt, NULL)); 40719566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL)); 40729318fe57SMatthew G. Knepley switch (cell) { 4073d71ae5a4SJacob Faibussowitsch case DM_POLYTOPE_TRI_PRISM_TENSOR: 4074d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate)); 4075d71ae5a4SJacob Faibussowitsch break; 4076d71ae5a4SJacob Faibussowitsch default: 4077d71ae5a4SJacob Faibussowitsch PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt)); 4078d71ae5a4SJacob Faibussowitsch break; 40799318fe57SMatthew G. Knepley } 40809371c9d4SSatish Balay } break; 4081b7f5c055SJed Brown case DM_SHAPE_SCHWARZ_P: // fallthrough 40829371c9d4SSatish Balay case DM_SHAPE_GYROID: { 4083b7f5c055SJed Brown PetscInt extent[3] = {1, 1, 1}, refine = 0, layers = 0, three; 4084b7f5c055SJed Brown PetscReal thickness = 0.; 4085b7f5c055SJed Brown DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE}; 4086b7f5c055SJed Brown DMPlexTPSType tps_type = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID; 40871436d7faSJed Brown PetscBool tps_distribute; 40889566063dSJacob Faibussowitsch PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three = 3, &three), NULL)); 40899566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL)); 40909566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum *)periodic, (three = 3, &three), NULL)); 40919566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL)); 40929566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL)); 40939566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute)); 40949566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL)); 40959566063dSJacob Faibussowitsch PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness)); 40969371c9d4SSatish Balay } break; 40979371c9d4SSatish Balay case DM_SHAPE_DOUBLET: { 409805bd46c0SStefano Zampini DM dmnew; 409905bd46c0SStefano Zampini PetscReal rl = 0.0; 410005bd46c0SStefano Zampini 410105bd46c0SStefano Zampini PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL)); 410205bd46c0SStefano Zampini PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew)); 41035de52c6dSVaclav Hapla PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew)); 410469d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &dmnew)); 41059371c9d4SSatish Balay } break; 4106cfb853baSMatthew G. Knepley case DM_SHAPE_HYPERCUBIC: { 4107cfb853baSMatthew G. Knepley PetscInt *edges; 4108cfb853baSMatthew G. Knepley PetscReal *lower, *upper; 4109cfb853baSMatthew G. Knepley DMBoundaryType *bdt; 4110cfb853baSMatthew G. Knepley PetscInt n, d; 4111cfb853baSMatthew G. Knepley 4112cfb853baSMatthew G. Knepley *useCoordSpace = PETSC_FALSE; 4113cfb853baSMatthew G. Knepley PetscCall(PetscMalloc4(dim, &edges, dim, &lower, dim, &upper, dim, &bdt)); 4114cfb853baSMatthew G. Knepley for (d = 0; d < dim; ++d) { 4115cfb853baSMatthew G. Knepley edges[d] = 1; 4116cfb853baSMatthew G. Knepley lower[d] = 0.; 4117cfb853baSMatthew G. Knepley upper[d] = 1.; 4118cfb853baSMatthew G. Knepley bdt[d] = DM_BOUNDARY_PERIODIC; 4119cfb853baSMatthew G. Knepley } 4120cfb853baSMatthew G. Knepley n = dim; 4121cfb853baSMatthew G. Knepley PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", edges, &n, &flg)); 4122cfb853baSMatthew G. Knepley n = dim; 4123cfb853baSMatthew G. Knepley PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg)); 4124cfb853baSMatthew G. Knepley PetscCheck(!flg || n == dim, comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim); 4125cfb853baSMatthew G. Knepley n = dim; 4126cfb853baSMatthew G. Knepley PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg)); 4127cfb853baSMatthew G. Knepley PetscCheck(!flg || n == dim, comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim); 4128cfb853baSMatthew G. Knepley n = dim; 4129cfb853baSMatthew G. Knepley PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg)); 4130cfb853baSMatthew G. Knepley PetscCheck(!flg || n == dim, comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim); 4131cfb853baSMatthew G. Knepley PetscCall(DMPlexCreateHypercubicMesh_Internal(dm, dim, lower, upper, edges, bdt)); 4132cfb853baSMatthew G. Knepley PetscCall(PetscFree4(edges, lower, upper, bdt)); 4133cfb853baSMatthew G. Knepley } break; 4134d71ae5a4SJacob Faibussowitsch default: 4135d71ae5a4SJacob Faibussowitsch SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]); 41369318fe57SMatthew G. Knepley } 41379318fe57SMatthew G. Knepley } 41389566063dSJacob Faibussowitsch PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE)); 413948a46eb9SPierre Jolivet if (!((PetscObject)dm)->name && nameflg) PetscCall(PetscObjectSetName((PetscObject)dm, plexname)); 41404e22dd4cSMatthew G. Knepley // Allow label creation 41414e22dd4cSMatthew G. Knepley PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_label_", &option, NULL, &flg)); 41424e22dd4cSMatthew G. Knepley if (flg) { 41434e22dd4cSMatthew G. Knepley DMLabel label; 41444e22dd4cSMatthew G. Knepley PetscInt points[1024], n = 1024; 41454e22dd4cSMatthew G. Knepley char fulloption[PETSC_MAX_PATH_LEN]; 41464e22dd4cSMatthew G. Knepley const char *name = &option[14]; 41474e22dd4cSMatthew G. Knepley 41484e22dd4cSMatthew G. Knepley PetscCall(DMCreateLabel(dm, name)); 41494e22dd4cSMatthew G. Knepley PetscCall(DMGetLabel(dm, name, &label)); 41504e22dd4cSMatthew G. Knepley fulloption[0] = '-'; 41514e22dd4cSMatthew G. Knepley fulloption[1] = 0; 41524e22dd4cSMatthew G. Knepley PetscCall(PetscStrlcat(fulloption, option, PETSC_MAX_PATH_LEN)); 41534e22dd4cSMatthew G. Knepley PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, NULL)); 41544e22dd4cSMatthew G. Knepley for (PetscInt p = 0; p < n; ++p) PetscCall(DMLabelSetValue(label, points[p], 1)); 41554e22dd4cSMatthew G. Knepley } 4156dd0eeac9SMatthew G. Knepley // Allow cohesive label creation 4157dd0eeac9SMatthew G. Knepley // Faces are input, completed, and all points are marked with their depth 4158dd0eeac9SMatthew G. Knepley PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_cohesive_label_", &option, NULL, &flg)); 4159dd0eeac9SMatthew G. Knepley if (flg) { 4160dd0eeac9SMatthew G. Knepley DMLabel label; 4161dd0eeac9SMatthew G. Knepley PetscInt points[1024], n, pStart, pEnd, Nl = 1; 4162dd0eeac9SMatthew G. Knepley char fulloption[PETSC_MAX_PATH_LEN]; 4163dd0eeac9SMatthew G. Knepley char name[PETSC_MAX_PATH_LEN]; 4164dd0eeac9SMatthew G. Knepley size_t len; 4165dd0eeac9SMatthew G. Knepley 4166dd0eeac9SMatthew G. Knepley PetscCall(DMPlexGetChart(dm, &pStart, &pEnd)); 4167dd0eeac9SMatthew G. Knepley PetscCall(PetscStrncpy(name, &option[23], PETSC_MAX_PATH_LEN)); 4168dd0eeac9SMatthew G. Knepley PetscCall(PetscStrlen(name, &len)); 4169dd0eeac9SMatthew G. Knepley if (name[len - 1] == '0') Nl = 10; 4170dd0eeac9SMatthew G. Knepley for (PetscInt l = 0; l < Nl; ++l) { 4171dd0eeac9SMatthew G. Knepley if (l > 0) name[len - 1] = '0' + l; 4172dd0eeac9SMatthew G. Knepley fulloption[0] = 0; 4173dd0eeac9SMatthew G. Knepley PetscCall(PetscStrlcat(fulloption, "-dm_plex_cohesive_label_", 32)); 4174dd0eeac9SMatthew G. Knepley PetscCall(PetscStrlcat(fulloption, name, PETSC_MAX_PATH_LEN - 32)); 4175dd0eeac9SMatthew G. Knepley n = 1024; 4176dd0eeac9SMatthew G. Knepley PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, &flg)); 4177dd0eeac9SMatthew G. Knepley if (!flg) break; 4178dd0eeac9SMatthew G. Knepley PetscCall(DMCreateLabel(dm, name)); 4179dd0eeac9SMatthew G. Knepley PetscCall(DMGetLabel(dm, name, &label)); 4180dd0eeac9SMatthew G. Knepley if (pStart >= pEnd) n = 0; 4181dd0eeac9SMatthew G. Knepley for (PetscInt p = 0; p < n; ++p) { 4182dd0eeac9SMatthew G. Knepley const PetscInt point = points[p]; 4183dd0eeac9SMatthew G. Knepley PetscInt *closure = NULL; 4184dd0eeac9SMatthew G. Knepley PetscInt clSize, pdepth; 4185dd0eeac9SMatthew G. Knepley 4186dd0eeac9SMatthew G. Knepley PetscCall(DMPlexGetPointDepth(dm, point, &pdepth)); 4187dd0eeac9SMatthew G. Knepley PetscCall(DMLabelSetValue(label, point, pdepth)); 4188dd0eeac9SMatthew G. Knepley PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure)); 4189dd0eeac9SMatthew G. Knepley for (PetscInt cl = 0; cl < clSize * 2; cl += 2) { 4190dd0eeac9SMatthew G. Knepley PetscCall(DMPlexGetPointDepth(dm, closure[cl], &pdepth)); 4191dd0eeac9SMatthew G. Knepley PetscCall(DMLabelSetValue(label, closure[cl], pdepth)); 4192dd0eeac9SMatthew G. Knepley } 4193dd0eeac9SMatthew G. Knepley PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure)); 4194dd0eeac9SMatthew G. Knepley } 4195dd0eeac9SMatthew G. Knepley PetscCall(DMPlexOrientLabel(dm, label)); 41960542aa8cSMatthew G. Knepley PetscCall(DMPlexLabelCohesiveComplete(dm, label, NULL, 1, PETSC_FALSE, PETSC_FALSE, NULL)); 4197dd0eeac9SMatthew G. Knepley } 4198dd0eeac9SMatthew G. Knepley } 4199708be2fdSJed Brown PetscCall(PetscLogEventEnd(DMPLEX_CreateFromOptions, dm, 0, 0, 0)); 42003ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 42010a6ba040SMatthew G. Knepley } 42020a6ba040SMatthew G. Knepley 4203d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM dm, PetscOptionItems *PetscOptionsObject) 4204d71ae5a4SJacob Faibussowitsch { 42050a6ba040SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 42067f9d8d6cSVaclav Hapla PetscBool flg, flg2; 42079318fe57SMatthew G. Knepley char bdLabel[PETSC_MAX_PATH_LEN]; 4208adc21957SMatthew G. Knepley char method[PETSC_MAX_PATH_LEN]; 42090a6ba040SMatthew G. Knepley 42100a6ba040SMatthew G. Knepley PetscFunctionBegin; 42110a6ba040SMatthew G. Knepley /* Handle viewing */ 42129566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL)); 42135962854dSMatthew G. Knepley PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level for all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL, 0)); 42145962854dSMatthew G. Knepley PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fvm", "Debug output level for all fvm computations", "DMPlexSNESComputeResidualFVM", 0, &mesh->printFVM, NULL, 0)); 42159566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL)); 42169566063dSJacob Faibussowitsch PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL, 0)); 4217f5867de0SMatthew G. Knepley PetscCall(PetscOptionsBoundedInt("-dm_plex_print_locate", "Debug output level all point location computations", "DMLocatePoints", 0, &mesh->printLocate, NULL, 0)); 42189566063dSJacob Faibussowitsch PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg)); 42199566063dSJacob Faibussowitsch if (flg) PetscCall(PetscLogDefaultBegin()); 42209318fe57SMatthew G. Knepley /* Labeling */ 42219566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg)); 42229566063dSJacob Faibussowitsch if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel)); 4223953fc75cSMatthew G. Knepley /* Point Location */ 42249566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL)); 42250848f4b5SMatthew G. Knepley /* Partitioning and distribution */ 42269566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL)); 4227d02c7345SMatthew G. Knepley /* Reordering */ 4228adc21957SMatthew G. Knepley PetscCall(PetscOptionsBool("-dm_reorder_section", "Compute point permutation for local section", "DMReorderSectionSetDefault", PETSC_FALSE, &flg2, &flg)); 4229adc21957SMatthew G. Knepley if (flg) PetscCall(DMReorderSectionSetDefault(dm, flg2 ? DM_REORDER_DEFAULT_TRUE : DM_REORDER_DEFAULT_FALSE)); 4230adc21957SMatthew G. Knepley PetscCall(PetscOptionsString("-dm_reorder_section_type", "Reordering method for local section", "DMReorderSectionSetType", method, method, PETSC_MAX_PATH_LEN, &flg)); 4231adc21957SMatthew G. Knepley if (flg) PetscCall(DMReorderSectionSetType(dm, method)); 42322e62ab5aSMatthew G. Knepley /* Generation and remeshing */ 42339566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL)); 4234b29cfa1cSToby Isaac /* Projection behavior */ 4235d5b43468SJose E. Roman PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maximum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL, 0)); 42369566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL)); 4237f12cf164SMatthew G. Knepley /* Checking structure */ 4238f12cf164SMatthew G. Knepley { 42397f9d8d6cSVaclav Hapla PetscBool all = PETSC_FALSE; 4240f12cf164SMatthew G. Knepley 42417f9d8d6cSVaclav Hapla PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL)); 42427f9d8d6cSVaclav Hapla if (all) { 42437f9d8d6cSVaclav Hapla PetscCall(DMPlexCheck(dm)); 42447f9d8d6cSVaclav Hapla } else { 42459566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2)); 42467f9d8d6cSVaclav Hapla if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm)); 42479566063dSJacob 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)); 42487f9d8d6cSVaclav Hapla if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0)); 42499566063dSJacob 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)); 42507f9d8d6cSVaclav Hapla if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0)); 42519566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2)); 42527f9d8d6cSVaclav Hapla if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm)); 42539566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2)); 4254d7d32a9aSMatthew G. Knepley if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE)); 42559566063dSJacob 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)); 42567f9d8d6cSVaclav Hapla if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm)); 42577f9d8d6cSVaclav Hapla } 42589566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2)); 42599566063dSJacob Faibussowitsch if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE)); 4260f12cf164SMatthew G. Knepley } 42619318fe57SMatthew G. Knepley { 42629318fe57SMatthew G. Knepley PetscReal scale = 1.0; 42634f3833eaSMatthew G. Knepley 42649566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg)); 42659318fe57SMatthew G. Knepley if (flg) { 42669318fe57SMatthew G. Knepley Vec coordinates, coordinatesLocal; 42679318fe57SMatthew G. Knepley 42689566063dSJacob Faibussowitsch PetscCall(DMGetCoordinates(dm, &coordinates)); 42699566063dSJacob Faibussowitsch PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal)); 42709566063dSJacob Faibussowitsch PetscCall(VecScale(coordinates, scale)); 42719566063dSJacob Faibussowitsch PetscCall(VecScale(coordinatesLocal, scale)); 42729318fe57SMatthew G. Knepley } 42739318fe57SMatthew G. Knepley } 42749566063dSJacob Faibussowitsch PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner)); 42753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 427668d4fef7SMatthew G. Knepley } 427768d4fef7SMatthew G. Knepley 4278d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_Overlap_Plex(DM dm, PetscOptionItems *PetscOptionsObject, PetscInt *overlap) 4279d71ae5a4SJacob Faibussowitsch { 4280c506a872SMatthew G. Knepley PetscInt numOvLabels = 16, numOvExLabels = 16; 4281c506a872SMatthew G. Knepley char *ovLabelNames[16], *ovExLabelNames[16]; 4282c506a872SMatthew G. Knepley PetscInt numOvValues = 16, numOvExValues = 16, l; 4283c506a872SMatthew G. Knepley PetscBool flg; 4284c506a872SMatthew G. Knepley 4285c506a872SMatthew G. Knepley PetscFunctionBegin; 4286c506a872SMatthew G. Knepley PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0)); 4287c506a872SMatthew G. Knepley PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg)); 4288c506a872SMatthew G. Knepley if (!flg) numOvLabels = 0; 4289c506a872SMatthew G. Knepley if (numOvLabels) { 4290c506a872SMatthew G. Knepley ((DM_Plex *)dm->data)->numOvLabels = numOvLabels; 4291c506a872SMatthew G. Knepley for (l = 0; l < numOvLabels; ++l) { 4292c506a872SMatthew G. Knepley PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex *)dm->data)->ovLabels[l])); 4293c506a872SMatthew G. Knepley PetscCheck(((DM_Plex *)dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]); 4294c506a872SMatthew G. Knepley PetscCall(PetscFree(ovLabelNames[l])); 4295c506a872SMatthew G. Knepley } 4296c506a872SMatthew G. Knepley PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovValues, &numOvValues, &flg)); 4297c506a872SMatthew G. Knepley if (!flg) numOvValues = 0; 4298c506a872SMatthew G. Knepley PetscCheck(numOvLabels == numOvValues, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "The number of labels %" PetscInt_FMT " must match the number of values %" PetscInt_FMT, numOvLabels, numOvValues); 4299c506a872SMatthew G. Knepley 4300c506a872SMatthew G. Knepley PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg)); 4301c506a872SMatthew G. Knepley if (!flg) numOvExLabels = 0; 4302c506a872SMatthew G. Knepley ((DM_Plex *)dm->data)->numOvExLabels = numOvExLabels; 4303c506a872SMatthew G. Knepley for (l = 0; l < numOvExLabels; ++l) { 4304c506a872SMatthew G. Knepley PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex *)dm->data)->ovExLabels[l])); 4305c506a872SMatthew G. Knepley PetscCheck(((DM_Plex *)dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]); 4306c506a872SMatthew G. Knepley PetscCall(PetscFree(ovExLabelNames[l])); 4307c506a872SMatthew G. Knepley } 4308c506a872SMatthew G. Knepley PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovExValues, &numOvExValues, &flg)); 4309c506a872SMatthew G. Knepley if (!flg) numOvExValues = 0; 4310c506a872SMatthew G. Knepley PetscCheck(numOvExLabels == numOvExValues, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "The number of exclude labels %" PetscInt_FMT " must match the number of values %" PetscInt_FMT, numOvExLabels, numOvExValues); 4311c506a872SMatthew G. Knepley } 43123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4313c506a872SMatthew G. Knepley } 4314c506a872SMatthew G. Knepley 4315d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetFromOptions_Plex(DM dm, PetscOptionItems *PetscOptionsObject) 4316d71ae5a4SJacob Faibussowitsch { 4317bdf63967SMatthew G. Knepley PetscFunctionList ordlist; 4318bdf63967SMatthew G. Knepley char oname[256]; 43194e22dd4cSMatthew G. Knepley char sublabelname[PETSC_MAX_PATH_LEN] = ""; 4320adc21957SMatthew G. Knepley DMReorderDefaultFlag reorder; 4321d410b0cfSMatthew G. Knepley PetscReal volume = -1.0; 43229318fe57SMatthew G. Knepley PetscInt prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim; 4323a286e215SMatthew G. Knepley PetscBool uniformOrig = PETSC_FALSE, created = PETSC_FALSE, uniform = PETSC_TRUE, distribute, saveSF = PETSC_FALSE, interpolate = PETSC_TRUE, coordSpace = PETSC_TRUE, remap = PETSC_TRUE, ghostCells = PETSC_FALSE, isHierarchy, ignoreModel = PETSC_FALSE, flg; 432468d4fef7SMatthew G. Knepley 432568d4fef7SMatthew G. Knepley PetscFunctionBegin; 4326d0609cedSBarry Smith PetscOptionsHeadBegin(PetscOptionsObject, "DMPlex Options"); 4327dd4c3f67SMatthew G. Knepley if (dm->cloneOpts) goto non_refine; 43289318fe57SMatthew G. Knepley /* Handle automatic creation */ 43299566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 43306bc1bd01Sksagiyam if (dim < 0) { 43316bc1bd01Sksagiyam PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm)); 43326bc1bd01Sksagiyam created = PETSC_TRUE; 43336bc1bd01Sksagiyam } 43346bc1bd01Sksagiyam PetscCall(DMGetDimension(dm, &dim)); 4335d89e6e46SMatthew G. Knepley /* Handle interpolation before distribution */ 43369566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg)); 4337d89e6e46SMatthew G. Knepley if (flg) { 4338d89e6e46SMatthew G. Knepley DMPlexInterpolatedFlag interpolated; 4339d89e6e46SMatthew G. Knepley 43409566063dSJacob Faibussowitsch PetscCall(DMPlexIsInterpolated(dm, &interpolated)); 4341d89e6e46SMatthew G. Knepley if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) { 4342d89e6e46SMatthew G. Knepley DM udm; 4343d89e6e46SMatthew G. Knepley 43449566063dSJacob Faibussowitsch PetscCall(DMPlexUninterpolate(dm, &udm)); 434569d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &udm)); 4346d89e6e46SMatthew G. Knepley } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) { 4347d89e6e46SMatthew G. Knepley DM idm; 4348d89e6e46SMatthew G. Knepley 43499566063dSJacob Faibussowitsch PetscCall(DMPlexInterpolate(dm, &idm)); 435069d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &idm)); 4351d89e6e46SMatthew G. Knepley } 4352d89e6e46SMatthew G. Knepley } 43534e22dd4cSMatthew G. Knepley // Handle submesh selection before distribution 43544e22dd4cSMatthew G. Knepley PetscCall(PetscOptionsString("-dm_plex_submesh", "Label to use for submesh selection", "", sublabelname, sublabelname, PETSC_MAX_PATH_LEN, &flg)); 43554e22dd4cSMatthew G. Knepley if (flg) { 43564e22dd4cSMatthew G. Knepley DM subdm; 43574e22dd4cSMatthew G. Knepley DMLabel label; 43584e22dd4cSMatthew G. Knepley IS valueIS, pointIS; 43594e22dd4cSMatthew G. Knepley const PetscInt *values, *points; 43604e22dd4cSMatthew G. Knepley PetscBool markedFaces = PETSC_FALSE; 43614e22dd4cSMatthew G. Knepley PetscInt Nv, value, Np; 43624e22dd4cSMatthew G. Knepley 43634e22dd4cSMatthew G. Knepley PetscCall(DMGetLabel(dm, sublabelname, &label)); 43644e22dd4cSMatthew G. Knepley PetscCall(DMLabelGetNumValues(label, &Nv)); 43654e22dd4cSMatthew G. Knepley PetscCheck(Nv == 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Only a single label value is currently supported for submesh selection, not %" PetscInt_FMT, Nv); 43664e22dd4cSMatthew G. Knepley PetscCall(DMLabelGetValueIS(label, &valueIS)); 43674e22dd4cSMatthew G. Knepley PetscCall(ISGetIndices(valueIS, &values)); 43684e22dd4cSMatthew G. Knepley value = values[0]; 43694e22dd4cSMatthew G. Knepley PetscCall(ISRestoreIndices(valueIS, &values)); 43704e22dd4cSMatthew G. Knepley PetscCall(ISDestroy(&valueIS)); 43714e22dd4cSMatthew G. Knepley PetscCall(DMLabelGetStratumSize(label, value, &Np)); 43724e22dd4cSMatthew G. Knepley PetscCall(DMLabelGetStratumIS(label, value, &pointIS)); 43734e22dd4cSMatthew G. Knepley PetscCall(ISGetIndices(pointIS, &points)); 43744e22dd4cSMatthew G. Knepley for (PetscInt p = 0; p < Np; ++p) { 43754e22dd4cSMatthew G. Knepley PetscInt pdepth; 43764e22dd4cSMatthew G. Knepley 43774e22dd4cSMatthew G. Knepley PetscCall(DMPlexGetPointDepth(dm, points[p], &pdepth)); 43784e22dd4cSMatthew G. Knepley if (pdepth) { 43794e22dd4cSMatthew G. Knepley markedFaces = PETSC_TRUE; 43804e22dd4cSMatthew G. Knepley break; 43814e22dd4cSMatthew G. Knepley } 43824e22dd4cSMatthew G. Knepley } 43834e22dd4cSMatthew G. Knepley PetscCall(ISRestoreIndices(pointIS, &points)); 43844e22dd4cSMatthew G. Knepley PetscCall(ISDestroy(&pointIS)); 43854e22dd4cSMatthew G. Knepley PetscCall(DMPlexCreateSubmesh(dm, label, value, markedFaces, &subdm)); 43864e22dd4cSMatthew G. Knepley PetscCall(DMPlexReplace_Internal(dm, &subdm)); 43874e22dd4cSMatthew G. Knepley PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject)); 43884e22dd4cSMatthew G. Knepley } 43899b44eab4SMatthew G. Knepley /* Handle DMPlex refinement before distribution */ 43909566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_refine_ignore_model", "Flag to ignore the geometry model when refining", "DMCreate", ignoreModel, &ignoreModel, &flg)); 4391ad540459SPierre Jolivet if (flg) ((DM_Plex *)dm->data)->ignoreModel = ignoreModel; 43929566063dSJacob Faibussowitsch PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig)); 43939566063dSJacob Faibussowitsch PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL, 0)); 43949566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL)); 43959566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg)); 43969566063dSJacob Faibussowitsch if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform)); 43979566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg)); 43989318fe57SMatthew G. Knepley if (flg) { 43999566063dSJacob Faibussowitsch PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE)); 44009566063dSJacob Faibussowitsch PetscCall(DMPlexSetRefinementLimit(dm, volume)); 44019318fe57SMatthew G. Knepley prerefine = PetscMax(prerefine, 1); 44029318fe57SMatthew G. Knepley } 4403b23db253SStefano Zampini if (prerefine) PetscCall(DMLocalizeCoordinates(dm)); 44049b44eab4SMatthew G. Knepley for (r = 0; r < prerefine; ++r) { 44059b44eab4SMatthew G. Knepley DM rdm; 44069b44eab4SMatthew G. Knepley PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc; 44079b44eab4SMatthew G. Knepley 4408dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject)); 44099566063dSJacob Faibussowitsch PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm)); 441069d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &rdm)); 4411dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject)); 441261a622f3SMatthew G. Knepley if (coordFunc && remap) { 44139566063dSJacob Faibussowitsch PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc)); 44149b44eab4SMatthew G. Knepley ((DM_Plex *)dm->data)->coordFunc = coordFunc; 44159b44eab4SMatthew G. Knepley } 44169b44eab4SMatthew G. Knepley } 44179566063dSJacob Faibussowitsch PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig)); 44189318fe57SMatthew G. Knepley /* Handle DMPlex extrusion before distribution */ 44199566063dSJacob Faibussowitsch PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0)); 44209318fe57SMatthew G. Knepley if (extLayers) { 44219318fe57SMatthew G. Knepley DM edm; 44229318fe57SMatthew G. Knepley 44239566063dSJacob Faibussowitsch PetscCall(DMExtrude(dm, extLayers, &edm)); 442469d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &edm)); 442548d16a33SMatthew G. Knepley ((DM_Plex *)dm->data)->coordFunc = NULL; 4426dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject)); 4427d410b0cfSMatthew G. Knepley extLayers = 0; 44285e17fc22SAidan Hamilton PetscCall(DMGetDimension(dm, &dim)); 44299318fe57SMatthew G. Knepley } 4430bdf63967SMatthew G. Knepley /* Handle DMPlex reordering before distribution */ 44316bc1bd01Sksagiyam PetscCall(DMPlexReorderGetDefault(dm, &reorder)); 44329566063dSJacob Faibussowitsch PetscCall(MatGetOrderingList(&ordlist)); 44336bc1bd01Sksagiyam PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname))); 44349566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg)); 4435adc21957SMatthew G. Knepley if (reorder == DM_REORDER_DEFAULT_TRUE || flg) { 4436bdf63967SMatthew G. Knepley DM pdm; 4437bdf63967SMatthew G. Knepley IS perm; 4438bdf63967SMatthew G. Knepley 44399566063dSJacob Faibussowitsch PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm)); 44409566063dSJacob Faibussowitsch PetscCall(DMPlexPermute(dm, perm, &pdm)); 44419566063dSJacob Faibussowitsch PetscCall(ISDestroy(&perm)); 444269d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &pdm)); 4443dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject)); 4444bdf63967SMatthew G. Knepley } 44459b44eab4SMatthew G. Knepley /* Handle DMPlex distribution */ 44469566063dSJacob Faibussowitsch PetscCall(DMPlexDistributeGetDefault(dm, &distribute)); 4447c506a872SMatthew G. Knepley PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL)); 4448a286e215SMatthew G. Knepley PetscCall(PetscOptionsBool("-dm_distribute_save_sf", "Flag to save the migration SF", "DMPlexSetMigrationSF", saveSF, &saveSF, NULL)); 4449dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap)); 44509b44eab4SMatthew G. Knepley if (distribute) { 44519b44eab4SMatthew G. Knepley DM pdm = NULL; 44529b44eab4SMatthew G. Knepley PetscPartitioner part; 4453a286e215SMatthew G. Knepley PetscSF sfMigration; 44549b44eab4SMatthew G. Knepley 44559566063dSJacob Faibussowitsch PetscCall(DMPlexGetPartitioner(dm, &part)); 44569566063dSJacob Faibussowitsch PetscCall(PetscPartitionerSetFromOptions(part)); 4457a286e215SMatthew G. Knepley PetscCall(DMPlexDistribute(dm, overlap, &sfMigration, &pdm)); 445848a46eb9SPierre Jolivet if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm)); 4459a286e215SMatthew G. Knepley if (saveSF) PetscCall(DMPlexSetMigrationSF(dm, sfMigration)); 4460a286e215SMatthew G. Knepley PetscCall(PetscSFDestroy(&sfMigration)); 44619b44eab4SMatthew G. Knepley } 4462d2b2dc1eSMatthew G. Knepley /* Must check CEED options before creating function space for coordinates */ 4463d2b2dc1eSMatthew G. Knepley { 4464d2b2dc1eSMatthew G. Knepley PetscBool useCeed = PETSC_FALSE, flg; 4465d2b2dc1eSMatthew G. Knepley 4466d2b2dc1eSMatthew G. Knepley PetscCall(PetscOptionsBool("-dm_plex_use_ceed", "Use LibCEED as the FEM backend", "DMPlexSetUseCeed", useCeed, &useCeed, &flg)); 4467d2b2dc1eSMatthew G. Knepley if (flg) PetscCall(DMPlexSetUseCeed(dm, useCeed)); 4468d2b2dc1eSMatthew G. Knepley } 44699318fe57SMatthew G. Knepley /* Create coordinate space */ 44709318fe57SMatthew G. Knepley if (created) { 447161a622f3SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 4472e44f6aebSMatthew G. Knepley PetscInt degree = 1, deg; 44735515ebd3SMatthew G. Knepley PetscInt height = 0; 44745515ebd3SMatthew G. Knepley DM cdm; 4475c3db174cSMatthew G. Knepley PetscBool flg, localize = PETSC_TRUE, sparseLocalize = PETSC_TRUE; 44769318fe57SMatthew G. Knepley 44779566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg)); 44789566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, °ree, NULL)); 4479e44f6aebSMatthew G. Knepley PetscCall(DMGetCoordinateDegree_Internal(dm, °)); 4480e44f6aebSMatthew G. Knepley if (coordSpace && deg <= 1) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, PETSC_TRUE, mesh->coordFunc)); 44815515ebd3SMatthew G. Knepley PetscCall(DMGetCoordinateDM(dm, &cdm)); 448261a622f3SMatthew G. Knepley if (flg && !coordSpace) { 448361a622f3SMatthew G. Knepley PetscDS cds; 448461a622f3SMatthew G. Knepley PetscObject obj; 448561a622f3SMatthew G. Knepley PetscClassId id; 448661a622f3SMatthew G. Knepley 44879566063dSJacob Faibussowitsch PetscCall(DMGetDS(cdm, &cds)); 44889566063dSJacob Faibussowitsch PetscCall(PetscDSGetDiscretization(cds, 0, &obj)); 44899566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(obj, &id)); 449061a622f3SMatthew G. Knepley if (id == PETSCFE_CLASSID) { 449161a622f3SMatthew G. Knepley PetscContainer dummy; 449261a622f3SMatthew G. Knepley 44939566063dSJacob Faibussowitsch PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy)); 44949566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)dummy, "coordinates")); 44959566063dSJacob Faibussowitsch PetscCall(DMSetField(cdm, 0, NULL, (PetscObject)dummy)); 44969566063dSJacob Faibussowitsch PetscCall(PetscContainerDestroy(&dummy)); 44979566063dSJacob Faibussowitsch PetscCall(DMClearDS(cdm)); 449861a622f3SMatthew G. Knepley } 449961a622f3SMatthew G. Knepley mesh->coordFunc = NULL; 450061a622f3SMatthew G. Knepley } 4501c3db174cSMatthew G. Knepley PetscCall(PetscOptionsBool("-dm_localize", "Localize mesh coordinates", "", localize, &localize, NULL)); 4502c3db174cSMatthew G. Knepley PetscCall(PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "DMSetSparseLocalize", sparseLocalize, &sparseLocalize, &flg)); 4503c3db174cSMatthew G. Knepley if (flg) PetscCall(DMSetSparseLocalize(dm, sparseLocalize)); 45045515ebd3SMatthew G. Knepley PetscCall(PetscOptionsInt("-dm_localize_height", "Localize edges and faces in addition to cells", "", height, &height, &flg)); 45055515ebd3SMatthew G. Knepley if (flg) PetscCall(DMPlexSetMaxProjectionHeight(cdm, height)); 4506c3db174cSMatthew G. Knepley if (localize) PetscCall(DMLocalizeCoordinates(dm)); 45079318fe57SMatthew G. Knepley } 450868d4fef7SMatthew G. Knepley /* Handle DMPlex refinement */ 450961a622f3SMatthew G. Knepley remap = PETSC_TRUE; 45109566063dSJacob Faibussowitsch PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL, 0)); 45119566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL)); 45129566063dSJacob Faibussowitsch PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy, 0)); 45139566063dSJacob Faibussowitsch if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE)); 451468d4fef7SMatthew G. Knepley if (refine && isHierarchy) { 4515acdc6f61SToby Isaac DM *dms, coarseDM; 451668d4fef7SMatthew G. Knepley 45179566063dSJacob Faibussowitsch PetscCall(DMGetCoarseDM(dm, &coarseDM)); 45189566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)coarseDM)); 45199566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(refine, &dms)); 45209566063dSJacob Faibussowitsch PetscCall(DMRefineHierarchy(dm, refine, dms)); 452168d4fef7SMatthew G. Knepley /* Total hack since we do not pass in a pointer */ 45229566063dSJacob Faibussowitsch PetscCall(DMPlexSwap_Static(dm, dms[refine - 1])); 452368d4fef7SMatthew G. Knepley if (refine == 1) { 45249566063dSJacob Faibussowitsch PetscCall(DMSetCoarseDM(dm, dms[0])); 45259566063dSJacob Faibussowitsch PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE)); 452668d4fef7SMatthew G. Knepley } else { 45279566063dSJacob Faibussowitsch PetscCall(DMSetCoarseDM(dm, dms[refine - 2])); 45289566063dSJacob Faibussowitsch PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE)); 45299566063dSJacob Faibussowitsch PetscCall(DMSetCoarseDM(dms[0], dms[refine - 1])); 45309566063dSJacob Faibussowitsch PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE)); 453168d4fef7SMatthew G. Knepley } 45329566063dSJacob Faibussowitsch PetscCall(DMSetCoarseDM(dms[refine - 1], coarseDM)); 45339566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)coarseDM)); 453468d4fef7SMatthew G. Knepley /* Free DMs */ 453568d4fef7SMatthew G. Knepley for (r = 0; r < refine; ++r) { 4536dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject)); 45379566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dms[r])); 453868d4fef7SMatthew G. Knepley } 45399566063dSJacob Faibussowitsch PetscCall(PetscFree(dms)); 454068d4fef7SMatthew G. Knepley } else { 454168d4fef7SMatthew G. Knepley for (r = 0; r < refine; ++r) { 45429318fe57SMatthew G. Knepley DM rdm; 454351a74b61SMatthew G. Knepley PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc; 454468d4fef7SMatthew G. Knepley 4545dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject)); 45469566063dSJacob Faibussowitsch PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm)); 454768d4fef7SMatthew G. Knepley /* Total hack since we do not pass in a pointer */ 454869d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &rdm)); 4549dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject)); 455061a622f3SMatthew G. Knepley if (coordFunc && remap) { 45519566063dSJacob Faibussowitsch PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc)); 455251a74b61SMatthew G. Knepley ((DM_Plex *)dm->data)->coordFunc = coordFunc; 455351a74b61SMatthew G. Knepley } 455468d4fef7SMatthew G. Knepley } 455568d4fef7SMatthew G. Knepley } 45563cf6fe12SMatthew G. Knepley /* Handle DMPlex coarsening */ 45579566063dSJacob Faibussowitsch PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL, 0)); 45589566063dSJacob Faibussowitsch PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy, 0)); 4559b653a561SMatthew G. Knepley if (coarsen && isHierarchy) { 4560b653a561SMatthew G. Knepley DM *dms; 4561b653a561SMatthew G. Knepley 45629566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(coarsen, &dms)); 45639566063dSJacob Faibussowitsch PetscCall(DMCoarsenHierarchy(dm, coarsen, dms)); 4564b653a561SMatthew G. Knepley /* Free DMs */ 4565b653a561SMatthew G. Knepley for (r = 0; r < coarsen; ++r) { 4566dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject)); 45679566063dSJacob Faibussowitsch PetscCall(DMDestroy(&dms[r])); 4568b653a561SMatthew G. Knepley } 45699566063dSJacob Faibussowitsch PetscCall(PetscFree(dms)); 4570b653a561SMatthew G. Knepley } else { 4571b653a561SMatthew G. Knepley for (r = 0; r < coarsen; ++r) { 45729318fe57SMatthew G. Knepley DM cdm; 45739318fe57SMatthew G. Knepley PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc; 45743cf6fe12SMatthew G. Knepley 4575dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject)); 45769566063dSJacob Faibussowitsch PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &cdm)); 45773cf6fe12SMatthew G. Knepley /* Total hack since we do not pass in a pointer */ 457869d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &cdm)); 4579dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject)); 45809318fe57SMatthew G. Knepley if (coordFunc) { 45819566063dSJacob Faibussowitsch PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc)); 45829318fe57SMatthew G. Knepley ((DM_Plex *)dm->data)->coordFunc = coordFunc; 45839318fe57SMatthew G. Knepley } 45843cf6fe12SMatthew G. Knepley } 4585b653a561SMatthew G. Knepley } 4586be664eb1SMatthew G. Knepley // Handle coordinate remapping 4587be664eb1SMatthew G. Knepley remap = PETSC_FALSE; 4588be664eb1SMatthew G. Knepley PetscCall(PetscOptionsBool("-dm_coord_remap", "Flag to control coordinate remapping", "", remap, &remap, NULL)); 4589be664eb1SMatthew G. Knepley if (remap) { 4590be664eb1SMatthew G. Knepley DMPlexCoordMap map = DM_COORD_MAP_NONE; 4591be664eb1SMatthew G. Knepley PetscPointFunc mapFunc = NULL; 4592be664eb1SMatthew G. Knepley PetscScalar params[16]; 4593f45b553cSPierre Jolivet PetscInt Np = PETSC_STATIC_ARRAY_LENGTH(params), cdim; 4594be664eb1SMatthew G. Knepley MPI_Comm comm; 4595be664eb1SMatthew G. Knepley 4596be664eb1SMatthew G. Knepley PetscCall(PetscObjectGetComm((PetscObject)dm, &comm)); 4597be664eb1SMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 4598be664eb1SMatthew G. Knepley PetscCall(PetscOptionsScalarArray("-dm_coord_map_params", "Parameters for the coordinate remapping", "", params, &Np, &flg)); 4599be664eb1SMatthew G. Knepley if (!flg) Np = 0; 4600be664eb1SMatthew G. Knepley // TODO Allow user to pass a map function by name 4601be664eb1SMatthew G. Knepley PetscCall(PetscOptionsEnum("-dm_coord_map", "Coordinate mapping for built-in mesh", "", DMPlexCoordMaps, (PetscEnum)map, (PetscEnum *)&map, &flg)); 4602be664eb1SMatthew G. Knepley if (flg) { 4603be664eb1SMatthew G. Knepley switch (map) { 4604be664eb1SMatthew G. Knepley case DM_COORD_MAP_NONE: 4605be664eb1SMatthew G. Knepley mapFunc = coordMap_identity; 4606be664eb1SMatthew G. Knepley break; 4607be664eb1SMatthew G. Knepley case DM_COORD_MAP_SHEAR: 4608be664eb1SMatthew G. Knepley mapFunc = coordMap_shear; 4609be664eb1SMatthew G. Knepley if (!Np) { 4610be664eb1SMatthew G. Knepley Np = cdim + 1; 4611be664eb1SMatthew G. Knepley params[0] = 0; 4612be664eb1SMatthew G. Knepley for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0; 4613be664eb1SMatthew G. Knepley } 4614be664eb1SMatthew G. Knepley PetscCheck(Np == cdim + 1, comm, PETSC_ERR_ARG_WRONG, "The shear coordinate map must have cdim + 1 = %" PetscInt_FMT " parameters, not %" PetscInt_FMT, cdim + 1, Np); 4615be664eb1SMatthew G. Knepley break; 4616be664eb1SMatthew G. Knepley case DM_COORD_MAP_FLARE: 4617be664eb1SMatthew G. Knepley mapFunc = coordMap_flare; 4618be664eb1SMatthew G. Knepley if (!Np) { 4619be664eb1SMatthew G. Knepley Np = cdim + 1; 4620be664eb1SMatthew G. Knepley params[0] = 0; 4621be664eb1SMatthew G. Knepley for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0; 4622be664eb1SMatthew G. Knepley } 4623be664eb1SMatthew G. Knepley PetscCheck(Np == cdim + 1, comm, PETSC_ERR_ARG_WRONG, "The flare coordinate map must have cdim + 1 = %" PetscInt_FMT " parameters, not %" PetscInt_FMT, cdim + 1, Np); 4624be664eb1SMatthew G. Knepley break; 4625be664eb1SMatthew G. Knepley case DM_COORD_MAP_ANNULUS: 4626be664eb1SMatthew G. Knepley mapFunc = coordMap_annulus; 4627be664eb1SMatthew G. Knepley if (!Np) { 4628be664eb1SMatthew G. Knepley Np = 2; 4629be664eb1SMatthew G. Knepley params[0] = 1.; 4630be664eb1SMatthew G. Knepley params[1] = 2.; 4631be664eb1SMatthew G. Knepley } 4632be664eb1SMatthew G. Knepley PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The annulus coordinate map must have 2 parameters, not %" PetscInt_FMT, Np); 4633be664eb1SMatthew G. Knepley break; 4634be664eb1SMatthew G. Knepley case DM_COORD_MAP_SHELL: 4635be664eb1SMatthew G. Knepley mapFunc = coordMap_shell; 4636be664eb1SMatthew G. Knepley if (!Np) { 4637be664eb1SMatthew G. Knepley Np = 2; 4638be664eb1SMatthew G. Knepley params[0] = 1.; 4639be664eb1SMatthew G. Knepley params[1] = 2.; 4640be664eb1SMatthew G. Knepley } 4641be664eb1SMatthew G. Knepley PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The spherical shell coordinate map must have 2 parameters, not %" PetscInt_FMT, Np); 4642be664eb1SMatthew G. Knepley break; 4643be664eb1SMatthew G. Knepley default: 4644be664eb1SMatthew G. Knepley mapFunc = coordMap_identity; 4645be664eb1SMatthew G. Knepley } 4646be664eb1SMatthew G. Knepley } 4647be664eb1SMatthew G. Knepley if (Np) { 4648be664eb1SMatthew G. Knepley DM cdm; 4649be664eb1SMatthew G. Knepley PetscDS cds; 4650be664eb1SMatthew G. Knepley 4651be664eb1SMatthew G. Knepley PetscCall(DMGetCoordinateDM(dm, &cdm)); 4652be664eb1SMatthew G. Knepley PetscCall(DMGetDS(cdm, &cds)); 4653be664eb1SMatthew G. Knepley PetscCall(PetscDSSetConstants(cds, Np, params)); 4654be664eb1SMatthew G. Knepley } 4655be664eb1SMatthew G. Knepley PetscCall(DMPlexRemapGeometry(dm, 0.0, mapFunc)); 4656be664eb1SMatthew G. Knepley } 4657909dfd52SMatthew G. Knepley /* Handle ghost cells */ 46589566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL)); 4659909dfd52SMatthew G. Knepley if (ghostCells) { 4660909dfd52SMatthew G. Knepley DM gdm; 4661909dfd52SMatthew G. Knepley char lname[PETSC_MAX_PATH_LEN]; 4662909dfd52SMatthew G. Knepley 4663909dfd52SMatthew G. Knepley lname[0] = '\0'; 46649566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg)); 46659566063dSJacob Faibussowitsch PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm)); 466669d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &gdm)); 4667909dfd52SMatthew G. Knepley } 46686913077dSMatthew G. Knepley /* Handle 1D order */ 4669adc21957SMatthew G. Knepley if (reorder != DM_REORDER_DEFAULT_FALSE && dim == 1) { 46706913077dSMatthew G. Knepley DM cdm, rdm; 46716913077dSMatthew G. Knepley PetscDS cds; 46726913077dSMatthew G. Knepley PetscObject obj; 46736913077dSMatthew G. Knepley PetscClassId id = PETSC_OBJECT_CLASSID; 46746913077dSMatthew G. Knepley IS perm; 46756bc1bd01Sksagiyam PetscInt Nf; 46766913077dSMatthew G. Knepley PetscBool distributed; 46776913077dSMatthew G. Knepley 46789566063dSJacob Faibussowitsch PetscCall(DMPlexIsDistributed(dm, &distributed)); 46799566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 46809566063dSJacob Faibussowitsch PetscCall(DMGetDS(cdm, &cds)); 46819566063dSJacob Faibussowitsch PetscCall(PetscDSGetNumFields(cds, &Nf)); 46826913077dSMatthew G. Knepley if (Nf) { 46839566063dSJacob Faibussowitsch PetscCall(PetscDSGetDiscretization(cds, 0, &obj)); 46849566063dSJacob Faibussowitsch PetscCall(PetscObjectGetClassId(obj, &id)); 46856913077dSMatthew G. Knepley } 46866bc1bd01Sksagiyam if (!distributed && id != PETSCFE_CLASSID) { 46879566063dSJacob Faibussowitsch PetscCall(DMPlexGetOrdering1D(dm, &perm)); 46889566063dSJacob Faibussowitsch PetscCall(DMPlexPermute(dm, perm, &rdm)); 468969d8a87bSksagiyam PetscCall(DMPlexReplace_Internal(dm, &rdm)); 46909566063dSJacob Faibussowitsch PetscCall(ISDestroy(&perm)); 46916913077dSMatthew G. Knepley } 46926913077dSMatthew G. Knepley } 46933cf6fe12SMatthew G. Knepley /* Handle */ 4694dd4c3f67SMatthew G. Knepley non_refine: 4695dbbe0bcdSBarry Smith PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject)); 4696d0609cedSBarry Smith PetscOptionsHeadEnd(); 46973ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46980a6ba040SMatthew G. Knepley } 46990a6ba040SMatthew G. Knepley 4700d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateGlobalVector_Plex(DM dm, Vec *vec) 4701d71ae5a4SJacob Faibussowitsch { 4702552f7358SJed Brown PetscFunctionBegin; 47039566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector_Section_Private(dm, vec)); 47049566063dSJacob Faibussowitsch /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */ 47059566063dSJacob Faibussowitsch PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex)); 47069566063dSJacob Faibussowitsch PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void))VecView_Plex_Native)); 47079566063dSJacob Faibussowitsch PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex)); 47089566063dSJacob Faibussowitsch PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void))VecLoad_Plex_Native)); 47093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4710552f7358SJed Brown } 4711552f7358SJed Brown 4712d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateLocalVector_Plex(DM dm, Vec *vec) 4713d71ae5a4SJacob Faibussowitsch { 4714552f7358SJed Brown PetscFunctionBegin; 47159566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector_Section_Private(dm, vec)); 47169566063dSJacob Faibussowitsch PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex_Local)); 47179566063dSJacob Faibussowitsch PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex_Local)); 47183ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4719552f7358SJed Brown } 4720552f7358SJed Brown 4721d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd) 4722d71ae5a4SJacob Faibussowitsch { 4723793f3fe5SMatthew G. Knepley PetscInt depth, d; 4724793f3fe5SMatthew G. Knepley 4725793f3fe5SMatthew G. Knepley PetscFunctionBegin; 47269566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepth(dm, &depth)); 4727793f3fe5SMatthew G. Knepley if (depth == 1) { 47289566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &d)); 47299566063dSJacob Faibussowitsch if (dim == 0) PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd)); 47309566063dSJacob Faibussowitsch else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd)); 47319371c9d4SSatish Balay else { 47329371c9d4SSatish Balay *pStart = 0; 47339371c9d4SSatish Balay *pEnd = 0; 47349371c9d4SSatish Balay } 4735793f3fe5SMatthew G. Knepley } else { 47369566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd)); 4737793f3fe5SMatthew G. Knepley } 47383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4739793f3fe5SMatthew G. Knepley } 4740793f3fe5SMatthew G. Knepley 4741d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[]) 4742d71ae5a4SJacob Faibussowitsch { 4743502a2867SDave May PetscSF sf; 47440a19bb7dSprj- PetscInt niranks, njranks, n; 47450a19bb7dSprj- const PetscMPIInt *iranks, *jranks; 47460a19bb7dSprj- DM_Plex *data = (DM_Plex *)dm->data; 4747502a2867SDave May 47482f356facSMatthew G. Knepley PetscFunctionBegin; 47499566063dSJacob Faibussowitsch PetscCall(DMGetPointSF(dm, &sf)); 47500a19bb7dSprj- if (!data->neighbors) { 47519566063dSJacob Faibussowitsch PetscCall(PetscSFSetUp(sf)); 47529566063dSJacob Faibussowitsch PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL)); 47539566063dSJacob Faibussowitsch PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL)); 47549566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors)); 47559566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks)); 47569566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks)); 47570a19bb7dSprj- n = njranks + niranks; 47589566063dSJacob Faibussowitsch PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1)); 47590a19bb7dSprj- /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */ 47609566063dSJacob Faibussowitsch PetscCall(PetscMPIIntCast(n, data->neighbors)); 47610a19bb7dSprj- } 47620a19bb7dSprj- if (nranks) *nranks = data->neighbors[0]; 47630a19bb7dSprj- if (ranks) { 47640a19bb7dSprj- if (data->neighbors[0]) *ranks = data->neighbors + 1; 47650a19bb7dSprj- else *ranks = NULL; 47660a19bb7dSprj- } 47673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4768502a2867SDave May } 4769502a2867SDave May 47701eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec); 47711eb70e55SToby Isaac 4772d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMInitialize_Plex(DM dm) 4773d71ae5a4SJacob Faibussowitsch { 4774552f7358SJed Brown PetscFunctionBegin; 4775552f7358SJed Brown dm->ops->view = DMView_Plex; 47762c40f234SMatthew G. Knepley dm->ops->load = DMLoad_Plex; 4777552f7358SJed Brown dm->ops->setfromoptions = DMSetFromOptions_Plex; 477838221697SMatthew G. Knepley dm->ops->clone = DMClone_Plex; 4779552f7358SJed Brown dm->ops->setup = DMSetUp_Plex; 47801bb6d2a8SBarry Smith dm->ops->createlocalsection = DMCreateLocalSection_Plex; 4781adc21957SMatthew G. Knepley dm->ops->createsectionpermutation = DMCreateSectionPermutation_Plex; 478266ad2231SToby Isaac dm->ops->createdefaultconstraints = DMCreateDefaultConstraints_Plex; 4783552f7358SJed Brown dm->ops->createglobalvector = DMCreateGlobalVector_Plex; 4784552f7358SJed Brown dm->ops->createlocalvector = DMCreateLocalVector_Plex; 4785184d77edSJed Brown dm->ops->getlocaltoglobalmapping = NULL; 47860298fd71SBarry Smith dm->ops->createfieldis = NULL; 4787552f7358SJed Brown dm->ops->createcoordinatedm = DMCreateCoordinateDM_Plex; 4788f19dbd58SToby Isaac dm->ops->createcoordinatefield = DMCreateCoordinateField_Plex; 47890a6ba040SMatthew G. Knepley dm->ops->getcoloring = NULL; 4790552f7358SJed Brown dm->ops->creatematrix = DMCreateMatrix_Plex; 4791bceba477SMatthew G. Knepley dm->ops->createinterpolation = DMCreateInterpolation_Plex; 4792bd041c0cSMatthew G. Knepley dm->ops->createmassmatrix = DMCreateMassMatrix_Plex; 4793b4937a87SMatthew G. Knepley dm->ops->createmassmatrixlumped = DMCreateMassMatrixLumped_Plex; 47945a84ad33SLisandro Dalcin dm->ops->createinjection = DMCreateInjection_Plex; 4795552f7358SJed Brown dm->ops->refine = DMRefine_Plex; 47960a6ba040SMatthew G. Knepley dm->ops->coarsen = DMCoarsen_Plex; 47970a6ba040SMatthew G. Knepley dm->ops->refinehierarchy = DMRefineHierarchy_Plex; 4798b653a561SMatthew G. Knepley dm->ops->coarsenhierarchy = DMCoarsenHierarchy_Plex; 4799d410b0cfSMatthew G. Knepley dm->ops->extrude = DMExtrude_Plex; 48000298fd71SBarry Smith dm->ops->globaltolocalbegin = NULL; 48010298fd71SBarry Smith dm->ops->globaltolocalend = NULL; 48020298fd71SBarry Smith dm->ops->localtoglobalbegin = NULL; 48030298fd71SBarry Smith dm->ops->localtoglobalend = NULL; 4804552f7358SJed Brown dm->ops->destroy = DMDestroy_Plex; 4805552f7358SJed Brown dm->ops->createsubdm = DMCreateSubDM_Plex; 48062adcc780SMatthew G. Knepley dm->ops->createsuperdm = DMCreateSuperDM_Plex; 4807793f3fe5SMatthew G. Knepley dm->ops->getdimpoints = DMGetDimPoints_Plex; 4808552f7358SJed Brown dm->ops->locatepoints = DMLocatePoints_Plex; 48090709b2feSToby Isaac dm->ops->projectfunctionlocal = DMProjectFunctionLocal_Plex; 48100709b2feSToby Isaac dm->ops->projectfunctionlabellocal = DMProjectFunctionLabelLocal_Plex; 4811bfc4295aSToby Isaac dm->ops->projectfieldlocal = DMProjectFieldLocal_Plex; 48128c6c5593SMatthew G. Knepley dm->ops->projectfieldlabellocal = DMProjectFieldLabelLocal_Plex; 4813ece3a9fcSMatthew G. Knepley dm->ops->projectbdfieldlabellocal = DMProjectBdFieldLabelLocal_Plex; 48140709b2feSToby Isaac dm->ops->computel2diff = DMComputeL2Diff_Plex; 4815b698f381SToby Isaac dm->ops->computel2gradientdiff = DMComputeL2GradientDiff_Plex; 48162a16baeaSToby Isaac dm->ops->computel2fielddiff = DMComputeL2FieldDiff_Plex; 481728d58a37SPierre Jolivet dm->ops->getneighbors = DMGetNeighbors_Plex; 48186c6a6b79SMatthew G. Knepley dm->ops->getlocalboundingbox = DMGetLocalBoundingBox_Coordinates; 4819907a3e9cSStefano Zampini dm->ops->createdomaindecomposition = DMCreateDomainDecomposition_Plex; 4820907a3e9cSStefano Zampini dm->ops->createddscatters = DMCreateDomainDecompositionScatters_Plex; 48219566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_Plex)); 48226c51210dSStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerivativeBoundaryValues_C", DMPlexInsertTimeDerivativeBoundaryValues_Plex)); 48239566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", DMSetUpGLVisViewer_Plex)); 48249566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", DMCreateNeumannOverlap_Plex)); 48259566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", DMPlexDistributeGetDefault_Plex)); 48269566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", DMPlexDistributeSetDefault_Plex)); 48276bc1bd01Sksagiyam PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", DMPlexReorderGetDefault_Plex)); 48286bc1bd01Sksagiyam PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", DMPlexReorderSetDefault_Plex)); 4829adc21957SMatthew G. Knepley PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetDefault_C", DMReorderSectionGetDefault_Plex)); 4830adc21957SMatthew G. Knepley PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetDefault_C", DMReorderSectionSetDefault_Plex)); 4831adc21957SMatthew G. Knepley PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetType_C", DMReorderSectionGetType_Plex)); 4832adc21957SMatthew G. Knepley PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetType_C", DMReorderSectionSetType_Plex)); 48339566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", DMInterpolateSolution_Plex)); 4834c506a872SMatthew G. Knepley PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex)); 4835c506a872SMatthew G. Knepley PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", DMPlexSetOverlap_Plex)); 4836d2b2dc1eSMatthew G. Knepley PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetUseCeed_C", DMPlexGetUseCeed_Plex)); 4837d2b2dc1eSMatthew G. Knepley PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetUseCeed_C", DMPlexSetUseCeed_Plex)); 48383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4839552f7358SJed Brown } 4840552f7358SJed Brown 4841d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm) 4842d71ae5a4SJacob Faibussowitsch { 484363a16f15SMatthew G. Knepley DM_Plex *mesh = (DM_Plex *)dm->data; 48441fca310dSJames Wright const PetscSF *face_sfs; 48451fca310dSJames Wright PetscInt num_face_sfs; 484663a16f15SMatthew G. Knepley 484763a16f15SMatthew G. Knepley PetscFunctionBegin; 484863a16f15SMatthew G. Knepley mesh->refct++; 484963a16f15SMatthew G. Knepley (*newdm)->data = mesh; 48501fca310dSJames Wright PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, &face_sfs)); 48511fca310dSJames Wright PetscCall(DMPlexSetIsoperiodicFaceSF(*newdm, num_face_sfs, (PetscSF *)face_sfs)); 48529566063dSJacob Faibussowitsch PetscCall(PetscObjectChangeTypeName((PetscObject)*newdm, DMPLEX)); 48539566063dSJacob Faibussowitsch PetscCall(DMInitialize_Plex(*newdm)); 48543ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 485563a16f15SMatthew G. Knepley } 485663a16f15SMatthew G. Knepley 48578818961aSMatthew G Knepley /*MC 4858a1cb98faSBarry Smith DMPLEX = "plex" - A `DM` object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram. 485920f4b53cSBarry Smith In the local representation, `Vec`s contain all unknowns in the interior and shared boundary. This is 48608818961aSMatthew G Knepley specified by a PetscSection object. Ownership in the global representation is determined by 4861a1cb98faSBarry Smith ownership of the underlying `DMPLEX` points. This is specified by another `PetscSection` object. 48628818961aSMatthew G Knepley 4863e5893cccSMatthew G. Knepley Options Database Keys: 4864250712c9SMatthew G. Knepley + -dm_refine_pre - Refine mesh before distribution 4865250712c9SMatthew G. Knepley + -dm_refine_uniform_pre - Choose uniform or generator-based refinement 4866250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre - Cell volume limit after pre-refinement using generator 4867250712c9SMatthew G. Knepley . -dm_distribute - Distribute mesh across processes 4868250712c9SMatthew G. Knepley . -dm_distribute_overlap - Number of cells to overlap for distribution 4869250712c9SMatthew G. Knepley . -dm_refine - Refine mesh after distribution 4870c3db174cSMatthew G. Knepley . -dm_localize <bool> - Whether to localize coordinates for periodic meshes 4871c3db174cSMatthew G. Knepley . -dm_sparse_localize <bool> - Whether to only localize cells on the periodic boundary 4872250712c9SMatthew G. Knepley . -dm_plex_hash_location - Use grid hashing for point location 4873ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p> - The number of divisions in each direction of the grid hash 4874f12cf164SMatthew G. Knepley . -dm_plex_partition_balance - Attempt to evenly divide points on partition boundary between processes 4875f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd - Allow changes to the boundary on remeshing 4876d5b43468SJose E. Roman . -dm_plex_max_projection_height - Maximum mesh point height used to project locally 4877f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement - Use special nested projection algorithm for regular refinement 4878d02c7345SMatthew G. Knepley . -dm_plex_reorder_section - Use specialized blocking if available 4879aaa8cc7dSPierre Jolivet . -dm_plex_check_all - Perform all checks below 4880f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry - Check that the adjacency information in the mesh is symmetric 4881f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices 4882f12cf164SMatthew 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 4883f12cf164SMatthew G. Knepley . -dm_plex_check_geometry - Check that cells have positive volume 4884f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex - View the mesh in LaTeX/TikZ 4885e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num> - Scale the TikZ 48865962854dSMatthew G. Knepley . -dm_plex_print_fem <num> - View FEM assembly information, such as element vectors and matrices 48875962854dSMatthew G. Knepley - -dm_plex_print_fvm <num> - View FVM assembly information, such as flux updates 4888e5893cccSMatthew G. Knepley 48898818961aSMatthew G Knepley Level: intermediate 48908818961aSMatthew G Knepley 48911cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`, `PetscSection` 48928818961aSMatthew G Knepley M*/ 48938818961aSMatthew G Knepley 4894d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm) 4895d71ae5a4SJacob Faibussowitsch { 4896552f7358SJed Brown DM_Plex *mesh; 4897412e9a14SMatthew G. Knepley PetscInt unit; 4898552f7358SJed Brown 4899552f7358SJed Brown PetscFunctionBegin; 4900f39ec787SMatthew G. Knepley PetscCall(PetscCitationsRegister(PlexCitation, &Plexcite)); 4901552f7358SJed Brown PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 49024dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&mesh)); 4903adc21957SMatthew G. Knepley dm->reorderSection = DM_REORDER_DEFAULT_NOTSET; 4904552f7358SJed Brown dm->data = mesh; 4905552f7358SJed Brown 4906552f7358SJed Brown mesh->refct = 1; 49079566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection)); 49089566063dSJacob Faibussowitsch PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection)); 4909552f7358SJed Brown mesh->refinementUniform = PETSC_TRUE; 4910552f7358SJed Brown mesh->refinementLimit = -1.0; 4911e600fa54SMatthew G. Knepley mesh->distDefault = PETSC_TRUE; 4912adc21957SMatthew G. Knepley mesh->reorderDefault = DM_REORDER_DEFAULT_NOTSET; 49131d1f2f2aSksagiyam mesh->distributionName = NULL; 49147d0f5628SVaclav Hapla mesh->interpolated = DMPLEX_INTERPOLATED_INVALID; 49157d0f5628SVaclav Hapla mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID; 4916552f7358SJed Brown 49179566063dSJacob Faibussowitsch PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner)); 49182e62ab5aSMatthew G. Knepley mesh->remeshBd = PETSC_FALSE; 4919d9deefdfSMatthew G. Knepley 49208865f1eaSKarl Rupp for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0; 4921552f7358SJed Brown 4922df0420ecSMatthew G. Knepley mesh->depthState = -1; 4923ba2698f1SMatthew G. Knepley mesh->celltypeState = -1; 49246113b454SMatthew G. Knepley mesh->printTol = 1.0e-10; 4925552f7358SJed Brown 49269566063dSJacob Faibussowitsch PetscCall(DMInitialize_Plex(dm)); 49273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4928552f7358SJed Brown } 4929552f7358SJed Brown 4930552f7358SJed Brown /*@ 4931a1cb98faSBarry Smith DMPlexCreate - Creates a `DMPLEX` object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram. 4932552f7358SJed Brown 4933d083f849SBarry Smith Collective 4934552f7358SJed Brown 4935552f7358SJed Brown Input Parameter: 4936a1cb98faSBarry Smith . comm - The communicator for the `DMPLEX` object 4937552f7358SJed Brown 4938552f7358SJed Brown Output Parameter: 4939a1cb98faSBarry Smith . mesh - The `DMPLEX` object 4940552f7358SJed Brown 4941552f7358SJed Brown Level: beginner 4942552f7358SJed Brown 494342747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMCreate()`, `DMSetType()` 4944552f7358SJed Brown @*/ 4945d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh) 4946d71ae5a4SJacob Faibussowitsch { 4947552f7358SJed Brown PetscFunctionBegin; 49484f572ea9SToby Isaac PetscAssertPointer(mesh, 2); 49499566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, mesh)); 49509566063dSJacob Faibussowitsch PetscCall(DMSetType(*mesh, DMPLEX)); 49513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4952552f7358SJed Brown } 4953552f7358SJed Brown 4954b09969d6SVaclav Hapla /*@C 4955a1cb98faSBarry Smith DMPlexBuildFromCellListParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (common mesh generator output) 4956a1cb98faSBarry Smith 495720f4b53cSBarry Smith Collective; No Fortran Support 4958b09969d6SVaclav Hapla 4959b09969d6SVaclav Hapla Input Parameters: 4960a1cb98faSBarry Smith + dm - The `DM` 4961b09969d6SVaclav Hapla . numCells - The number of cells owned by this process 4962a1cb98faSBarry Smith . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE` 4963a1cb98faSBarry Smith . NVertices - The global number of vertices, or `PETSC_DETERMINE` 4964b09969d6SVaclav Hapla . numCorners - The number of vertices for each cell 49655e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell 4966b09969d6SVaclav Hapla 4967be8c289dSNicolas Barral Output Parameters: 4968a1cb98faSBarry Smith + vertexSF - (Optional) `PetscSF` describing complete vertex ownership 4969be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array 4970b09969d6SVaclav Hapla 4971b09969d6SVaclav Hapla Level: advanced 4972b09969d6SVaclav Hapla 4973a1cb98faSBarry Smith Notes: 4974a1cb98faSBarry Smith Two triangles sharing a face 4975a1cb98faSBarry Smith .vb 4976a1cb98faSBarry Smith 4977a1cb98faSBarry Smith 2 4978a1cb98faSBarry Smith / | \ 4979a1cb98faSBarry Smith / | \ 4980a1cb98faSBarry Smith / | \ 4981a1cb98faSBarry Smith 0 0 | 1 3 4982a1cb98faSBarry Smith \ | / 4983a1cb98faSBarry Smith \ | / 4984a1cb98faSBarry Smith \ | / 4985a1cb98faSBarry Smith 1 4986a1cb98faSBarry Smith .ve 4987a1cb98faSBarry Smith would have input 4988a1cb98faSBarry Smith .vb 4989a1cb98faSBarry Smith numCells = 2, numVertices = 4 4990a1cb98faSBarry Smith cells = [0 1 2 1 3 2] 4991a1cb98faSBarry Smith .ve 4992a1cb98faSBarry Smith which would result in the `DMPLEX` 4993a1cb98faSBarry Smith .vb 4994a1cb98faSBarry Smith 4995a1cb98faSBarry Smith 4 4996a1cb98faSBarry Smith / | \ 4997a1cb98faSBarry Smith / | \ 4998a1cb98faSBarry Smith / | \ 4999a1cb98faSBarry Smith 2 0 | 1 5 5000a1cb98faSBarry Smith \ | / 5001a1cb98faSBarry Smith \ | / 5002a1cb98faSBarry Smith \ | / 5003a1cb98faSBarry Smith 3 5004a1cb98faSBarry Smith .ve 5005a1cb98faSBarry Smith 5006a1cb98faSBarry Smith Vertices are implicitly numbered consecutively 0,...,NVertices. 5007a1cb98faSBarry Smith Each rank owns a chunk of numVertices consecutive vertices. 5008a1cb98faSBarry Smith If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout. 5009a1cb98faSBarry Smith If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1. 5010a1cb98faSBarry Smith If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks. 5011a1cb98faSBarry Smith 5012a1cb98faSBarry Smith The cell distribution is arbitrary non-overlapping, independent of the vertex distribution. 5013a1cb98faSBarry Smith 50141cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`, 5015a1cb98faSBarry Smith `PetscSF` 5016b09969d6SVaclav Hapla @*/ 5017d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved) 5018d71ae5a4SJacob Faibussowitsch { 50192464107aSksagiyam PetscSF sfPoint; 50202464107aSksagiyam PetscLayout layout; 502182fb893eSVaclav Hapla PetscInt numVerticesAdj, *verticesAdj, *cones, c, p; 5022a47d0d45SMatthew G. Knepley 5023a47d0d45SMatthew G. Knepley PetscFunctionBegin; 502425b6865aSVaclav Hapla PetscValidLogicalCollectiveInt(dm, NVertices, 4); 50259566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0)); 502625b6865aSVaclav Hapla /* Get/check global number of vertices */ 502725b6865aSVaclav Hapla { 502825b6865aSVaclav Hapla PetscInt NVerticesInCells, i; 502925b6865aSVaclav Hapla const PetscInt len = numCells * numCorners; 503025b6865aSVaclav Hapla 503125b6865aSVaclav Hapla /* NVerticesInCells = max(cells) + 1 */ 503225b6865aSVaclav Hapla NVerticesInCells = PETSC_MIN_INT; 50339371c9d4SSatish Balay for (i = 0; i < len; i++) 50349371c9d4SSatish Balay if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i]; 503525b6865aSVaclav Hapla ++NVerticesInCells; 5036712fec58SPierre Jolivet PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm))); 503725b6865aSVaclav Hapla 503825b6865aSVaclav Hapla if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells; 50399371c9d4SSatish Balay else 50409371c9d4SSatish Balay PetscCheck(NVertices == PETSC_DECIDE || NVertices >= NVerticesInCells, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Specified global number of vertices %" PetscInt_FMT " must be greater than or equal to the number of vertices in cells %" PetscInt_FMT, NVertices, NVerticesInCells); 504125b6865aSVaclav Hapla } 50429079aca8SVaclav Hapla /* Count locally unique vertices */ 50439079aca8SVaclav Hapla { 50449079aca8SVaclav Hapla PetscHSetI vhash; 50459079aca8SVaclav Hapla PetscInt off = 0; 50469079aca8SVaclav Hapla 50479566063dSJacob Faibussowitsch PetscCall(PetscHSetICreate(&vhash)); 5048a47d0d45SMatthew G. Knepley for (c = 0; c < numCells; ++c) { 504948a46eb9SPierre Jolivet for (p = 0; p < numCorners; ++p) PetscCall(PetscHSetIAdd(vhash, cells[c * numCorners + p])); 5050a47d0d45SMatthew G. Knepley } 50519566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj)); 50529566063dSJacob Faibussowitsch if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj)); 5053ad540459SPierre Jolivet else verticesAdj = *verticesAdjSaved; 50549566063dSJacob Faibussowitsch PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj)); 50559566063dSJacob Faibussowitsch PetscCall(PetscHSetIDestroy(&vhash)); 505663a3b9bcSJacob Faibussowitsch PetscCheck(off == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj); 5057a47d0d45SMatthew G. Knepley } 50589566063dSJacob Faibussowitsch PetscCall(PetscSortInt(numVerticesAdj, verticesAdj)); 5059a47d0d45SMatthew G. Knepley /* Create cones */ 50609566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj)); 50619566063dSJacob Faibussowitsch for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners)); 50629566063dSJacob Faibussowitsch PetscCall(DMSetUp(dm)); 50639566063dSJacob Faibussowitsch PetscCall(DMPlexGetCones(dm, &cones)); 5064a47d0d45SMatthew G. Knepley for (c = 0; c < numCells; ++c) { 5065a47d0d45SMatthew G. Knepley for (p = 0; p < numCorners; ++p) { 5066a47d0d45SMatthew G. Knepley const PetscInt gv = cells[c * numCorners + p]; 5067a47d0d45SMatthew G. Knepley PetscInt lv; 5068a47d0d45SMatthew G. Knepley 50699079aca8SVaclav Hapla /* Positions within verticesAdj form 0-based local vertex numbering; 50709079aca8SVaclav Hapla we need to shift it by numCells to get correct DAG points (cells go first) */ 50719566063dSJacob Faibussowitsch PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv)); 507263a3b9bcSJacob Faibussowitsch PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv); 5073961cfab0SVaclav Hapla cones[c * numCorners + p] = lv + numCells; 5074a47d0d45SMatthew G. Knepley } 5075a47d0d45SMatthew G. Knepley } 50762464107aSksagiyam /* Build point sf */ 50779566063dSJacob Faibussowitsch PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout)); 50789566063dSJacob Faibussowitsch PetscCall(PetscLayoutSetSize(layout, NVertices)); 50799566063dSJacob Faibussowitsch PetscCall(PetscLayoutSetLocalSize(layout, numVertices)); 50809566063dSJacob Faibussowitsch PetscCall(PetscLayoutSetBlockSize(layout, 1)); 50819566063dSJacob Faibussowitsch PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint)); 50829566063dSJacob Faibussowitsch PetscCall(PetscLayoutDestroy(&layout)); 50839566063dSJacob Faibussowitsch if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj)); 50849566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF")); 50852464107aSksagiyam if (dm->sf) { 50862464107aSksagiyam const char *prefix; 50872464107aSksagiyam 50889566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix)); 50899566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix)); 50902464107aSksagiyam } 50919566063dSJacob Faibussowitsch PetscCall(DMSetPointSF(dm, sfPoint)); 50929566063dSJacob Faibussowitsch PetscCall(PetscSFDestroy(&sfPoint)); 5093f4f49eeaSPierre Jolivet if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)*vertexSF, "Vertex Ownership SF")); 5094a47d0d45SMatthew G. Knepley /* Fill in the rest of the topology structure */ 50959566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dm)); 50969566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dm)); 50979566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0)); 50983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5099a47d0d45SMatthew G. Knepley } 5100a47d0d45SMatthew G. Knepley 5101cc4c1da9SBarry Smith /*@ 5102a1cb98faSBarry Smith DMPlexBuildCoordinatesFromCellListParallel - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output) 5103a1cb98faSBarry Smith 510420f4b53cSBarry Smith Collective; No Fortran Support 5105b09969d6SVaclav Hapla 5106b09969d6SVaclav Hapla Input Parameters: 5107a1cb98faSBarry Smith + dm - The `DM` 5108b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates 5109a1cb98faSBarry Smith . sfVert - `PetscSF` describing complete vertex ownership 5110b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex 5111b09969d6SVaclav Hapla 5112b09969d6SVaclav Hapla Level: advanced 5113b09969d6SVaclav Hapla 51141cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()` 5115b09969d6SVaclav Hapla @*/ 5116d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[]) 5117d71ae5a4SJacob Faibussowitsch { 5118a47d0d45SMatthew G. Knepley PetscSection coordSection; 5119a47d0d45SMatthew G. Knepley Vec coordinates; 5120a47d0d45SMatthew G. Knepley PetscScalar *coords; 51211edcf0b2SVaclav Hapla PetscInt numVertices, numVerticesAdj, coordSize, v, vStart, vEnd; 5122a47d0d45SMatthew G. Knepley 5123a47d0d45SMatthew G. Knepley PetscFunctionBegin; 51249566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0)); 51259566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 51261dca8a05SBarry Smith PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first."); 51279566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(dm, spaceDim)); 51289566063dSJacob Faibussowitsch PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL)); 51291dca8a05SBarry Smith PetscCheck(vEnd - vStart == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Supplied sfVert has wrong number of leaves = %" PetscInt_FMT " != %" PetscInt_FMT " = vEnd - vStart", numVerticesAdj, vEnd - vStart); 51309566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 51319566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 51329566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim)); 51339566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd)); 51341edcf0b2SVaclav Hapla for (v = vStart; v < vEnd; ++v) { 51359566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, spaceDim)); 51369566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim)); 5137a47d0d45SMatthew G. Knepley } 51389566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 51399566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 51409566063dSJacob Faibussowitsch PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates)); 51419566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(coordinates, spaceDim)); 51429566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 51439566063dSJacob Faibussowitsch PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 51449566063dSJacob Faibussowitsch PetscCall(VecSetType(coordinates, VECSTANDARD)); 51459566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 5146a47d0d45SMatthew G. Knepley { 5147a47d0d45SMatthew G. Knepley MPI_Datatype coordtype; 5148a47d0d45SMatthew G. Knepley 5149a47d0d45SMatthew G. Knepley /* Need a temp buffer for coords if we have complex/single */ 51509566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype)); 51519566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_commit(&coordtype)); 515221016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX) 515321016a8bSBarry Smith { 515421016a8bSBarry Smith PetscScalar *svertexCoords; 515521016a8bSBarry Smith PetscInt i; 51569566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(numVertices * spaceDim, &svertexCoords)); 51573612f820SVaclav Hapla for (i = 0; i < numVertices * spaceDim; i++) svertexCoords[i] = vertexCoords[i]; 51589566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE)); 51599566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE)); 51609566063dSJacob Faibussowitsch PetscCall(PetscFree(svertexCoords)); 516121016a8bSBarry Smith } 516221016a8bSBarry Smith #else 51639566063dSJacob Faibussowitsch PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE)); 51649566063dSJacob Faibussowitsch PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE)); 516521016a8bSBarry Smith #endif 51669566063dSJacob Faibussowitsch PetscCallMPI(MPI_Type_free(&coordtype)); 5167a47d0d45SMatthew G. Knepley } 51689566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 51699566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, coordinates)); 51709566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 51719566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0)); 51723ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5173a47d0d45SMatthew G. Knepley } 5174a47d0d45SMatthew G. Knepley 5175c3edce3dSSatish Balay /*@ 5176a1cb98faSBarry Smith DMPlexCreateFromCellListParallelPetsc - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output) 5177a1cb98faSBarry Smith 5178a1cb98faSBarry Smith Collective 5179a47d0d45SMatthew G. Knepley 5180a47d0d45SMatthew G. Knepley Input Parameters: 5181a47d0d45SMatthew G. Knepley + comm - The communicator 5182a47d0d45SMatthew G. Knepley . dim - The topological dimension of the mesh 5183a47d0d45SMatthew G. Knepley . numCells - The number of cells owned by this process 5184a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DECIDE` 5185a1cb98faSBarry Smith . NVertices - The global number of vertices, or `PETSC_DECIDE` 5186a47d0d45SMatthew G. Knepley . numCorners - The number of vertices for each cell 5187a47d0d45SMatthew G. Knepley . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically 5188a47d0d45SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell 5189a47d0d45SMatthew G. Knepley . spaceDim - The spatial dimension used for coordinates 5190a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex 5191a47d0d45SMatthew G. Knepley 5192d8d19677SJose E. Roman Output Parameters: 5193a1cb98faSBarry Smith + dm - The `DM` 5194a1cb98faSBarry Smith . vertexSF - (Optional) `PetscSF` describing complete vertex ownership 519560225df5SJacob Faibussowitsch - verticesAdj - (Optional) vertex adjacency array 5196a47d0d45SMatthew G. Knepley 5197b09969d6SVaclav Hapla Level: intermediate 5198a47d0d45SMatthew G. Knepley 5199a1cb98faSBarry Smith Notes: 5200a1cb98faSBarry Smith This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, 5201a1cb98faSBarry Smith `DMPlexBuildFromCellListParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()` 5202a1cb98faSBarry Smith 5203a1cb98faSBarry Smith See `DMPlexBuildFromCellListParallel()` for an example and details about the topology-related parameters. 5204a1cb98faSBarry Smith 5205a1cb98faSBarry Smith See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters. 5206a1cb98faSBarry Smith 52071cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()` 5208a47d0d45SMatthew G. Knepley @*/ 5209d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromCellListParallelPetsc(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, PetscBool interpolate, const PetscInt cells[], PetscInt spaceDim, const PetscReal vertexCoords[], PetscSF *vertexSF, PetscInt **verticesAdj, DM *dm) 5210d71ae5a4SJacob Faibussowitsch { 5211a47d0d45SMatthew G. Knepley PetscSF sfVert; 5212a47d0d45SMatthew G. Knepley 5213a47d0d45SMatthew G. Knepley PetscFunctionBegin; 52149566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 52159566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 5216a47d0d45SMatthew G. Knepley PetscValidLogicalCollectiveInt(*dm, dim, 2); 5217064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveInt(*dm, spaceDim, 9); 52189566063dSJacob Faibussowitsch PetscCall(DMSetDimension(*dm, dim)); 52199566063dSJacob Faibussowitsch PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj)); 5220a47d0d45SMatthew G. Knepley if (interpolate) { 52215fd9971aSMatthew G. Knepley DM idm; 5222a47d0d45SMatthew G. Knepley 52239566063dSJacob Faibussowitsch PetscCall(DMPlexInterpolate(*dm, &idm)); 52249566063dSJacob Faibussowitsch PetscCall(DMDestroy(dm)); 5225a47d0d45SMatthew G. Knepley *dm = idm; 5226a47d0d45SMatthew G. Knepley } 52279566063dSJacob Faibussowitsch PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords)); 522818d54ad4SMichael Lange if (vertexSF) *vertexSF = sfVert; 52299566063dSJacob Faibussowitsch else PetscCall(PetscSFDestroy(&sfVert)); 52303ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5231a47d0d45SMatthew G. Knepley } 5232a47d0d45SMatthew G. Knepley 5233cc4c1da9SBarry Smith /*@ 5234a1cb98faSBarry Smith DMPlexBuildFromCellList - Build `DMPLEX` topology from a list of vertices for each cell (common mesh generator output) 5235a1cb98faSBarry Smith 523620f4b53cSBarry Smith Collective; No Fortran Support 52379298eaa6SMatthew G Knepley 52389298eaa6SMatthew G Knepley Input Parameters: 5239a1cb98faSBarry Smith + dm - The `DM` 5240b09969d6SVaclav Hapla . numCells - The number of cells owned by this process 5241a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DETERMINE` 52429298eaa6SMatthew G Knepley . numCorners - The number of vertices for each cell 5243a3b724e8SBarry Smith - cells - An array of `numCells` x `numCorners` numbers, the global vertex numbers for each cell 52449298eaa6SMatthew G Knepley 5245b09969d6SVaclav Hapla Level: advanced 52469298eaa6SMatthew G Knepley 5247b09969d6SVaclav Hapla Notes: 5248b09969d6SVaclav Hapla Two triangles sharing a face 5249a1cb98faSBarry Smith .vb 52509298eaa6SMatthew G Knepley 5251a1cb98faSBarry Smith 2 5252a1cb98faSBarry Smith / | \ 5253a1cb98faSBarry Smith / | \ 5254a1cb98faSBarry Smith / | \ 5255a1cb98faSBarry Smith 0 0 | 1 3 5256a1cb98faSBarry Smith \ | / 5257a1cb98faSBarry Smith \ | / 5258a1cb98faSBarry Smith \ | / 5259a1cb98faSBarry Smith 1 5260a1cb98faSBarry Smith .ve 5261a1cb98faSBarry Smith would have input 5262a1cb98faSBarry Smith .vb 5263a1cb98faSBarry Smith numCells = 2, numVertices = 4 5264a1cb98faSBarry Smith cells = [0 1 2 1 3 2] 5265a1cb98faSBarry Smith .ve 5266a1cb98faSBarry Smith which would result in the `DMPLEX` 5267a1cb98faSBarry Smith .vb 5268a1cb98faSBarry Smith 5269a1cb98faSBarry Smith 4 5270a1cb98faSBarry Smith / | \ 5271a1cb98faSBarry Smith / | \ 5272a1cb98faSBarry Smith / | \ 5273a1cb98faSBarry Smith 2 0 | 1 5 5274a1cb98faSBarry Smith \ | / 5275a1cb98faSBarry Smith \ | / 5276a1cb98faSBarry Smith \ | / 5277a1cb98faSBarry Smith 3 5278a1cb98faSBarry Smith .ve 5279a1cb98faSBarry Smith 5280a1cb98faSBarry Smith If numVertices is `PETSC_DETERMINE`, it is computed by PETSc as the maximum vertex index in cells + 1. 528125b6865aSVaclav Hapla 52821cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()` 5283b09969d6SVaclav Hapla @*/ 5284d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[]) 5285d71ae5a4SJacob Faibussowitsch { 5286961cfab0SVaclav Hapla PetscInt *cones, c, p, dim; 5287b09969d6SVaclav Hapla 5288b09969d6SVaclav Hapla PetscFunctionBegin; 52899566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0)); 52909566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 529125b6865aSVaclav Hapla /* Get/check global number of vertices */ 529225b6865aSVaclav Hapla { 529325b6865aSVaclav Hapla PetscInt NVerticesInCells, i; 529425b6865aSVaclav Hapla const PetscInt len = numCells * numCorners; 529525b6865aSVaclav Hapla 529625b6865aSVaclav Hapla /* NVerticesInCells = max(cells) + 1 */ 529725b6865aSVaclav Hapla NVerticesInCells = PETSC_MIN_INT; 52989371c9d4SSatish Balay for (i = 0; i < len; i++) 52999371c9d4SSatish Balay if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i]; 530025b6865aSVaclav Hapla ++NVerticesInCells; 530125b6865aSVaclav Hapla 530225b6865aSVaclav Hapla if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells; 53039371c9d4SSatish Balay else 53049371c9d4SSatish Balay PetscCheck(numVertices >= NVerticesInCells, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Specified number of vertices %" PetscInt_FMT " must be greater than or equal to the number of vertices in cells %" PetscInt_FMT, numVertices, NVerticesInCells); 530525b6865aSVaclav Hapla } 53069566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices)); 530748a46eb9SPierre Jolivet for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners)); 53089566063dSJacob Faibussowitsch PetscCall(DMSetUp(dm)); 53099566063dSJacob Faibussowitsch PetscCall(DMPlexGetCones(dm, &cones)); 5310b09969d6SVaclav Hapla for (c = 0; c < numCells; ++c) { 5311ad540459SPierre Jolivet for (p = 0; p < numCorners; ++p) cones[c * numCorners + p] = cells[c * numCorners + p] + numCells; 5312b09969d6SVaclav Hapla } 53139566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dm)); 53149566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dm)); 53159566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0)); 53163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5317b09969d6SVaclav Hapla } 5318b09969d6SVaclav Hapla 5319cc4c1da9SBarry Smith /*@ 5320a1cb98faSBarry Smith DMPlexBuildCoordinatesFromCellList - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output) 5321a1cb98faSBarry Smith 5322cc4c1da9SBarry Smith Collective 5323b09969d6SVaclav Hapla 5324b09969d6SVaclav Hapla Input Parameters: 5325a1cb98faSBarry Smith + dm - The `DM` 5326b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates 5327b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex 5328b09969d6SVaclav Hapla 5329b09969d6SVaclav Hapla Level: advanced 5330b09969d6SVaclav Hapla 53311cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()` 5332b09969d6SVaclav Hapla @*/ 5333d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[]) 5334d71ae5a4SJacob Faibussowitsch { 5335b09969d6SVaclav Hapla PetscSection coordSection; 5336b09969d6SVaclav Hapla Vec coordinates; 5337b09969d6SVaclav Hapla DM cdm; 5338b09969d6SVaclav Hapla PetscScalar *coords; 53391edcf0b2SVaclav Hapla PetscInt v, vStart, vEnd, d; 5340b09969d6SVaclav Hapla 5341b09969d6SVaclav Hapla PetscFunctionBegin; 53429566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0)); 53439566063dSJacob Faibussowitsch PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd)); 53441dca8a05SBarry Smith PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first."); 53459566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(dm, spaceDim)); 53469566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 53479566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 53489566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim)); 53499566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd)); 53501edcf0b2SVaclav Hapla for (v = vStart; v < vEnd; ++v) { 53519566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, spaceDim)); 53529566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim)); 5353b09969d6SVaclav Hapla } 53549566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 5355b09969d6SVaclav Hapla 53569566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDM(dm, &cdm)); 53579566063dSJacob Faibussowitsch PetscCall(DMCreateLocalVector(cdm, &coordinates)); 53589566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(coordinates, spaceDim)); 53599566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 53609566063dSJacob Faibussowitsch PetscCall(VecGetArrayWrite(coordinates, &coords)); 53611edcf0b2SVaclav Hapla for (v = 0; v < vEnd - vStart; ++v) { 5362ad540459SPierre Jolivet for (d = 0; d < spaceDim; ++d) coords[v * spaceDim + d] = vertexCoords[v * spaceDim + d]; 5363b09969d6SVaclav Hapla } 53649566063dSJacob Faibussowitsch PetscCall(VecRestoreArrayWrite(coordinates, &coords)); 53659566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, coordinates)); 53669566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 53679566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0)); 53683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5369b09969d6SVaclav Hapla } 5370b09969d6SVaclav Hapla 5371b09969d6SVaclav Hapla /*@ 5372a1cb98faSBarry Smith DMPlexCreateFromCellListPetsc - Create `DMPLEX` from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input 53733df08285SMatthew G. Knepley 5374a1cb98faSBarry Smith Collective 5375b09969d6SVaclav Hapla 5376b09969d6SVaclav Hapla Input Parameters: 5377b09969d6SVaclav Hapla + comm - The communicator 5378b09969d6SVaclav Hapla . dim - The topological dimension of the mesh 53793df08285SMatthew G. Knepley . numCells - The number of cells, only on process 0 5380a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DECIDE`, only on process 0 53813df08285SMatthew G. Knepley . numCorners - The number of vertices for each cell, only on process 0 5382b09969d6SVaclav Hapla . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically 53833df08285SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0 5384b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates 53853df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0 5386b09969d6SVaclav Hapla 5387b09969d6SVaclav Hapla Output Parameter: 5388a1cb98faSBarry Smith . dm - The `DM`, which only has points on process 0 538925b6865aSVaclav Hapla 5390b09969d6SVaclav Hapla Level: intermediate 5391b09969d6SVaclav Hapla 5392a1cb98faSBarry Smith Notes: 5393a1cb98faSBarry Smith This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, `DMPlexBuildFromCellList()`, 5394a1cb98faSBarry Smith `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellList()` 5395a1cb98faSBarry Smith 5396a1cb98faSBarry Smith See `DMPlexBuildFromCellList()` for an example and details about the topology-related parameters. 5397a1cb98faSBarry Smith See `DMPlexBuildCoordinatesFromCellList()` for details about the geometry-related parameters. 5398a1cb98faSBarry Smith See `DMPlexCreateFromCellListParallelPetsc()` for parallel input 5399a1cb98faSBarry Smith 54001cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()` 54019298eaa6SMatthew G Knepley @*/ 5402d71ae5a4SJacob 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) 5403d71ae5a4SJacob Faibussowitsch { 54043df08285SMatthew G. Knepley PetscMPIInt rank; 54059298eaa6SMatthew G Knepley 54069298eaa6SMatthew G Knepley PetscFunctionBegin; 540728b400f6SJacob 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."); 54089566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 54099566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 54109566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 54119566063dSJacob Faibussowitsch PetscCall(DMSetDimension(*dm, dim)); 5412c5853193SPierre Jolivet if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells)); 54139566063dSJacob Faibussowitsch else PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL)); 54149298eaa6SMatthew G Knepley if (interpolate) { 54155fd9971aSMatthew G. Knepley DM idm; 54169298eaa6SMatthew G Knepley 54179566063dSJacob Faibussowitsch PetscCall(DMPlexInterpolate(*dm, &idm)); 54189566063dSJacob Faibussowitsch PetscCall(DMDestroy(dm)); 54199298eaa6SMatthew G Knepley *dm = idm; 54209298eaa6SMatthew G Knepley } 5421c5853193SPierre Jolivet if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords)); 54229566063dSJacob Faibussowitsch else PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL)); 54233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 54249298eaa6SMatthew G Knepley } 54259298eaa6SMatthew G Knepley 5426939f6067SMatthew G. Knepley /*@ 542720f4b53cSBarry Smith DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a `DM` 5428939f6067SMatthew G. Knepley 5429939f6067SMatthew G. Knepley Input Parameters: 543020f4b53cSBarry Smith + dm - The empty `DM` object, usually from `DMCreate()` and `DMSetDimension()` 5431939f6067SMatthew G. Knepley . depth - The depth of the DAG 543220f4b53cSBarry Smith . numPoints - Array of size depth + 1 containing the number of points at each `depth` 5433939f6067SMatthew G. Knepley . coneSize - The cone size of each point 5434939f6067SMatthew G. Knepley . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point 5435939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point 543620f4b53cSBarry Smith - vertexCoords - An array of `numPoints`[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via `DMSetCoordinateDim()` 5437939f6067SMatthew G. Knepley 5438939f6067SMatthew G. Knepley Output Parameter: 543920f4b53cSBarry Smith . dm - The `DM` 544020f4b53cSBarry Smith 544120f4b53cSBarry Smith Level: advanced 5442939f6067SMatthew G. Knepley 5443a1cb98faSBarry Smith Note: 5444a1cb98faSBarry Smith Two triangles sharing a face would have input 5445a1cb98faSBarry Smith .vb 5446a1cb98faSBarry Smith depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0] 5447a1cb98faSBarry Smith cones = [2 3 4 3 5 4], coneOrientations = [0 0 0 0 0 0] 5448a1cb98faSBarry Smith vertexCoords = [-1.0 0.0 0.0 -1.0 0.0 1.0 1.0 0.0] 5449a1cb98faSBarry Smith .ve 5450939f6067SMatthew G. Knepley which would result in the DMPlex 5451a1cb98faSBarry Smith .vb 5452a1cb98faSBarry Smith 4 5453a1cb98faSBarry Smith / | \ 5454a1cb98faSBarry Smith / | \ 5455a1cb98faSBarry Smith / | \ 5456a1cb98faSBarry Smith 2 0 | 1 5 5457a1cb98faSBarry Smith \ | / 5458a1cb98faSBarry Smith \ | / 5459a1cb98faSBarry Smith \ | / 5460a1cb98faSBarry Smith 3 5461a1cb98faSBarry Smith .ve 5462a1cb98faSBarry Smith Notice that all points are numbered consecutively, unlike `DMPlexCreateFromCellListPetsc()` 5463939f6067SMatthew G. Knepley 54641cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()` 5465939f6067SMatthew G. Knepley @*/ 5466d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[]) 5467d71ae5a4SJacob Faibussowitsch { 54689298eaa6SMatthew G Knepley Vec coordinates; 54699298eaa6SMatthew G Knepley PetscSection coordSection; 54709298eaa6SMatthew G Knepley PetscScalar *coords; 5471811e8653SToby Isaac PetscInt coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off; 54729298eaa6SMatthew G Knepley 54739298eaa6SMatthew G Knepley PetscFunctionBegin; 54749566063dSJacob Faibussowitsch PetscCall(DMGetDimension(dm, &dim)); 54759566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateDim(dm, &dimEmbed)); 547663a3b9bcSJacob Faibussowitsch PetscCheck(dimEmbed >= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT, dimEmbed, dim); 54779298eaa6SMatthew G Knepley for (d = 0; d <= depth; ++d) pEnd += numPoints[d]; 54789566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(dm, pStart, pEnd)); 54799298eaa6SMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 54809566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeSize(dm, p, coneSize[p - pStart])); 5481ad540459SPierre Jolivet if (firstVertex < 0 && !coneSize[p - pStart]) firstVertex = p - pStart; 548297e052ccSToby Isaac } 54831dca8a05SBarry Smith PetscCheck(firstVertex >= 0 || !numPoints[0], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]); 54849566063dSJacob Faibussowitsch PetscCall(DMSetUp(dm)); /* Allocate space for cones */ 54859298eaa6SMatthew G Knepley for (p = pStart, off = 0; p < pEnd; off += coneSize[p - pStart], ++p) { 54869566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(dm, p, &cones[off])); 54879566063dSJacob Faibussowitsch PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off])); 54889298eaa6SMatthew G Knepley } 54899566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(dm)); 54909566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(dm)); 54919298eaa6SMatthew G Knepley /* Build coordinates */ 54929566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(dm, &coordSection)); 54939566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 54949566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed)); 54959566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numPoints[0])); 54969298eaa6SMatthew G Knepley for (v = firstVertex; v < firstVertex + numPoints[0]; ++v) { 54979566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed)); 54989566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed)); 54999298eaa6SMatthew G Knepley } 55009566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 55019566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 55029566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates)); 55039566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 55049566063dSJacob Faibussowitsch PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 55059566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(coordinates, dimEmbed)); 55069566063dSJacob Faibussowitsch PetscCall(VecSetType(coordinates, VECSTANDARD)); 55079318fe57SMatthew G. Knepley if (vertexCoords) { 55089566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 55099298eaa6SMatthew G Knepley for (v = 0; v < numPoints[0]; ++v) { 55109298eaa6SMatthew G Knepley PetscInt off; 55119298eaa6SMatthew G Knepley 55129566063dSJacob Faibussowitsch PetscCall(PetscSectionGetOffset(coordSection, v + firstVertex, &off)); 5513ad540459SPierre Jolivet for (d = 0; d < dimEmbed; ++d) coords[off + d] = vertexCoords[v * dimEmbed + d]; 55149298eaa6SMatthew G Knepley } 55159318fe57SMatthew G. Knepley } 55169566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 55179566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(dm, coordinates)); 55189566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 55193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 55209298eaa6SMatthew G Knepley } 55218415267dSToby Isaac 5522a4e35b19SJacob Faibussowitsch /* 5523a1cb98faSBarry Smith DMPlexCreateCellVertexFromFile - Create a `DMPLEX` mesh from a simple cell-vertex file. 5524a1cb98faSBarry Smith 5525a1cb98faSBarry Smith Collective 55268ca92349SMatthew G. Knepley 55278ca92349SMatthew G. Knepley + comm - The MPI communicator 55288ca92349SMatthew G. Knepley . filename - Name of the .dat file 55298ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh 55308ca92349SMatthew G. Knepley 55318ca92349SMatthew G. Knepley Output Parameter: 5532a1cb98faSBarry Smith . dm - The `DM` object representing the mesh 55338ca92349SMatthew G. Knepley 55348ca92349SMatthew G. Knepley Level: beginner 55358ca92349SMatthew G. Knepley 5536a1cb98faSBarry Smith Note: 5537a1cb98faSBarry Smith The format is the simplest possible: 5538a1cb98faSBarry Smith .vb 5539d0812dedSMatthew G. Knepley dim Ne Nv Nc Nl 5540d0812dedSMatthew G. Knepley v_1 v_2 ... v_Nc 5541d0812dedSMatthew G. Knepley ... 5542d0812dedSMatthew G. Knepley x y z marker_1 ... marker_Nl 5543a1cb98faSBarry Smith .ve 5544a1cb98faSBarry Smith 5545a1cb98faSBarry Smith Developer Note: 5546a1cb98faSBarry Smith Should use a `PetscViewer` not a filename 5547a1cb98faSBarry Smith 55486afe31f6SMartin Diehl .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()` 5549a4e35b19SJacob Faibussowitsch */ 5550ff6a9541SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm) 5551d71ae5a4SJacob Faibussowitsch { 55528ca92349SMatthew G. Knepley DMLabel marker; 55538ca92349SMatthew G. Knepley PetscViewer viewer; 55548ca92349SMatthew G. Knepley Vec coordinates; 55558ca92349SMatthew G. Knepley PetscSection coordSection; 55568ca92349SMatthew G. Knepley PetscScalar *coords; 55578ca92349SMatthew G. Knepley char line[PETSC_MAX_PATH_LEN]; 5558d0812dedSMatthew G. Knepley PetscInt cdim, coordSize, v, c, d; 55598ca92349SMatthew G. Knepley PetscMPIInt rank; 5560d0812dedSMatthew G. Knepley int snum, dim, Nv, Nc, Ncn, Nl; 55618ca92349SMatthew G. Knepley 55628ca92349SMatthew G. Knepley PetscFunctionBegin; 55639566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 55649566063dSJacob Faibussowitsch PetscCall(PetscViewerCreate(comm, &viewer)); 55659566063dSJacob Faibussowitsch PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII)); 55669566063dSJacob Faibussowitsch PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ)); 55679566063dSJacob Faibussowitsch PetscCall(PetscViewerFileSetName(viewer, filename)); 5568dd400576SPatrick Sanan if (rank == 0) { 5569d0812dedSMatthew G. Knepley PetscCall(PetscViewerRead(viewer, line, 5, NULL, PETSC_STRING)); 5570d0812dedSMatthew G. Knepley snum = sscanf(line, "%d %d %d %d %d", &dim, &Nc, &Nv, &Ncn, &Nl); 5571d0812dedSMatthew G. Knepley PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line); 557225ce1634SJed Brown } else { 5573f8d5e320SMatthew G. Knepley Nc = Nv = Ncn = Nl = 0; 55748ca92349SMatthew G. Knepley } 5575d0812dedSMatthew G. Knepley PetscCallMPI(MPI_Bcast(&dim, 1, MPI_INT, 0, comm)); 5576d0812dedSMatthew G. Knepley cdim = (PetscInt)dim; 55779566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 55789566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 55799566063dSJacob Faibussowitsch PetscCall(DMPlexSetChart(*dm, 0, Nc + Nv)); 5580d0812dedSMatthew G. Knepley PetscCall(DMSetDimension(*dm, (PetscInt)dim)); 55819566063dSJacob Faibussowitsch PetscCall(DMSetCoordinateDim(*dm, cdim)); 55828ca92349SMatthew G. Knepley /* Read topology */ 5583dd400576SPatrick Sanan if (rank == 0) { 5584f8d5e320SMatthew G. Knepley char format[PETSC_MAX_PATH_LEN]; 5585f8d5e320SMatthew G. Knepley PetscInt cone[8]; 55868ca92349SMatthew G. Knepley int vbuf[8], v; 55878ca92349SMatthew G. Knepley 55889371c9d4SSatish Balay for (c = 0; c < Ncn; ++c) { 55899371c9d4SSatish Balay format[c * 3 + 0] = '%'; 55909371c9d4SSatish Balay format[c * 3 + 1] = 'd'; 55919371c9d4SSatish Balay format[c * 3 + 2] = ' '; 55929371c9d4SSatish Balay } 5593f8d5e320SMatthew G. Knepley format[Ncn * 3 - 1] = '\0'; 55949566063dSJacob Faibussowitsch for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn)); 55959566063dSJacob Faibussowitsch PetscCall(DMSetUp(*dm)); 55968ca92349SMatthew G. Knepley for (c = 0; c < Nc; ++c) { 55979566063dSJacob Faibussowitsch PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING)); 5598f8d5e320SMatthew G. Knepley switch (Ncn) { 5599d71ae5a4SJacob Faibussowitsch case 2: 5600d71ae5a4SJacob Faibussowitsch snum = sscanf(line, format, &vbuf[0], &vbuf[1]); 5601d71ae5a4SJacob Faibussowitsch break; 5602d71ae5a4SJacob Faibussowitsch case 3: 5603d71ae5a4SJacob Faibussowitsch snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]); 5604d71ae5a4SJacob Faibussowitsch break; 5605d71ae5a4SJacob Faibussowitsch case 4: 5606d71ae5a4SJacob Faibussowitsch snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]); 5607d71ae5a4SJacob Faibussowitsch break; 5608d71ae5a4SJacob Faibussowitsch case 6: 5609d71ae5a4SJacob Faibussowitsch snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]); 5610d71ae5a4SJacob Faibussowitsch break; 5611d71ae5a4SJacob Faibussowitsch case 8: 5612d71ae5a4SJacob Faibussowitsch snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]); 5613d71ae5a4SJacob Faibussowitsch break; 5614d71ae5a4SJacob Faibussowitsch default: 5615d71ae5a4SJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn); 5616f8d5e320SMatthew G. Knepley } 561708401ef6SPierre Jolivet PetscCheck(snum == Ncn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line); 5618f8d5e320SMatthew G. Knepley for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc; 56198ca92349SMatthew G. Knepley /* Hexahedra are inverted */ 5620f8d5e320SMatthew G. Knepley if (Ncn == 8) { 56218ca92349SMatthew G. Knepley PetscInt tmp = cone[1]; 56228ca92349SMatthew G. Knepley cone[1] = cone[3]; 56238ca92349SMatthew G. Knepley cone[3] = tmp; 56248ca92349SMatthew G. Knepley } 56259566063dSJacob Faibussowitsch PetscCall(DMPlexSetCone(*dm, c, cone)); 56268ca92349SMatthew G. Knepley } 56278ca92349SMatthew G. Knepley } 56289566063dSJacob Faibussowitsch PetscCall(DMPlexSymmetrize(*dm)); 56299566063dSJacob Faibussowitsch PetscCall(DMPlexStratify(*dm)); 56308ca92349SMatthew G. Knepley /* Read coordinates */ 56319566063dSJacob Faibussowitsch PetscCall(DMGetCoordinateSection(*dm, &coordSection)); 56329566063dSJacob Faibussowitsch PetscCall(PetscSectionSetNumFields(coordSection, 1)); 56339566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim)); 56349566063dSJacob Faibussowitsch PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv)); 56358ca92349SMatthew G. Knepley for (v = Nc; v < Nc + Nv; ++v) { 56369566063dSJacob Faibussowitsch PetscCall(PetscSectionSetDof(coordSection, v, cdim)); 56379566063dSJacob Faibussowitsch PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim)); 56388ca92349SMatthew G. Knepley } 56399566063dSJacob Faibussowitsch PetscCall(PetscSectionSetUp(coordSection)); 56409566063dSJacob Faibussowitsch PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize)); 56419566063dSJacob Faibussowitsch PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates)); 56429566063dSJacob Faibussowitsch PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates")); 56439566063dSJacob Faibussowitsch PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE)); 56449566063dSJacob Faibussowitsch PetscCall(VecSetBlockSize(coordinates, cdim)); 56459566063dSJacob Faibussowitsch PetscCall(VecSetType(coordinates, VECSTANDARD)); 56469566063dSJacob Faibussowitsch PetscCall(VecGetArray(coordinates, &coords)); 5647dd400576SPatrick Sanan if (rank == 0) { 5648f8d5e320SMatthew G. Knepley char format[PETSC_MAX_PATH_LEN]; 56498ca92349SMatthew G. Knepley double x[3]; 5650f8d5e320SMatthew G. Knepley int l, val[3]; 56518ca92349SMatthew G. Knepley 5652f8d5e320SMatthew G. Knepley if (Nl) { 56539371c9d4SSatish Balay for (l = 0; l < Nl; ++l) { 56549371c9d4SSatish Balay format[l * 3 + 0] = '%'; 56559371c9d4SSatish Balay format[l * 3 + 1] = 'd'; 56569371c9d4SSatish Balay format[l * 3 + 2] = ' '; 56579371c9d4SSatish Balay } 5658f8d5e320SMatthew G. Knepley format[Nl * 3 - 1] = '\0'; 56599566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(*dm, "marker")); 56609566063dSJacob Faibussowitsch PetscCall(DMGetLabel(*dm, "marker", &marker)); 5661f8d5e320SMatthew G. Knepley } 56628ca92349SMatthew G. Knepley for (v = 0; v < Nv; ++v) { 56639566063dSJacob Faibussowitsch PetscCall(PetscViewerRead(viewer, line, 3 + Nl, NULL, PETSC_STRING)); 5664f8d5e320SMatthew G. Knepley snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]); 566508401ef6SPierre Jolivet PetscCheck(snum == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line); 5666f8d5e320SMatthew G. Knepley switch (Nl) { 5667d71ae5a4SJacob Faibussowitsch case 0: 5668d71ae5a4SJacob Faibussowitsch snum = 0; 5669d71ae5a4SJacob Faibussowitsch break; 5670d71ae5a4SJacob Faibussowitsch case 1: 5671d71ae5a4SJacob Faibussowitsch snum = sscanf(line, format, &val[0]); 5672d71ae5a4SJacob Faibussowitsch break; 5673d71ae5a4SJacob Faibussowitsch case 2: 5674d71ae5a4SJacob Faibussowitsch snum = sscanf(line, format, &val[0], &val[1]); 5675d71ae5a4SJacob Faibussowitsch break; 5676d71ae5a4SJacob Faibussowitsch case 3: 5677d71ae5a4SJacob Faibussowitsch snum = sscanf(line, format, &val[0], &val[1], &val[2]); 5678d71ae5a4SJacob Faibussowitsch break; 5679d71ae5a4SJacob Faibussowitsch default: 5680d71ae5a4SJacob Faibussowitsch SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl); 5681f8d5e320SMatthew G. Knepley } 568208401ef6SPierre Jolivet PetscCheck(snum == Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line); 56838ca92349SMatthew G. Knepley for (d = 0; d < cdim; ++d) coords[v * cdim + d] = x[d]; 56849566063dSJacob Faibussowitsch for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v + Nc, val[l])); 56858ca92349SMatthew G. Knepley } 56868ca92349SMatthew G. Knepley } 56879566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(coordinates, &coords)); 56889566063dSJacob Faibussowitsch PetscCall(DMSetCoordinatesLocal(*dm, coordinates)); 56899566063dSJacob Faibussowitsch PetscCall(VecDestroy(&coordinates)); 56909566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 56918ca92349SMatthew G. Knepley if (interpolate) { 56928ca92349SMatthew G. Knepley DM idm; 56938ca92349SMatthew G. Knepley DMLabel bdlabel; 56948ca92349SMatthew G. Knepley 56959566063dSJacob Faibussowitsch PetscCall(DMPlexInterpolate(*dm, &idm)); 56969566063dSJacob Faibussowitsch PetscCall(DMDestroy(dm)); 56978ca92349SMatthew G. Knepley *dm = idm; 56988ca92349SMatthew G. Knepley 5699f8d5e320SMatthew G. Knepley if (!Nl) { 57009566063dSJacob Faibussowitsch PetscCall(DMCreateLabel(*dm, "marker")); 57019566063dSJacob Faibussowitsch PetscCall(DMGetLabel(*dm, "marker", &bdlabel)); 57029566063dSJacob Faibussowitsch PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel)); 57039566063dSJacob Faibussowitsch PetscCall(DMPlexLabelComplete(*dm, bdlabel)); 57048ca92349SMatthew G. Knepley } 5705f8d5e320SMatthew G. Knepley } 57063ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 57078ca92349SMatthew G. Knepley } 57088ca92349SMatthew G. Knepley 5709cc4c1da9SBarry Smith /*@ 5710a1cb98faSBarry Smith DMPlexCreateFromFile - This takes a filename and produces a `DM` 5711a1cb98faSBarry Smith 5712a1cb98faSBarry Smith Collective 5713ca522641SMatthew G. Knepley 5714ca522641SMatthew G. Knepley Input Parameters: 5715ca522641SMatthew G. Knepley + comm - The communicator 5716ca522641SMatthew G. Knepley . filename - A file name 5717a1cb98faSBarry Smith . plexname - The object name of the resulting `DM`, also used for intra-datafile lookup by some formats 5718ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces) 5719ca522641SMatthew G. Knepley 5720ca522641SMatthew G. Knepley Output Parameter: 5721a1cb98faSBarry Smith . dm - The `DM` 5722ca522641SMatthew G. Knepley 5723a1cb98faSBarry Smith Options Database Key: 5724a1cb98faSBarry Smith . -dm_plex_create_from_hdf5_xdmf - use the `PETSC_VIEWER_HDF5_XDMF` format for reading HDF5 572502ef0d99SVaclav Hapla 572637fdd005SBarry Smith Use `-dm_plex_create_ prefix` to pass options to the internal `PetscViewer`, e.g. 5727bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective 5728bca97951SVaclav Hapla 5729ca522641SMatthew G. Knepley Level: beginner 5730ca522641SMatthew G. Knepley 5731a1cb98faSBarry Smith Notes: 5732a1cb98faSBarry Smith Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX` 5733a1cb98faSBarry Smith meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()` 5734a1cb98faSBarry Smith before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object. 5735a1cb98faSBarry Smith The input parameter name is thus used to name the `DMPLEX` object when `DMPlexCreateFromFile()` internally 5736a1cb98faSBarry Smith calls `DMLoad()`. Currently, name is ignored for other viewer types and/or formats. 5737a1cb98faSBarry Smith 57381cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()` 5739ca522641SMatthew G. Knepley @*/ 5740d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm) 5741d71ae5a4SJacob Faibussowitsch { 5742ef3a5affSJacob Faibussowitsch const char extGmsh[] = ".msh"; 5743ef3a5affSJacob Faibussowitsch const char extGmsh2[] = ".msh2"; 5744ef3a5affSJacob Faibussowitsch const char extGmsh4[] = ".msh4"; 5745ef3a5affSJacob Faibussowitsch const char extCGNS[] = ".cgns"; 5746ef3a5affSJacob Faibussowitsch const char extExodus[] = ".exo"; 5747ef3a5affSJacob Faibussowitsch const char extExodus_e[] = ".e"; 5748ef3a5affSJacob Faibussowitsch const char extGenesis[] = ".gen"; 5749ef3a5affSJacob Faibussowitsch const char extFluent[] = ".cas"; 5750ef3a5affSJacob Faibussowitsch const char extHDF5[] = ".h5"; 57516f2c871aSStefano Zampini const char extXDMFHDF5[] = ".xdmf.h5"; 5752ef3a5affSJacob Faibussowitsch const char extPLY[] = ".ply"; 5753ef3a5affSJacob Faibussowitsch const char extEGADSLite[] = ".egadslite"; 5754ef3a5affSJacob Faibussowitsch const char extEGADS[] = ".egads"; 5755ef3a5affSJacob Faibussowitsch const char extIGES[] = ".igs"; 5756ef3a5affSJacob Faibussowitsch const char extSTEP[] = ".stp"; 5757ef3a5affSJacob Faibussowitsch const char extCV[] = ".dat"; 5758ca522641SMatthew G. Knepley size_t len; 57596afe31f6SMartin Diehl PetscBool isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV, isXDMFHDF5; 5760ca522641SMatthew G. Knepley PetscMPIInt rank; 5761ca522641SMatthew G. Knepley 5762ca522641SMatthew G. Knepley PetscFunctionBegin; 57634f572ea9SToby Isaac PetscAssertPointer(filename, 2); 57644f572ea9SToby Isaac if (plexname) PetscAssertPointer(plexname, 3); 57654f572ea9SToby Isaac PetscAssertPointer(dm, 5); 57669566063dSJacob Faibussowitsch PetscCall(DMInitializePackage()); 57679566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile, 0, 0, 0, 0)); 57689566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 57699566063dSJacob Faibussowitsch PetscCall(PetscStrlen(filename, &len)); 577028b400f6SJacob Faibussowitsch PetscCheck(len, comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path"); 5771ef3a5affSJacob Faibussowitsch 57729371c9d4SSatish Balay #define CheckExtension(extension__, is_extension__) \ 57739371c9d4SSatish Balay do { \ 5774274aaeaaSJacob Faibussowitsch PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \ 5775274aaeaaSJacob Faibussowitsch /* don't count the null-terminator at the end */ \ 5776274aaeaaSJacob Faibussowitsch const size_t ext_len = sizeof(extension__) - 1; \ 5777274aaeaaSJacob Faibussowitsch if (len < ext_len) { \ 5778ef3a5affSJacob Faibussowitsch is_extension__ = PETSC_FALSE; \ 5779ef3a5affSJacob Faibussowitsch } else { \ 5780274aaeaaSJacob Faibussowitsch PetscCall(PetscStrncmp(filename + len - ext_len, extension__, ext_len, &is_extension__)); \ 5781ef3a5affSJacob Faibussowitsch } \ 5782ef3a5affSJacob Faibussowitsch } while (0) 5783ef3a5affSJacob Faibussowitsch 5784ef3a5affSJacob Faibussowitsch CheckExtension(extGmsh, isGmsh); 5785ef3a5affSJacob Faibussowitsch CheckExtension(extGmsh2, isGmsh2); 5786ef3a5affSJacob Faibussowitsch CheckExtension(extGmsh4, isGmsh4); 5787ef3a5affSJacob Faibussowitsch CheckExtension(extCGNS, isCGNS); 5788ef3a5affSJacob Faibussowitsch CheckExtension(extExodus, isExodus); 5789ef3a5affSJacob Faibussowitsch if (!isExodus) CheckExtension(extExodus_e, isExodus); 5790ef3a5affSJacob Faibussowitsch CheckExtension(extGenesis, isGenesis); 5791ef3a5affSJacob Faibussowitsch CheckExtension(extFluent, isFluent); 5792ef3a5affSJacob Faibussowitsch CheckExtension(extHDF5, isHDF5); 5793ef3a5affSJacob Faibussowitsch CheckExtension(extPLY, isPLY); 5794ef3a5affSJacob Faibussowitsch CheckExtension(extEGADSLite, isEGADSLite); 5795ef3a5affSJacob Faibussowitsch CheckExtension(extEGADS, isEGADS); 5796ef3a5affSJacob Faibussowitsch CheckExtension(extIGES, isIGES); 5797ef3a5affSJacob Faibussowitsch CheckExtension(extSTEP, isSTEP); 5798ef3a5affSJacob Faibussowitsch CheckExtension(extCV, isCV); 57996f2c871aSStefano Zampini CheckExtension(extXDMFHDF5, isXDMFHDF5); 5800ef3a5affSJacob Faibussowitsch 5801ef3a5affSJacob Faibussowitsch #undef CheckExtension 5802ef3a5affSJacob Faibussowitsch 5803de78e4feSLisandro Dalcin if (isGmsh || isGmsh2 || isGmsh4) { 58049566063dSJacob Faibussowitsch PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm)); 5805ca522641SMatthew G. Knepley } else if (isCGNS) { 58069566063dSJacob Faibussowitsch PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm)); 580790c68965SMatthew G. Knepley } else if (isExodus || isGenesis) { 58089566063dSJacob Faibussowitsch PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm)); 58092f0bd6dcSMichael Lange } else if (isFluent) { 58109566063dSJacob Faibussowitsch PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm)); 5811cc2f8f65SMatthew G. Knepley } else if (isHDF5) { 5812cc2f8f65SMatthew G. Knepley PetscViewer viewer; 5813cc2f8f65SMatthew G. Knepley 581443b242b4SVaclav 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 */ 58156f2c871aSStefano Zampini PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &isXDMFHDF5, NULL)); 58169566063dSJacob Faibussowitsch PetscCall(PetscViewerCreate(comm, &viewer)); 58179566063dSJacob Faibussowitsch PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5)); 58189566063dSJacob Faibussowitsch PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_")); 58199566063dSJacob Faibussowitsch PetscCall(PetscViewerSetFromOptions(viewer)); 58209566063dSJacob Faibussowitsch PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ)); 58219566063dSJacob Faibussowitsch PetscCall(PetscViewerFileSetName(viewer, filename)); 5822cd7e8a5eSksagiyam 58239566063dSJacob Faibussowitsch PetscCall(DMCreate(comm, dm)); 5824f4f49eeaSPierre Jolivet PetscCall(PetscObjectSetName((PetscObject)*dm, plexname)); 58259566063dSJacob Faibussowitsch PetscCall(DMSetType(*dm, DMPLEX)); 58266f2c871aSStefano Zampini if (isXDMFHDF5) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF)); 58279566063dSJacob Faibussowitsch PetscCall(DMLoad(*dm, viewer)); 58286f2c871aSStefano Zampini if (isXDMFHDF5) PetscCall(PetscViewerPopFormat(viewer)); 58299566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 58305fd9971aSMatthew G. Knepley 58315fd9971aSMatthew G. Knepley if (interpolate) { 58325fd9971aSMatthew G. Knepley DM idm; 58335fd9971aSMatthew G. Knepley 58349566063dSJacob Faibussowitsch PetscCall(DMPlexInterpolate(*dm, &idm)); 58359566063dSJacob Faibussowitsch PetscCall(DMDestroy(dm)); 58365fd9971aSMatthew G. Knepley *dm = idm; 58375fd9971aSMatthew G. Knepley } 5838f2801cd6SMatthew G. Knepley } else if (isPLY) { 58399566063dSJacob Faibussowitsch PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm)); 5840c1cad2e7SMatthew G. Knepley } else if (isEGADSLite || isEGADS || isIGES || isSTEP) { 58419566063dSJacob Faibussowitsch if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm)); 58429566063dSJacob Faibussowitsch else PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm)); 58437bee2925SMatthew Knepley if (!interpolate) { 58447bee2925SMatthew Knepley DM udm; 58457bee2925SMatthew Knepley 58469566063dSJacob Faibussowitsch PetscCall(DMPlexUninterpolate(*dm, &udm)); 58479566063dSJacob Faibussowitsch PetscCall(DMDestroy(dm)); 58487bee2925SMatthew Knepley *dm = udm; 58497bee2925SMatthew Knepley } 58508ca92349SMatthew G. Knepley } else if (isCV) { 58519566063dSJacob Faibussowitsch PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm)); 585298921bdaSJacob Faibussowitsch } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename); 58539566063dSJacob Faibussowitsch PetscCall(PetscStrlen(plexname, &len)); 5854f4f49eeaSPierre Jolivet if (len) PetscCall(PetscObjectSetName((PetscObject)*dm, plexname)); 58559566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile, 0, 0, 0, 0)); 58563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5857ca522641SMatthew G. Knepley } 585812b8a6daSStefano Zampini 5859cc4c1da9SBarry Smith /*@ 58609f6c5813SMatthew G. Knepley DMPlexCreateEphemeral - This takes a `DMPlexTransform` and a base `DMPlex` and produces an ephemeral `DM`, meaning one that is created on the fly in response to queries. 58619f6c5813SMatthew G. Knepley 58620528010dSStefano Zampini Input Parameters: 58630528010dSStefano Zampini + tr - The `DMPlexTransform` 58640528010dSStefano Zampini - prefix - An options prefix, or NULL 58659f6c5813SMatthew G. Knepley 58669f6c5813SMatthew G. Knepley Output Parameter: 58679f6c5813SMatthew G. Knepley . dm - The `DM` 58689f6c5813SMatthew G. Knepley 58699f6c5813SMatthew G. Knepley Level: beginner 58709f6c5813SMatthew G. Knepley 587120f4b53cSBarry Smith Notes: 587220f4b53cSBarry Smith An emphemeral mesh is one that is not stored concretely, as in the default `DMPLEX` implementation, but rather is produced on the fly in response to queries, using information from the transform and the base mesh. 587320f4b53cSBarry Smith 58749f6c5813SMatthew G. Knepley .seealso: `DMPlexCreateFromFile`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()` 58759f6c5813SMatthew G. Knepley @*/ 58760528010dSStefano Zampini PetscErrorCode DMPlexCreateEphemeral(DMPlexTransform tr, const char prefix[], DM *dm) 58779f6c5813SMatthew G. Knepley { 58780528010dSStefano Zampini DM bdm, bcdm, cdm; 58790528010dSStefano Zampini Vec coordinates, coordinatesNew; 58800528010dSStefano Zampini PetscSection cs; 58810528010dSStefano Zampini PetscInt dim, cdim, Nl; 58829f6c5813SMatthew G. Knepley 58839f6c5813SMatthew G. Knepley PetscFunctionBegin; 58849f6c5813SMatthew G. Knepley PetscCall(DMCreate(PetscObjectComm((PetscObject)tr), dm)); 58859f6c5813SMatthew G. Knepley PetscCall(DMSetType(*dm, DMPLEX)); 58860528010dSStefano Zampini ((DM_Plex *)(*dm)->data)->interpolated = DMPLEX_INTERPOLATED_FULL; 58870528010dSStefano Zampini // Handle coordinates 58880528010dSStefano Zampini PetscCall(DMPlexTransformGetDM(tr, &bdm)); 58890528010dSStefano Zampini PetscCall(DMGetCoordinateDim(bdm, &cdim)); 58900528010dSStefano Zampini PetscCall(DMSetCoordinateDim(*dm, cdim)); 58910528010dSStefano Zampini PetscCall(DMGetDimension(bdm, &dim)); 58920528010dSStefano Zampini PetscCall(DMSetDimension(*dm, dim)); 58930528010dSStefano Zampini PetscCall(DMGetCoordinateDM(bdm, &bcdm)); 58940528010dSStefano Zampini PetscCall(DMGetCoordinateDM(*dm, &cdm)); 58950528010dSStefano Zampini PetscCall(DMCopyDisc(bcdm, cdm)); 58960528010dSStefano Zampini PetscCall(DMGetLocalSection(cdm, &cs)); 58970528010dSStefano Zampini PetscCall(PetscSectionSetNumFields(cs, 1)); 58980528010dSStefano Zampini PetscCall(PetscSectionSetFieldComponents(cs, 0, cdim)); 58990528010dSStefano Zampini PetscCall(DMGetCoordinatesLocal(bdm, &coordinates)); 59000528010dSStefano Zampini PetscCall(VecDuplicate(coordinates, &coordinatesNew)); 59010528010dSStefano Zampini PetscCall(VecCopy(coordinates, coordinatesNew)); 59020528010dSStefano Zampini PetscCall(DMSetCoordinatesLocal(*dm, coordinatesNew)); 59030528010dSStefano Zampini PetscCall(VecDestroy(&coordinatesNew)); 59049f6c5813SMatthew G. Knepley 59059f6c5813SMatthew G. Knepley PetscCall(PetscObjectReference((PetscObject)tr)); 59069f6c5813SMatthew G. Knepley PetscCall(DMPlexTransformDestroy(&((DM_Plex *)(*dm)->data)->tr)); 59079f6c5813SMatthew G. Knepley ((DM_Plex *)(*dm)->data)->tr = tr; 59080528010dSStefano Zampini PetscCall(DMPlexDistributeSetDefault(*dm, PETSC_FALSE)); 59090528010dSStefano Zampini PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*dm, prefix)); 59100528010dSStefano Zampini PetscCall(DMSetFromOptions(*dm)); 59119f6c5813SMatthew G. Knepley 59129f6c5813SMatthew G. Knepley PetscCall(DMGetNumLabels(bdm, &Nl)); 59139f6c5813SMatthew G. Knepley for (PetscInt l = 0; l < Nl; ++l) { 59149f6c5813SMatthew G. Knepley DMLabel label, labelNew; 59159f6c5813SMatthew G. Knepley const char *lname; 59169f6c5813SMatthew G. Knepley PetscBool isDepth, isCellType; 59179f6c5813SMatthew G. Knepley 59189f6c5813SMatthew G. Knepley PetscCall(DMGetLabelName(bdm, l, &lname)); 59199f6c5813SMatthew G. Knepley PetscCall(PetscStrcmp(lname, "depth", &isDepth)); 59209f6c5813SMatthew G. Knepley if (isDepth) continue; 59219f6c5813SMatthew G. Knepley PetscCall(PetscStrcmp(lname, "celltype", &isCellType)); 59229f6c5813SMatthew G. Knepley if (isCellType) continue; 59239f6c5813SMatthew G. Knepley PetscCall(DMCreateLabel(*dm, lname)); 59249f6c5813SMatthew G. Knepley PetscCall(DMGetLabel(bdm, lname, &label)); 59259f6c5813SMatthew G. Knepley PetscCall(DMGetLabel(*dm, lname, &labelNew)); 59269f6c5813SMatthew G. Knepley PetscCall(DMLabelSetType(labelNew, DMLABELEPHEMERAL)); 59279f6c5813SMatthew G. Knepley PetscCall(DMLabelEphemeralSetLabel(labelNew, label)); 59289f6c5813SMatthew G. Knepley PetscCall(DMLabelEphemeralSetTransform(labelNew, tr)); 59299f6c5813SMatthew G. Knepley PetscCall(DMLabelSetUp(labelNew)); 59309f6c5813SMatthew G. Knepley } 59313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 59329f6c5813SMatthew G. Knepley } 5933