190d1c1a4SMatthew G. Knepley static char help[] = "Tests dof numberings for external integrators such as LibCEED.\n\n"; 290d1c1a4SMatthew G. Knepley 390d1c1a4SMatthew G. Knepley #include <petscdmplex.h> 490d1c1a4SMatthew G. Knepley #include <petscds.h> 590d1c1a4SMatthew G. Knepley 690d1c1a4SMatthew G. Knepley typedef struct { 752e7713aSMatthew G. Knepley PetscBool useFE; 85f06a3ddSJed Brown PetscInt check_face; 95f06a3ddSJed Brown PetscBool closure_tensor; 1090d1c1a4SMatthew G. Knepley } AppCtx; 1190d1c1a4SMatthew G. Knepley 12d71ae5a4SJacob Faibussowitsch static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) 13d71ae5a4SJacob Faibussowitsch { 1490d1c1a4SMatthew G. Knepley PetscFunctionBeginUser; 1552e7713aSMatthew G. Knepley options->useFE = PETSC_TRUE; 165f06a3ddSJed Brown options->check_face = 1; 175f06a3ddSJed Brown options->closure_tensor = PETSC_FALSE; 1890d1c1a4SMatthew G. Knepley PetscOptionsBegin(comm, "", "Dof Ordering Options", "DMPLEX"); 1952e7713aSMatthew G. Knepley PetscCall(PetscOptionsBool("-use_fe", "Use FE or FV discretization", "ex49.c", options->useFE, &options->useFE, NULL)); 205f06a3ddSJed Brown PetscCall(PetscOptionsInt("-check_face", "Face set to report on", "ex49.c", options->check_face, &options->check_face, NULL)); 215f06a3ddSJed Brown PetscCall(PetscOptionsBool("-closure_tensor", "Use DMPlexSetClosurePermutationTensor()", "ex49.c", options->closure_tensor, &options->closure_tensor, NULL)); 2290d1c1a4SMatthew G. Knepley PetscOptionsEnd(); 233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2490d1c1a4SMatthew G. Knepley } 2590d1c1a4SMatthew G. Knepley 26d71ae5a4SJacob Faibussowitsch static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) 27d71ae5a4SJacob Faibussowitsch { 2890d1c1a4SMatthew G. Knepley PetscFunctionBeginUser; 2990d1c1a4SMatthew G. Knepley PetscCall(DMCreate(comm, dm)); 3090d1c1a4SMatthew G. Knepley PetscCall(DMSetType(*dm, DMPLEX)); 3190d1c1a4SMatthew G. Knepley PetscCall(DMSetFromOptions(*dm)); 3290d1c1a4SMatthew G. Knepley PetscCall(DMSetApplicationContext(*dm, user)); 3390d1c1a4SMatthew G. Knepley PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view")); 343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3590d1c1a4SMatthew G. Knepley } 3690d1c1a4SMatthew G. Knepley 37d71ae5a4SJacob Faibussowitsch static PetscErrorCode SetupDiscretization(DM dm, AppCtx *user) 38d71ae5a4SJacob Faibussowitsch { 3990d1c1a4SMatthew G. Knepley DM cdm = dm; 4090d1c1a4SMatthew G. Knepley PetscInt dim; 4190d1c1a4SMatthew G. Knepley 4290d1c1a4SMatthew G. Knepley PetscFunctionBeginUser; 4390d1c1a4SMatthew G. Knepley PetscCall(DMGetDimension(dm, &dim)); 4452e7713aSMatthew G. Knepley if (user->useFE) { 4552e7713aSMatthew G. Knepley PetscFE fe; 46*5962854dSMatthew G. Knepley DMPolytopeType ct; 47*5962854dSMatthew G. Knepley PetscInt cStart; 4852e7713aSMatthew G. Knepley 49*5962854dSMatthew G. Knepley PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, NULL)); 50*5962854dSMatthew G. Knepley PetscCall(DMPlexGetCellType(dm, cStart, &ct)); 51*5962854dSMatthew G. Knepley PetscCall(PetscFECreateByCell(PETSC_COMM_SELF, dim, 1, ct, NULL, -1, &fe)); 5290d1c1a4SMatthew G. Knepley PetscCall(PetscObjectSetName((PetscObject)fe, "scalar")); 5390d1c1a4SMatthew G. Knepley PetscCall(DMSetField(dm, 0, NULL, (PetscObject)fe)); 5490d1c1a4SMatthew G. Knepley PetscCall(DMSetField(dm, 1, NULL, (PetscObject)fe)); 5590d1c1a4SMatthew G. Knepley PetscCall(PetscFEDestroy(&fe)); 5652e7713aSMatthew G. Knepley } else { 5752e7713aSMatthew G. Knepley PetscFV fv; 5852e7713aSMatthew G. Knepley 5952e7713aSMatthew G. Knepley PetscCall(PetscFVCreate(PETSC_COMM_SELF, &fv)); 6052e7713aSMatthew G. Knepley PetscCall(PetscFVSetType(fv, PETSCFVLEASTSQUARES)); 6152e7713aSMatthew G. Knepley PetscCall(PetscFVSetNumComponents(fv, dim)); 6252e7713aSMatthew G. Knepley PetscCall(PetscFVSetSpatialDimension(fv, dim)); 6352e7713aSMatthew G. Knepley PetscCall(PetscFVSetFromOptions(fv)); 6452e7713aSMatthew G. Knepley PetscCall(PetscFVSetUp(fv)); 6552e7713aSMatthew G. Knepley PetscCall(PetscObjectSetName((PetscObject)fv, "vector")); 6652e7713aSMatthew G. Knepley PetscCall(DMSetField(dm, 0, NULL, (PetscObject)fv)); 6752e7713aSMatthew G. Knepley PetscCall(PetscFVDestroy(&fv)); 6852e7713aSMatthew G. Knepley } 6990d1c1a4SMatthew G. Knepley PetscCall(DMCreateDS(dm)); 7090d1c1a4SMatthew G. Knepley while (cdm) { 7190d1c1a4SMatthew G. Knepley PetscCall(DMCopyDisc(dm, cdm)); 7290d1c1a4SMatthew G. Knepley PetscCall(DMGetCoarseDM(cdm, &cdm)); 7390d1c1a4SMatthew G. Knepley } 743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7590d1c1a4SMatthew G. Knepley } 7690d1c1a4SMatthew G. Knepley 775f06a3ddSJed Brown static PetscErrorCode CheckOffsets(DM dm, AppCtx *user, const char *domain_name, PetscInt label_value, PetscInt height) 78d71ae5a4SJacob Faibussowitsch { 7990d1c1a4SMatthew G. Knepley const char *height_name[] = {"cells", "faces"}; 8090d1c1a4SMatthew G. Knepley DMLabel domain_label = NULL; 8190d1c1a4SMatthew G. Knepley DM cdm; 8290d1c1a4SMatthew G. Knepley PetscInt Nf, f; 83f2c6b1a2SJed Brown ISLocalToGlobalMapping ltog; 8490d1c1a4SMatthew G. Knepley 8590d1c1a4SMatthew G. Knepley PetscFunctionBeginUser; 8690d1c1a4SMatthew G. Knepley if (domain_name) PetscCall(DMGetLabel(dm, domain_name, &domain_label)); 873e72e933SJed Brown PetscCall(PetscPrintf(PETSC_COMM_WORLD, "## %s: '%s' {%" PetscInt_FMT "}%s\n", height_name[height], domain_name ? domain_name : "default", label_value, domain_name && !domain_label ? " (null label)" : "")); 883ba16761SJacob Faibussowitsch if (domain_name && !domain_label) PetscFunctionReturn(PETSC_SUCCESS); 895f06a3ddSJed Brown if (user->closure_tensor) PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL)); 9090d1c1a4SMatthew G. Knepley // Offsets for cell closures 9190d1c1a4SMatthew G. Knepley PetscCall(DMGetNumFields(dm, &Nf)); 9290d1c1a4SMatthew G. Knepley for (f = 0; f < Nf; ++f) { 9352e7713aSMatthew G. Knepley PetscObject obj; 9452e7713aSMatthew G. Knepley PetscClassId id; 9590d1c1a4SMatthew G. Knepley char name[PETSC_MAX_PATH_LEN]; 9690d1c1a4SMatthew G. Knepley 9752e7713aSMatthew G. Knepley PetscCall(DMGetField(dm, f, NULL, &obj)); 9852e7713aSMatthew G. Knepley PetscCall(PetscObjectGetClassId(obj, &id)); 9952e7713aSMatthew G. Knepley if (id == PETSCFE_CLASSID) { 10052e7713aSMatthew G. Knepley IS offIS; 10152e7713aSMatthew G. Knepley PetscInt *offsets, Ncell, Ncl, Nc, n; 10252e7713aSMatthew G. Knepley 10390d1c1a4SMatthew G. Knepley PetscCall(DMPlexGetLocalOffsets(dm, domain_label, label_value, height, f, &Ncell, &Ncl, &Nc, &n, &offsets)); 10490d1c1a4SMatthew G. Knepley PetscCall(ISCreateGeneral(PETSC_COMM_SELF, Ncell * Ncl, offsets, PETSC_OWN_POINTER, &offIS)); 10590d1c1a4SMatthew G. Knepley PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "Field %" PetscInt_FMT " Offsets", f)); 10690d1c1a4SMatthew G. Knepley PetscCall(PetscObjectSetName((PetscObject)offIS, name)); 10790d1c1a4SMatthew G. Knepley PetscCall(ISViewFromOptions(offIS, NULL, "-offsets_view")); 10890d1c1a4SMatthew G. Knepley PetscCall(ISDestroy(&offIS)); 10952e7713aSMatthew G. Knepley } else if (id == PETSCFV_CLASSID) { 11052e7713aSMatthew G. Knepley IS offIS; 111*5962854dSMatthew G. Knepley PetscInt *offsets, *offsetsNeg, *offsetsPos, Nface, Nc, n, i = 0; 11252e7713aSMatthew G. Knepley 11352e7713aSMatthew G. Knepley PetscCall(DMPlexGetLocalOffsetsSupport(dm, domain_label, label_value, &Nface, &Nc, &n, &offsetsNeg, &offsetsPos)); 11452e7713aSMatthew G. Knepley PetscCall(PetscMalloc1(Nface * Nc * 2, &offsets)); 115*5962854dSMatthew G. Knepley for (PetscInt f = 0; f < Nface; ++f) { 11652e7713aSMatthew G. Knepley for (PetscInt c = 0; c < Nc; ++c) offsets[i++] = offsetsNeg[f * Nc + c]; 11752e7713aSMatthew G. Knepley for (PetscInt c = 0; c < Nc; ++c) offsets[i++] = offsetsPos[f * Nc + c]; 11852e7713aSMatthew G. Knepley } 119*5962854dSMatthew G. Knepley PetscCheck(i == Nface * Nc * 2, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total offsets %" PetscInt_FMT " != %" PetscInt_FMT, i, Nface * Nc * 2); 12052e7713aSMatthew G. Knepley PetscCall(PetscFree(offsetsNeg)); 12152e7713aSMatthew G. Knepley PetscCall(PetscFree(offsetsPos)); 12252e7713aSMatthew G. Knepley PetscCall(ISCreateGeneral(PETSC_COMM_SELF, Nface * Nc * 2, offsets, PETSC_OWN_POINTER, &offIS)); 12352e7713aSMatthew G. Knepley PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "Field %" PetscInt_FMT " Offsets", f)); 12452e7713aSMatthew G. Knepley PetscCall(PetscObjectSetName((PetscObject)offIS, name)); 12552e7713aSMatthew G. Knepley PetscCall(ISViewFromOptions(offIS, NULL, "-offsets_view")); 12652e7713aSMatthew G. Knepley PetscCall(ISDestroy(&offIS)); 12752e7713aSMatthew G. Knepley } else SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_PLIB, "Unrecognized type for DM field %" PetscInt_FMT, f); 12890d1c1a4SMatthew G. Knepley } 129f2c6b1a2SJed Brown PetscCall(DMGetLocalToGlobalMapping(dm, <og)); 130f2c6b1a2SJed Brown PetscCall(ISLocalToGlobalMappingViewFromOptions(ltog, NULL, "-ltog_view")); 131f2c6b1a2SJed Brown 13290d1c1a4SMatthew G. Knepley // Offsets for coordinates 13390d1c1a4SMatthew G. Knepley { 13490d1c1a4SMatthew G. Knepley Vec X; 13590d1c1a4SMatthew G. Knepley PetscSection s; 13690d1c1a4SMatthew G. Knepley const PetscScalar *x; 13790d1c1a4SMatthew G. Knepley const char *cname; 13852e7713aSMatthew G. Knepley PetscInt cdim, *offsets, Ncell, Ncl, Nc, n; 13990d1c1a4SMatthew G. Knepley PetscBool isDG = PETSC_FALSE; 14090d1c1a4SMatthew G. Knepley 14190d1c1a4SMatthew G. Knepley PetscCall(DMGetCellCoordinateDM(dm, &cdm)); 14290d1c1a4SMatthew G. Knepley if (!cdm) { 1439371c9d4SSatish Balay PetscCall(DMGetCoordinateDM(dm, &cdm)); 1449371c9d4SSatish Balay cname = "Coordinates"; 14590d1c1a4SMatthew G. Knepley PetscCall(DMGetCoordinatesLocal(dm, &X)); 14690d1c1a4SMatthew G. Knepley } else { 14790d1c1a4SMatthew G. Knepley isDG = PETSC_TRUE; 14890d1c1a4SMatthew G. Knepley cname = "DG Coordinates"; 14990d1c1a4SMatthew G. Knepley PetscCall(DMGetCellCoordinatesLocal(dm, &X)); 15090d1c1a4SMatthew G. Knepley } 1513ba16761SJacob Faibussowitsch if (isDG && height) PetscFunctionReturn(PETSC_SUCCESS); 15290d1c1a4SMatthew G. Knepley if (domain_name) PetscCall(DMGetLabel(cdm, domain_name, &domain_label)); 1535f06a3ddSJed Brown if (user->closure_tensor) PetscCall(DMPlexSetClosurePermutationTensor(cdm, PETSC_DETERMINE, NULL)); 15490d1c1a4SMatthew G. Knepley PetscCall(DMPlexGetLocalOffsets(cdm, domain_label, label_value, height, 0, &Ncell, &Ncl, &Nc, &n, &offsets)); 15590d1c1a4SMatthew G. Knepley PetscCall(DMGetCoordinateDim(dm, &cdim)); 15690d1c1a4SMatthew G. Knepley PetscCheck(Nc == cdim, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Geometric dimension %" PetscInt_FMT " should be %" PetscInt_FMT, Nc, cdim); 15790d1c1a4SMatthew G. Knepley PetscCall(DMGetLocalSection(cdm, &s)); 15890d1c1a4SMatthew G. Knepley PetscCall(VecGetArrayRead(X, &x)); 1595f06a3ddSJed Brown PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%s by element in %s order\n", cname, user->closure_tensor ? "tensor" : "bfs")); 16090d1c1a4SMatthew G. Knepley for (PetscInt c = 0; c < Ncell; ++c) { 16190d1c1a4SMatthew G. Knepley for (PetscInt v = 0; v < Ncl; ++v) { 16290d1c1a4SMatthew G. Knepley PetscInt off = offsets[c * Ncl + v], dgdof; 16390d1c1a4SMatthew G. Knepley const PetscScalar *vx = &x[off]; 16490d1c1a4SMatthew G. Knepley 16590d1c1a4SMatthew G. Knepley if (isDG) { 16690d1c1a4SMatthew G. Knepley PetscCall(PetscSectionGetDof(s, c, &dgdof)); 16790d1c1a4SMatthew G. Knepley PetscCheck(Ncl * Nc == dgdof, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Offset size %" PetscInt_FMT " should be %" PetscInt_FMT, Ncl * Nc, dgdof); 16890d1c1a4SMatthew G. Knepley } 169eefd97a2SJed Brown switch (cdim) { 1703e72e933SJed Brown case 1: 1713e72e933SJed Brown PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%" PetscInt_FMT "] %" PetscInt_FMT " <-- %2" PetscInt_FMT " (% 4.2f)\n", c, v, off, (double)PetscRealPart(vx[0]))); 1723e72e933SJed Brown break; 173d71ae5a4SJacob Faibussowitsch case 2: 1743e72e933SJed Brown PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%" PetscInt_FMT "] %" PetscInt_FMT " <-- %2" PetscInt_FMT " (% 4.2f, % 4.2f)\n", c, v, off, (double)PetscRealPart(vx[0]), (double)PetscRealPart(vx[1]))); 175d71ae5a4SJacob Faibussowitsch break; 176eefd97a2SJed Brown case 3: 1773e72e933SJed Brown PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%" PetscInt_FMT "] %" PetscInt_FMT " <-- %2" PetscInt_FMT " (% 4.2f, % 4.2f, % 4.2f)\n", c, v, off, (double)PetscRealPart(vx[0]), (double)PetscRealPart(vx[1]), (double)PetscRealPart(vx[2]))); 178eefd97a2SJed Brown } 17990d1c1a4SMatthew G. Knepley } 18090d1c1a4SMatthew G. Knepley } 1813e72e933SJed Brown PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, stdout)); 18290d1c1a4SMatthew G. Knepley PetscCall(VecRestoreArrayRead(X, &x)); 18390d1c1a4SMatthew G. Knepley PetscCall(PetscFree(offsets)); 184f2c6b1a2SJed Brown PetscCall(DMGetLocalToGlobalMapping(cdm, <og)); 185f2c6b1a2SJed Brown PetscCall(ISLocalToGlobalMappingViewFromOptions(ltog, NULL, "-coord_ltog_view")); 18690d1c1a4SMatthew G. Knepley } 1873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 18890d1c1a4SMatthew G. Knepley } 18990d1c1a4SMatthew G. Knepley 190d71ae5a4SJacob Faibussowitsch int main(int argc, char **argv) 191d71ae5a4SJacob Faibussowitsch { 19290d1c1a4SMatthew G. Knepley DM dm; 19390d1c1a4SMatthew G. Knepley AppCtx user; 1943e72e933SJed Brown PetscInt depth; 19590d1c1a4SMatthew G. Knepley 196327415f7SBarry Smith PetscFunctionBeginUser; 19790d1c1a4SMatthew G. Knepley PetscCall(PetscInitialize(&argc, &argv, NULL, help)); 19890d1c1a4SMatthew G. Knepley PetscCall(ProcessOptions(PETSC_COMM_WORLD, &user)); 19990d1c1a4SMatthew G. Knepley PetscCall(CreateMesh(PETSC_COMM_WORLD, &user, &dm)); 20090d1c1a4SMatthew G. Knepley PetscCall(SetupDiscretization(dm, &user)); 2015f06a3ddSJed Brown PetscCall(CheckOffsets(dm, &user, NULL, 0, 0)); 2023e72e933SJed Brown PetscCall(DMPlexGetDepth(dm, &depth)); 2035f06a3ddSJed Brown if (depth > 1) PetscCall(CheckOffsets(dm, &user, "Face Sets", user.check_face, 1)); 20490d1c1a4SMatthew G. Knepley PetscCall(DMDestroy(&dm)); 20590d1c1a4SMatthew G. Knepley PetscCall(PetscFinalize()); 20690d1c1a4SMatthew G. Knepley return 0; 20790d1c1a4SMatthew G. Knepley } 20890d1c1a4SMatthew G. Knepley 20990d1c1a4SMatthew G. Knepley /*TEST 21090d1c1a4SMatthew G. Knepley 21190d1c1a4SMatthew G. Knepley test: 21290d1c1a4SMatthew G. Knepley suffix: 0 21390d1c1a4SMatthew G. Knepley requires: triangle 21490d1c1a4SMatthew G. Knepley args: -dm_refine 1 -petscspace_degree 1 -dm_view -offsets_view 21590d1c1a4SMatthew G. Knepley 21690d1c1a4SMatthew G. Knepley test: 21790d1c1a4SMatthew G. Knepley suffix: 1 21890d1c1a4SMatthew G. Knepley args: -dm_plex_simplex 0 -dm_plex_box_bd periodic,none -dm_plex_box_faces 3,3 -dm_sparse_localize 0 -petscspace_degree 1 \ 21990d1c1a4SMatthew G. Knepley -dm_view -offsets_view 22090d1c1a4SMatthew G. Knepley 22190d1c1a4SMatthew G. Knepley test: 22290d1c1a4SMatthew G. Knepley suffix: cg_2d 22390d1c1a4SMatthew G. Knepley args: -dm_plex_simplex 0 -dm_plex_box_bd none,none -dm_plex_box_faces 3,3 -petscspace_degree 1 \ 22490d1c1a4SMatthew G. Knepley -dm_view -offsets_view 2253e72e933SJed Brown 2263e72e933SJed Brown test: 2273e72e933SJed Brown suffix: 1d_sfc 2285dca41c3SJed Brown args: -dm_plex_simplex 0 -dm_plex_dim 1 -dm_plex_shape zbox -dm_plex_box_faces 3 1 -dm_view -coord_ltog_view 2293e72e933SJed Brown 2303e72e933SJed Brown test: 2313e72e933SJed Brown suffix: 2d_sfc 2323e72e933SJed Brown nsize: 2 2335dca41c3SJed Brown args: -dm_plex_simplex 0 -dm_plex_dim 2 -dm_plex_shape zbox -dm_plex_box_faces 4,3 -dm_distribute 0 -petscspace_degree 1 -dm_view 2343e72e933SJed Brown 2354e2e9504SJed Brown test: 2364e2e9504SJed Brown suffix: 2d_sfc_periodic 2374e2e9504SJed Brown nsize: 2 2385dca41c3SJed Brown args: -dm_plex_simplex 0 -dm_plex_dim 2 -dm_plex_shape zbox -dm_plex_box_faces 4,3 -dm_distribute 0 -petscspace_degree 1 -dm_plex_box_bd periodic,none -dm_view ::ascii_info_detail 2394e2e9504SJed Brown 2405f06a3ddSJed Brown testset: 2415dca41c3SJed Brown args: -dm_plex_simplex 0 -dm_plex_dim 2 -dm_plex_shape zbox -dm_plex_box_faces 3,2 -petscspace_degree 1 -dm_plex_box_bd none,periodic -dm_view ::ascii_info_detail -closure_tensor 2425f06a3ddSJed Brown nsize: 2 2435f06a3ddSJed Brown test: 2445f06a3ddSJed Brown suffix: 2d_sfc_periodic_stranded 2455f06a3ddSJed Brown args: -dm_distribute 0 2465f06a3ddSJed Brown test: 2475f06a3ddSJed Brown suffix: 2d_sfc_periodic_stranded_dist 2485f06a3ddSJed Brown args: -dm_distribute 1 -petscpartitioner_type simple 2495f06a3ddSJed Brown 25052e7713aSMatthew G. Knepley test: 25152e7713aSMatthew G. Knepley suffix: fv_0 25252e7713aSMatthew G. Knepley requires: triangle 25352e7713aSMatthew G. Knepley args: -dm_refine 1 -use_fe 0 -dm_view -offsets_view 25452e7713aSMatthew G. Knepley 25590d1c1a4SMatthew G. Knepley TEST*/ 256