1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3a515125bSLeila Ghaffari // 4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause 5a515125bSLeila Ghaffari // 6727da7e7SJeremy L Thompson // This file is part of CEED: http://github.com/ceed 7a515125bSLeila Ghaffari 8a515125bSLeila Ghaffari /// @file 9a515125bSLeila Ghaffari /// Setup DM for Navier-Stokes example using PETSc 10a515125bSLeila Ghaffari 11e419654dSJeremy L Thompson #include <ceed.h> 12e419654dSJeremy L Thompson #include <petscdmplex.h> 13*67263decSKenneth E. Jansen #include <petscds.h> 14e419654dSJeremy L Thompson 15a515125bSLeila Ghaffari #include "../navierstokes.h" 16866f23abSJames Wright #include "../problems/stg_shur14.h" 17a515125bSLeila Ghaffari 1805a512bdSLeila Ghaffari // Create mesh 192b916ea7SJeremy L Thompson PetscErrorCode CreateDM(MPI_Comm comm, ProblemData *problem, MatType mat_type, VecType vec_type, DM *dm) { 20a515125bSLeila Ghaffari PetscFunctionBeginUser; 2105a512bdSLeila Ghaffari // Create DMPLEX 222b916ea7SJeremy L Thompson PetscCall(DMCreate(comm, dm)); 232b916ea7SJeremy L Thompson PetscCall(DMSetType(*dm, DMPLEX)); 24b150a244SJed Brown { 25b150a244SJed Brown PetscBool skip = PETSC_TRUE; 262b916ea7SJeremy L Thompson PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_mat_preallocate_skip", &skip, NULL)); 27b150a244SJed Brown PetscCall(DMSetMatrixPreallocateSkip(*dm, skip)); 28b150a244SJed Brown } 292b916ea7SJeremy L Thompson PetscCall(DMSetMatType(*dm, mat_type)); 302b916ea7SJeremy L Thompson PetscCall(DMSetVecType(*dm, vec_type)); 31a9804fe9SJed Brown 3205a512bdSLeila Ghaffari // Set Tensor elements 332b916ea7SJeremy L Thompson PetscCall(PetscOptionsSetValue(NULL, "-dm_plex_simplex", "0")); 342b916ea7SJeremy L Thompson PetscCall(PetscOptionsSetValue(NULL, "-dm_sparse_localize", "0")); 3505a512bdSLeila Ghaffari // Set CL options 362b916ea7SJeremy L Thompson PetscCall(DMSetFromOptions(*dm)); 372b916ea7SJeremy L Thompson PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view")); 38d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 39a515125bSLeila Ghaffari } 40a515125bSLeila Ghaffari 41a515125bSLeila Ghaffari // Setup DM 42*67263decSKenneth E. Jansen PetscErrorCode SetUpDM(DM dm, ProblemData *problem, PetscInt degree, PetscInt q_extra, SimpleBC bc, Physics phys) { 43*67263decSKenneth E. Jansen PetscInt q_order = degree + q_extra; 44a515125bSLeila Ghaffari PetscFunctionBeginUser; 45a515125bSLeila Ghaffari { 46*67263decSKenneth E. Jansen PetscBool is_simplex = PETSC_TRUE; 47*67263decSKenneth E. Jansen 48*67263decSKenneth E. Jansen // Check if simplex or tensor-product mesh 49*67263decSKenneth E. Jansen PetscCall(DMPlexIsSimplex(dm, &is_simplex)); 50a515125bSLeila Ghaffari // Configure the finite element space and boundary conditions 51a515125bSLeila Ghaffari PetscFE fe; 52a515125bSLeila Ghaffari PetscInt num_comp_q = 5; 53047c2946SJed Brown DMLabel label; 54*67263decSKenneth E. Jansen PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, num_comp_q, is_simplex, degree, q_order, &fe)); 552b916ea7SJeremy L Thompson PetscCall(PetscObjectSetName((PetscObject)fe, "Q")); 562b916ea7SJeremy L Thompson PetscCall(DMAddField(dm, NULL, (PetscObject)fe)); 572b916ea7SJeremy L Thompson PetscCall(DMCreateDS(dm)); 58*67263decSKenneth E. Jansen { // Project coordinates to enrich quadrature space 59*67263decSKenneth E. Jansen DM dm_coord; 60*67263decSKenneth E. Jansen PetscDS ds_coord; 61*67263decSKenneth E. Jansen PetscFE fe_coord_current, fe_coord_new, fe_coord_face_new; 62*67263decSKenneth E. Jansen PetscDualSpace fe_coord_dual_space; 63*67263decSKenneth E. Jansen PetscInt fe_coord_order, num_comp_coord; 64*67263decSKenneth E. Jansen 65*67263decSKenneth E. Jansen PetscCall(DMGetCoordinateDM(dm, &dm_coord)); 66*67263decSKenneth E. Jansen PetscCall(DMGetCoordinateDim(dm, &num_comp_coord)); 67*67263decSKenneth E. Jansen PetscCall(DMGetRegionDS(dm_coord, NULL, NULL, &ds_coord, NULL)); 68*67263decSKenneth E. Jansen PetscCall(PetscDSGetDiscretization(ds_coord, 0, (PetscObject *)&fe_coord_current)); 69*67263decSKenneth E. Jansen PetscCall(PetscFEGetDualSpace(fe_coord_current, &fe_coord_dual_space)); 70*67263decSKenneth E. Jansen PetscCall(PetscDualSpaceGetOrder(fe_coord_dual_space, &fe_coord_order)); 71*67263decSKenneth E. Jansen 72*67263decSKenneth E. Jansen // Create FE for coordinates 73*67263decSKenneth E. Jansen PetscCheck(fe_coord_order == 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_USER_INPUT, 74*67263decSKenneth E. Jansen "Only linear mesh geometry supported. Recieved %d order", fe_coord_order); 75*67263decSKenneth E. Jansen PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, num_comp_coord, is_simplex, fe_coord_order, q_order, &fe_coord_new)); 76*67263decSKenneth E. Jansen PetscCall(PetscFEGetHeightSubspace(fe_coord_new, 1, &fe_coord_face_new)); 77*67263decSKenneth E. Jansen PetscCall(DMProjectCoordinates(dm, fe_coord_new)); 78*67263decSKenneth E. Jansen PetscCall(PetscFEDestroy(&fe_coord_new)); 79*67263decSKenneth E. Jansen } 80*67263decSKenneth E. Jansen 812b916ea7SJeremy L Thompson PetscCall(DMGetLabel(dm, "Face Sets", &label)); 82c5e9980aSAdeleke O. Bankole PetscCall(DMPlexLabelComplete(dm, label)); 83002797a3SLeila Ghaffari // Set wall BCs 84002797a3SLeila Ghaffari if (bc->num_wall > 0) { 85bedfd28dSJames Wright PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, bc->num_wall, bc->walls, 0, bc->num_comps, bc->wall_comps, NULL, NULL, NULL, NULL)); 86002797a3SLeila Ghaffari } 87002797a3SLeila Ghaffari // Set slip BCs in the x direction 88002797a3SLeila Ghaffari if (bc->num_slip[0] > 0) { 89002797a3SLeila Ghaffari PetscInt comps[1] = {1}; 90bedfd28dSJames Wright PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", label, bc->num_slip[0], bc->slips[0], 0, 1, comps, NULL, NULL, NULL, NULL)); 91002797a3SLeila Ghaffari } 92002797a3SLeila Ghaffari // Set slip BCs in the y direction 93002797a3SLeila Ghaffari if (bc->num_slip[1] > 0) { 94002797a3SLeila Ghaffari PetscInt comps[1] = {2}; 95bedfd28dSJames Wright PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", label, bc->num_slip[1], bc->slips[1], 0, 1, comps, NULL, NULL, NULL, NULL)); 96002797a3SLeila Ghaffari } 97002797a3SLeila Ghaffari // Set slip BCs in the z direction 98002797a3SLeila Ghaffari if (bc->num_slip[2] > 0) { 99002797a3SLeila Ghaffari PetscInt comps[1] = {3}; 100bedfd28dSJames Wright PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label, bc->num_slip[2], bc->slips[2], 0, 1, comps, NULL, NULL, NULL, NULL)); 101002797a3SLeila Ghaffari } 102866f23abSJames Wright { 103866f23abSJames Wright PetscBool use_strongstg = PETSC_FALSE; 1042b916ea7SJeremy L Thompson PetscCall(PetscOptionsGetBool(NULL, NULL, "-stg_strong", &use_strongstg, NULL)); 10591933550SJames Wright if (use_strongstg) PetscCall(SetupStrongSTG(dm, bc, problem, phys)); 106866f23abSJames Wright } 107866f23abSJames Wright 108*67263decSKenneth E. Jansen if (!is_simplex) { 109*67263decSKenneth E. Jansen DM dm_coord; 110*67263decSKenneth E. Jansen PetscCall(DMGetCoordinateDM(dm, &dm_coord)); 1112b916ea7SJeremy L Thompson PetscCall(DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL)); 112*67263decSKenneth E. Jansen PetscCall(DMPlexSetClosurePermutationTensor(dm_coord, PETSC_DETERMINE, NULL)); 113*67263decSKenneth E. Jansen } 114*67263decSKenneth E. Jansen 1152b916ea7SJeremy L Thompson PetscCall(PetscFEDestroy(&fe)); 116a515125bSLeila Ghaffari } 117cbe60e31SLeila Ghaffari 118a515125bSLeila Ghaffari // Empty name for conserved field (because there is only one field) 119a515125bSLeila Ghaffari PetscSection section; 1202b916ea7SJeremy L Thompson PetscCall(DMGetLocalSection(dm, §ion)); 1212b916ea7SJeremy L Thompson PetscCall(PetscSectionSetFieldName(section, 0, "")); 1223636f6a4SJames Wright switch (phys->state_var) { 1233636f6a4SJames Wright case STATEVAR_CONSERVATIVE: 1242b916ea7SJeremy L Thompson PetscCall(PetscSectionSetComponentName(section, 0, 0, "Density")); 1252b916ea7SJeremy L Thompson PetscCall(PetscSectionSetComponentName(section, 0, 1, "Momentum X")); 1262b916ea7SJeremy L Thompson PetscCall(PetscSectionSetComponentName(section, 0, 2, "Momentum Y")); 1272b916ea7SJeremy L Thompson PetscCall(PetscSectionSetComponentName(section, 0, 3, "Momentum Z")); 1282b916ea7SJeremy L Thompson PetscCall(PetscSectionSetComponentName(section, 0, 4, "Energy Density")); 1293636f6a4SJames Wright break; 1303636f6a4SJames Wright 1313636f6a4SJames Wright case STATEVAR_PRIMITIVE: 1322b916ea7SJeremy L Thompson PetscCall(PetscSectionSetComponentName(section, 0, 0, "Pressure")); 1332b916ea7SJeremy L Thompson PetscCall(PetscSectionSetComponentName(section, 0, 1, "Velocity X")); 1342b916ea7SJeremy L Thompson PetscCall(PetscSectionSetComponentName(section, 0, 2, "Velocity Y")); 1352b916ea7SJeremy L Thompson PetscCall(PetscSectionSetComponentName(section, 0, 3, "Velocity Z")); 1362b916ea7SJeremy L Thompson PetscCall(PetscSectionSetComponentName(section, 0, 4, "Temperature")); 1373636f6a4SJames Wright break; 138a515125bSLeila Ghaffari } 139d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 140a515125bSLeila Ghaffari } 141a515125bSLeila Ghaffari 142a515125bSLeila Ghaffari // Refine DM for high-order viz 1432b916ea7SJeremy L Thompson PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem, SimpleBC bc, Physics phys) { 144a515125bSLeila Ghaffari DM dm_hierarchy[user->app_ctx->viz_refine + 1]; 145a515125bSLeila Ghaffari VecType vec_type; 146a515125bSLeila Ghaffari PetscFunctionBeginUser; 147a515125bSLeila Ghaffari 1482b916ea7SJeremy L Thompson PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE)); 149a515125bSLeila Ghaffari 150a515125bSLeila Ghaffari dm_hierarchy[0] = dm; 1512b916ea7SJeremy L Thompson for (PetscInt i = 0, d = user->app_ctx->degree; i < user->app_ctx->viz_refine; i++) { 152a515125bSLeila Ghaffari Mat interp_next; 1532b916ea7SJeremy L Thompson PetscCall(DMRefine(dm_hierarchy[i], MPI_COMM_NULL, &dm_hierarchy[i + 1])); 1542b916ea7SJeremy L Thompson PetscCall(DMClearDS(dm_hierarchy[i + 1])); 1552b916ea7SJeremy L Thompson PetscCall(DMClearFields(dm_hierarchy[i + 1])); 1562b916ea7SJeremy L Thompson PetscCall(DMSetCoarseDM(dm_hierarchy[i + 1], dm_hierarchy[i])); 157a515125bSLeila Ghaffari d = (d + 1) / 2; 158*67263decSKenneth E. Jansen PetscInt q_order = d + user->app_ctx->q_extra; 159a515125bSLeila Ghaffari if (i + 1 == user->app_ctx->viz_refine) d = 1; 1602b916ea7SJeremy L Thompson PetscCall(DMGetVecType(dm, &vec_type)); 1612b916ea7SJeremy L Thompson PetscCall(DMSetVecType(dm_hierarchy[i + 1], vec_type)); 162*67263decSKenneth E. Jansen PetscCall(SetUpDM(dm_hierarchy[i + 1], problem, d, q_order, bc, phys)); 1632b916ea7SJeremy L Thompson PetscCall(DMCreateInterpolation(dm_hierarchy[i], dm_hierarchy[i + 1], &interp_next, NULL)); 164a515125bSLeila Ghaffari if (!i) user->interp_viz = interp_next; 165a515125bSLeila Ghaffari else { 166a515125bSLeila Ghaffari Mat C; 1672b916ea7SJeremy L Thompson PetscCall(MatMatMult(interp_next, user->interp_viz, MAT_INITIAL_MATRIX, PETSC_DECIDE, &C)); 1682b916ea7SJeremy L Thompson PetscCall(MatDestroy(&interp_next)); 1692b916ea7SJeremy L Thompson PetscCall(MatDestroy(&user->interp_viz)); 170a515125bSLeila Ghaffari user->interp_viz = C; 171a515125bSLeila Ghaffari } 172a515125bSLeila Ghaffari } 173a515125bSLeila Ghaffari for (PetscInt i = 1; i < user->app_ctx->viz_refine; i++) { 1742b916ea7SJeremy L Thompson PetscCall(DMDestroy(&dm_hierarchy[i])); 175a515125bSLeila Ghaffari } 176a515125bSLeila Ghaffari user->dm_viz = dm_hierarchy[user->app_ctx->viz_refine]; 177a515125bSLeila Ghaffari 178d949ddfcSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 179a515125bSLeila Ghaffari } 180