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); 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 ierr = PetscSectionSetComponentName(section, 0, 0, "Density"); 106 CHKERRQ(ierr); 107 ierr = PetscSectionSetComponentName(section, 0, 1, "Momentum X"); 108 CHKERRQ(ierr); 109 ierr = PetscSectionSetComponentName(section, 0, 2, "Momentum Y"); 110 CHKERRQ(ierr); 111 ierr = PetscSectionSetComponentName(section, 0, 3, "Momentum Z"); 112 CHKERRQ(ierr); 113 ierr = PetscSectionSetComponentName(section, 0, 4, "Energy Density"); 114 CHKERRQ(ierr); 115 } 116 PetscFunctionReturn(0); 117 } 118 119 // Refine DM for high-order viz 120 PetscErrorCode VizRefineDM(DM dm, User user, ProblemData *problem, 121 SimpleBC bc, Physics phys) { 122 PetscErrorCode ierr; 123 DM dm_hierarchy[user->app_ctx->viz_refine + 1]; 124 VecType vec_type; 125 PetscFunctionBeginUser; 126 127 ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE); CHKERRQ(ierr); 128 129 dm_hierarchy[0] = dm; 130 for (PetscInt i = 0, d = user->app_ctx->degree; 131 i < user->app_ctx->viz_refine; i++) { 132 Mat interp_next; 133 ierr = DMRefine(dm_hierarchy[i], MPI_COMM_NULL, &dm_hierarchy[i+1]); 134 CHKERRQ(ierr); 135 ierr = DMClearDS(dm_hierarchy[i+1]); CHKERRQ(ierr); 136 ierr = DMClearFields(dm_hierarchy[i+1]); CHKERRQ(ierr); 137 ierr = DMSetCoarseDM(dm_hierarchy[i+1], dm_hierarchy[i]); CHKERRQ(ierr); 138 d = (d + 1) / 2; 139 if (i + 1 == user->app_ctx->viz_refine) d = 1; 140 ierr = DMGetVecType(dm, &vec_type); CHKERRQ(ierr); 141 ierr = DMSetVecType(dm_hierarchy[i+1], vec_type); CHKERRQ(ierr); 142 ierr = SetUpDM(dm_hierarchy[i+1], problem, d, bc, phys); 143 CHKERRQ(ierr); 144 ierr = DMCreateInterpolation(dm_hierarchy[i], dm_hierarchy[i+1], &interp_next, 145 NULL); CHKERRQ(ierr); 146 if (!i) user->interp_viz = interp_next; 147 else { 148 Mat C; 149 ierr = MatMatMult(interp_next, user->interp_viz, MAT_INITIAL_MATRIX, 150 PETSC_DECIDE, &C); CHKERRQ(ierr); 151 ierr = MatDestroy(&interp_next); CHKERRQ(ierr); 152 ierr = MatDestroy(&user->interp_viz); CHKERRQ(ierr); 153 user->interp_viz = C; 154 } 155 } 156 for (PetscInt i=1; i<user->app_ctx->viz_refine; i++) { 157 ierr = DMDestroy(&dm_hierarchy[i]); CHKERRQ(ierr); 158 } 159 user->dm_viz = dm_hierarchy[user->app_ctx->viz_refine]; 160 161 PetscFunctionReturn(0); 162 } 163