1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3 // 4 // SPDX-License-Identifier: BSD-2-Clause 5 // 6 // This file is part of CEED: http://github.com/ceed 7 8 /// @file 9 /// Setup DM for Navier-Stokes example using PETSc 10 11 #include "../navierstokes.h" 12 #include "../problems/stg_shur14.h" 13 14 // Create mesh 15 PetscErrorCode CreateDM(MPI_Comm comm, ProblemData *problem, 16 MatType mat_type, VecType vec_type, 17 DM *dm) { 18 PetscErrorCode ierr; 19 PetscFunctionBeginUser; 20 // Create DMPLEX 21 ierr = DMCreate(comm, dm); CHKERRQ(ierr); 22 ierr = DMSetType(*dm, DMPLEX); CHKERRQ(ierr); 23 { 24 PetscBool skip = PETSC_TRUE; 25 PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_mat_preallocate_skip", &skip, 26 NULL)); 27 PetscCall(DMSetMatrixPreallocateSkip(*dm, skip)); 28 } 29 ierr = DMSetMatType(*dm, mat_type); CHKERRQ(ierr); 30 ierr = DMSetVecType(*dm, vec_type); CHKERRQ(ierr); 31 32 // Set Tensor elements 33 ierr = PetscOptionsSetValue(NULL, "-dm_plex_simplex", "0"); CHKERRQ(ierr); 34 ierr = PetscOptionsSetValue(NULL, "-dm_sparse_localize", "0"); CHKERRQ(ierr); 35 // Set CL options 36 ierr = DMSetFromOptions(*dm); CHKERRQ(ierr); 37 ierr = DMViewFromOptions(*dm, NULL, "-dm_view"); CHKERRQ(ierr); 38 PetscFunctionReturn(0); 39 } 40 41 // Setup DM 42 PetscErrorCode SetUpDM(DM dm, ProblemData *problem, PetscInt degree, 43 SimpleBC bc, Physics phys) { 44 PetscErrorCode ierr; 45 PetscFunctionBeginUser; 46 { 47 // Configure the finite element space and boundary conditions 48 PetscFE fe; 49 PetscInt num_comp_q = 5; 50 DMLabel label; 51 ierr = PetscFECreateLagrange(PETSC_COMM_SELF, problem->dim, num_comp_q, 52 PETSC_FALSE, degree, PETSC_DECIDE, 53 &fe); CHKERRQ(ierr); 54 ierr = PetscObjectSetName((PetscObject)fe, "Q"); CHKERRQ(ierr); 55 ierr = DMAddField(dm, NULL,(PetscObject)fe); CHKERRQ(ierr); 56 ierr = DMCreateDS(dm); CHKERRQ(ierr); 57 ierr = DMGetLabel(dm, "Face Sets", &label); CHKERRQ(ierr); 58 // Set wall BCs 59 if (bc->num_wall > 0) { 60 ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 61 bc->num_wall, bc->walls, 0, bc->num_comps, 62 bc->wall_comps, (void(*)(void))problem->bc, 63 NULL, problem->bc_ctx, NULL); CHKERRQ(ierr); 64 } 65 // Set slip BCs in the x direction 66 if (bc->num_slip[0] > 0) { 67 PetscInt comps[1] = {1}; 68 ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipx", label, 69 bc->num_slip[0], bc->slips[0], 0, 1, comps, 70 (void(*)(void))NULL, NULL, problem->bc_ctx, NULL); CHKERRQ(ierr); 71 } 72 // Set slip BCs in the y direction 73 if (bc->num_slip[1] > 0) { 74 PetscInt comps[1] = {2}; 75 ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipy", label, 76 bc->num_slip[1], bc->slips[1], 0, 1, comps, 77 (void(*)(void))NULL, NULL, problem->bc_ctx, NULL); CHKERRQ(ierr); 78 } 79 // Set slip BCs in the z direction 80 if (bc->num_slip[2] > 0) { 81 PetscInt comps[1] = {3}; 82 ierr = DMAddBoundary(dm, DM_BC_ESSENTIAL, "slipz", label, 83 bc->num_slip[2], bc->slips[2], 0, 1, comps, 84 (void(*)(void))NULL, NULL, problem->bc_ctx, NULL); CHKERRQ(ierr); 85 } 86 { 87 PetscBool use_strongstg = PETSC_FALSE; 88 ierr = PetscOptionsGetBool(NULL, NULL, "-stg_strong", &use_strongstg, NULL); 89 CHKERRQ(ierr); 90 91 if (use_strongstg) { 92 ierr = SetupStrongSTG(dm, bc, problem, phys); CHKERRQ(ierr); 93 } 94 } 95 96 ierr = DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL); 97 CHKERRQ(ierr); 98 ierr = PetscFEDestroy(&fe); CHKERRQ(ierr); 99 } 100 101 // Empty name for conserved field (because there is only one field) 102 PetscSection section; 103 ierr = DMGetLocalSection(dm, §ion); CHKERRQ(ierr); 104 ierr = PetscSectionSetFieldName(section, 0, ""); CHKERRQ(ierr); 105 if (phys->primitive) { 106 ierr = PetscSectionSetComponentName(section, 0, 0, "Pressure"); 107 CHKERRQ(ierr); 108 ierr = PetscSectionSetComponentName(section, 0, 1, "Velocity X"); 109 CHKERRQ(ierr); 110 ierr = PetscSectionSetComponentName(section, 0, 2, "Velocity Y"); 111 CHKERRQ(ierr); 112 ierr = PetscSectionSetComponentName(section, 0, 3, "Velocity Z"); 113 CHKERRQ(ierr); 114 ierr = PetscSectionSetComponentName(section, 0, 4, "Temperature"); 115 CHKERRQ(ierr); 116 } else { 117 ierr = PetscSectionSetComponentName(section, 0, 0, "Density"); 118 CHKERRQ(ierr); 119 ierr = PetscSectionSetComponentName(section, 0, 1, "Momentum X"); 120 CHKERRQ(ierr); 121 ierr = PetscSectionSetComponentName(section, 0, 2, "Momentum Y"); 122 CHKERRQ(ierr); 123 ierr = PetscSectionSetComponentName(section, 0, 3, "Momentum Z"); 124 CHKERRQ(ierr); 125 ierr = PetscSectionSetComponentName(section, 0, 4, "Energy Density"); 126 CHKERRQ(ierr); 127 } 128 PetscFunctionReturn(0); 129 } 130 131 // Refine DM for high-order viz 132 PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem, 133 SimpleBC bc, Physics phys) { 134 PetscErrorCode ierr; 135 DM dm_hierarchy[user->app_ctx->viz_refine + 1]; 136 VecType vec_type; 137 PetscFunctionBeginUser; 138 139 ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE); CHKERRQ(ierr); 140 141 dm_hierarchy[0] = dm; 142 for (PetscInt i = 0, d = user->app_ctx->degree; 143 i < user->app_ctx->viz_refine; i++) { 144 Mat interp_next; 145 ierr = DMRefine(dm_hierarchy[i], MPI_COMM_NULL, &dm_hierarchy[i+1]); 146 CHKERRQ(ierr); 147 ierr = DMClearDS(dm_hierarchy[i+1]); CHKERRQ(ierr); 148 ierr = DMClearFields(dm_hierarchy[i+1]); CHKERRQ(ierr); 149 ierr = DMSetCoarseDM(dm_hierarchy[i+1], dm_hierarchy[i]); CHKERRQ(ierr); 150 d = (d + 1) / 2; 151 if (i + 1 == user->app_ctx->viz_refine) d = 1; 152 ierr = DMGetVecType(dm, &vec_type); CHKERRQ(ierr); 153 ierr = DMSetVecType(dm_hierarchy[i+1], vec_type); CHKERRQ(ierr); 154 ierr = SetUpDM(dm_hierarchy[i+1], problem, d, bc, phys); 155 CHKERRQ(ierr); 156 ierr = DMCreateInterpolation(dm_hierarchy[i], dm_hierarchy[i+1], &interp_next, 157 NULL); CHKERRQ(ierr); 158 if (!i) user->interp_viz = interp_next; 159 else { 160 Mat C; 161 ierr = MatMatMult(interp_next, user->interp_viz, MAT_INITIAL_MATRIX, 162 PETSC_DECIDE, &C); CHKERRQ(ierr); 163 ierr = MatDestroy(&interp_next); CHKERRQ(ierr); 164 ierr = MatDestroy(&user->interp_viz); CHKERRQ(ierr); 165 user->interp_viz = C; 166 } 167 } 168 for (PetscInt i=1; i<user->app_ctx->viz_refine; i++) { 169 ierr = DMDestroy(&dm_hierarchy[i]); CHKERRQ(ierr); 170 } 171 user->dm_viz = dm_hierarchy[user->app_ctx->viz_refine]; 172 173 PetscFunctionReturn(0); 174 } 175