1 // Copyright (c) 2017-2024, 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 /// Utility functions for setting up SHOCKTUBE 10 11 #include "../qfunctions/shocktube.h" 12 13 #include <ceed.h> 14 #include <petscdm.h> 15 16 #include "../navierstokes.h" 17 #include "../qfunctions/setupgeo.h" 18 19 PetscErrorCode NS_SHOCKTUBE(ProblemData *problem, DM dm, void *ctx, SimpleBC bc) { 20 SetupContextShock setup_context; 21 User user = *(User *)ctx; 22 MPI_Comm comm = user->comm; 23 Ceed ceed = user->ceed; 24 PetscBool implicit; 25 PetscBool yzb; 26 PetscInt stab; 27 ShockTubeContext shocktube_ctx; 28 CeedQFunctionContext shocktube_context; 29 30 PetscFunctionBeginUser; 31 PetscCall(PetscCalloc1(1, &setup_context)); 32 PetscCall(PetscCalloc1(1, &shocktube_ctx)); 33 34 // ------------------------------------------------------ 35 // SET UP SHOCKTUBE 36 // ------------------------------------------------------ 37 problem->dim = 3; 38 problem->q_data_size_vol = 10; 39 problem->q_data_size_sur = 4; 40 problem->setup_vol.qfunction = Setup; 41 problem->setup_vol.qfunction_loc = Setup_loc; 42 problem->setup_sur.qfunction = SetupBoundary; 43 problem->setup_sur.qfunction_loc = SetupBoundary_loc; 44 problem->ics.qfunction = ICsShockTube; 45 problem->ics.qfunction_loc = ICsShockTube_loc; 46 problem->apply_vol_rhs.qfunction = EulerShockTube; 47 problem->apply_vol_rhs.qfunction_loc = EulerShockTube_loc; 48 problem->apply_vol_ifunction.qfunction = NULL; 49 problem->apply_vol_ifunction.qfunction_loc = NULL; 50 problem->non_zero_time = PETSC_FALSE; 51 problem->print_info = PRINT_SHOCKTUBE; 52 53 // ------------------------------------------------------ 54 // Create the libCEED context 55 // ------------------------------------------------------ 56 // Driver section initial conditions 57 CeedScalar P_high = 1.0; // Pa 58 CeedScalar rho_high = 1.0; // kg/m^3 59 // Driven section initial conditions 60 CeedScalar P_low = 0.1; // Pa 61 CeedScalar rho_low = 0.125; // kg/m^3 62 // Stabilization parameter 63 CeedScalar c_tau = 0.5; // -, based on Hughes et al (2010) 64 // Tuning parameters for the YZB shock capturing 65 CeedScalar Cyzb = 0.1; // -, used in approximation of (Na),x 66 CeedScalar Byzb = 2.0; // -, 1 for smooth shocks 67 // 2 for sharp shocks 68 PetscReal domain_min[3], domain_max[3], domain_size[3]; 69 PetscCall(DMGetBoundingBox(dm, domain_min, domain_max)); 70 for (PetscInt i = 0; i < 3; i++) domain_size[i] = domain_max[i] - domain_min[i]; 71 72 // ------------------------------------------------------ 73 // Create the PETSc context 74 // ------------------------------------------------------ 75 PetscScalar meter = 1e-2; // 1 meter in scaled length units 76 PetscScalar second = 1e-2; // 1 second in scaled time units 77 78 // ------------------------------------------------------ 79 // Command line Options 80 // ------------------------------------------------------ 81 PetscOptionsBegin(comm, NULL, "Options for SHOCKTUBE problem", NULL); 82 83 // -- Numerical formulation options 84 PetscCall(PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", NULL, implicit = PETSC_FALSE, &implicit, NULL)); 85 PetscCall(PetscOptionsEnum("-stab", "Stabilization method", NULL, StabilizationTypes, (PetscEnum)(stab = STAB_NONE), (PetscEnum *)&stab, NULL)); 86 PetscCall(PetscOptionsScalar("-c_tau", "Stabilization constant", NULL, c_tau, &c_tau, NULL)); 87 PetscCall(PetscOptionsBool("-yzb", "Use YZB discontinuity capturing", NULL, yzb = PETSC_FALSE, &yzb, NULL)); 88 89 // -- Units 90 PetscCall(PetscOptionsScalar("-units_meter", "1 meter in scaled length units", NULL, meter, &meter, NULL)); 91 meter = fabs(meter); 92 PetscCall(PetscOptionsScalar("-units_second", "1 second in scaled time units", NULL, second, &second, NULL)); 93 second = fabs(second); 94 95 // -- Warnings 96 if (stab == STAB_SUPG) { 97 PetscCall(PetscPrintf(comm, "Warning! -stab supg not implemented for the shocktube problem. \n")); 98 } 99 if (yzb && implicit) { 100 PetscCall(PetscPrintf(comm, "Warning! -yzb only implemented for explicit timestepping. \n")); 101 } 102 103 PetscOptionsEnd(); 104 105 // ------------------------------------------------------ 106 // Set up the PETSc context 107 // ------------------------------------------------------ 108 user->units->meter = meter; 109 user->units->second = second; 110 111 // ------------------------------------------------------ 112 // Set up the libCEED context 113 // ------------------------------------------------------ 114 // -- Scale variables to desired units 115 for (PetscInt i = 0; i < 3; i++) { 116 domain_size[i] *= meter; 117 domain_min[i] *= meter; 118 } 119 problem->dm_scale = meter; 120 CeedScalar mid_point = 0.5 * (domain_size[0] + domain_min[0]); 121 122 // -- Setup Context 123 setup_context->mid_point = mid_point; 124 setup_context->time = 0.0; 125 setup_context->P_high = P_high; 126 setup_context->rho_high = rho_high; 127 setup_context->P_low = P_low; 128 setup_context->rho_low = rho_low; 129 130 // -- QFunction Context 131 user->phys->implicit = implicit; 132 shocktube_ctx->implicit = implicit; 133 shocktube_ctx->stabilization = stab; 134 shocktube_ctx->yzb = yzb; 135 shocktube_ctx->Cyzb = Cyzb; 136 shocktube_ctx->Byzb = Byzb; 137 shocktube_ctx->c_tau = c_tau; 138 139 PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context)); 140 PetscCallCeed(ceed, 141 CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*setup_context), setup_context)); 142 PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(problem->ics.qfunction_context, CEED_MEM_HOST, FreeContextPetsc)); 143 144 PetscCallCeed(ceed, CeedQFunctionContextCreate(user->ceed, &shocktube_context)); 145 PetscCallCeed(ceed, CeedQFunctionContextSetData(shocktube_context, CEED_MEM_HOST, CEED_USE_POINTER, sizeof(*shocktube_ctx), shocktube_ctx)); 146 PetscCallCeed(ceed, CeedQFunctionContextSetDataDestroy(shocktube_context, CEED_MEM_HOST, FreeContextPetsc)); 147 problem->apply_vol_rhs.qfunction_context = shocktube_context; 148 PetscFunctionReturn(PETSC_SUCCESS); 149 } 150 151 PetscErrorCode PRINT_SHOCKTUBE(User user, ProblemData *problem, AppCtx app_ctx) { 152 MPI_Comm comm = user->comm; 153 154 PetscFunctionBeginUser; 155 PetscCall(PetscPrintf(comm, 156 " Problem:\n" 157 " Problem Name : %s\n", 158 app_ctx->problem_name)); 159 PetscFunctionReturn(PETSC_SUCCESS); 160 } 161