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 /// Utility functions for setting up ADVECTION 10 11 #include "../navierstokes.h" 12 #include "../qfunctions/setupgeo.h" 13 #include "../qfunctions/advection.h" 14 15 PetscErrorCode NS_ADVECTION(ProblemData *problem, DM dm, void *ctx) { 16 WindType wind_type; 17 BubbleType bubble_type; 18 BubbleContinuityType bubble_continuity_type; 19 StabilizationType stab; 20 SetupContext setup_context; 21 User user = *(User *)ctx; 22 MPI_Comm comm = PETSC_COMM_WORLD; 23 PetscBool implicit; 24 PetscBool has_curr_time = PETSC_FALSE; 25 PetscInt ierr; 26 AdvectionContext advection_ctx; 27 CeedQFunctionContext advection_context; 28 29 PetscFunctionBeginUser; 30 ierr = PetscCalloc1(1, &setup_context); CHKERRQ(ierr); 31 ierr = PetscCalloc1(1, &advection_ctx); CHKERRQ(ierr); 32 33 // ------------------------------------------------------ 34 // SET UP ADVECTION 35 // ------------------------------------------------------ 36 problem->dim = 3; 37 problem->q_data_size_vol = 10; 38 problem->q_data_size_sur = 10; 39 problem->setup_vol.qfunction = Setup; 40 problem->setup_vol.qfunction_loc = Setup_loc; 41 problem->setup_sur.qfunction = SetupBoundary; 42 problem->setup_sur.qfunction_loc = SetupBoundary_loc; 43 problem->ics.qfunction = ICsAdvection; 44 problem->ics.qfunction_loc = ICsAdvection_loc; 45 problem->apply_vol_rhs.qfunction = Advection; 46 problem->apply_vol_rhs.qfunction_loc = Advection_loc; 47 problem->apply_vol_ifunction.qfunction = IFunction_Advection; 48 problem->apply_vol_ifunction.qfunction_loc = IFunction_Advection_loc; 49 problem->apply_inflow.qfunction = Advection_InOutFlow; 50 problem->apply_inflow.qfunction_loc = Advection_InOutFlow_loc; 51 problem->bc = Exact_Advection; 52 problem->bc_ctx = setup_context; 53 problem->non_zero_time = PETSC_FALSE; 54 problem->print_info = PRINT_ADVECTION; 55 56 // ------------------------------------------------------ 57 // Create the libCEED context 58 // ------------------------------------------------------ 59 CeedScalar rc = 1000.; // m (Radius of bubble) 60 CeedScalar CtauS = 0.; // dimensionless 61 CeedScalar strong_form = 0.; // [0,1] 62 CeedScalar E_wind = 1.e6; // J 63 PetscReal wind[3] = {1., 0, 0}; // m/s 64 PetscReal domain_min[3], domain_max[3], domain_size[3]; 65 ierr = DMGetBoundingBox(dm, domain_min, domain_max); CHKERRQ(ierr); 66 for (PetscInt i=0; i<3; i++) domain_size[i] = domain_max[i] - domain_min[i]; 67 68 69 // ------------------------------------------------------ 70 // Create the PETSc context 71 // ------------------------------------------------------ 72 PetscScalar meter = 1e-2; // 1 meter in scaled length units 73 PetscScalar kilogram = 1e-6; // 1 kilogram in scaled mass units 74 PetscScalar second = 1e-2; // 1 second in scaled time units 75 PetscScalar Joule; 76 77 // ------------------------------------------------------ 78 // Command line Options 79 // ------------------------------------------------------ 80 PetscOptionsBegin(comm, NULL, "Options for ADVECTION problem", NULL); 81 // -- Physics 82 ierr = PetscOptionsScalar("-rc", "Characteristic radius of thermal bubble", 83 NULL, rc, &rc, NULL); CHKERRQ(ierr); 84 PetscBool translation; 85 ierr = PetscOptionsEnum("-wind_type", "Wind type in Advection", 86 NULL, WindTypes, 87 (PetscEnum)(wind_type = WIND_ROTATION), 88 (PetscEnum *)&wind_type, &translation); CHKERRQ(ierr); 89 if (translation) user->phys->has_neumann = PETSC_TRUE; 90 PetscInt n = problem->dim; 91 PetscBool user_wind; 92 ierr = PetscOptionsRealArray("-wind_translation", "Constant wind vector", 93 NULL, wind, &n, &user_wind); CHKERRQ(ierr); 94 ierr = PetscOptionsScalar("-CtauS", 95 "Scale coefficient for tau (nondimensional)", 96 NULL, CtauS, &CtauS, NULL); CHKERRQ(ierr); 97 ierr = PetscOptionsScalar("-strong_form", 98 "Strong (1) or weak/integrated by parts (0) advection residual", 99 NULL, strong_form, &strong_form, NULL); CHKERRQ(ierr); 100 ierr = PetscOptionsScalar("-E_wind", "Total energy of inflow wind", 101 NULL, E_wind, &E_wind, NULL); CHKERRQ(ierr); 102 ierr = PetscOptionsEnum("-bubble_type", "Sphere (3D) or cylinder (2D)", 103 NULL, BubbleTypes, 104 (PetscEnum)(bubble_type = BUBBLE_SPHERE), 105 (PetscEnum *)&bubble_type, NULL); CHKERRQ(ierr); 106 ierr = PetscOptionsEnum("-bubble_continuity", "Smooth, back_sharp, or thick", 107 NULL, BubbleContinuityTypes, 108 (PetscEnum)(bubble_continuity_type = BUBBLE_CONTINUITY_SMOOTH), 109 (PetscEnum *)&bubble_continuity_type, NULL); CHKERRQ(ierr); 110 ierr = PetscOptionsEnum("-stab", "Stabilization method", NULL, 111 StabilizationTypes, (PetscEnum)(stab = STAB_NONE), 112 (PetscEnum *)&stab, NULL); CHKERRQ(ierr); 113 ierr = PetscOptionsBool("-implicit", "Use implicit (IFunction) formulation", 114 NULL, implicit=PETSC_FALSE, &implicit, NULL); 115 CHKERRQ(ierr); 116 117 // -- Units 118 ierr = PetscOptionsScalar("-units_meter", "1 meter in scaled length units", 119 NULL, meter, &meter, NULL); CHKERRQ(ierr); 120 meter = fabs(meter); 121 ierr = PetscOptionsScalar("-units_kilogram","1 kilogram in scaled mass units", 122 NULL, kilogram, &kilogram, NULL); CHKERRQ(ierr); 123 kilogram = fabs(kilogram); 124 ierr = PetscOptionsScalar("-units_second","1 second in scaled time units", 125 NULL, second, &second, NULL); CHKERRQ(ierr); 126 second = fabs(second); 127 128 // -- Warnings 129 if (wind_type == WIND_ROTATION && user_wind) { 130 ierr = PetscPrintf(comm, 131 "Warning! Use -wind_translation only with -wind_type translation\n"); 132 CHKERRQ(ierr); 133 } 134 if (wind_type == WIND_TRANSLATION 135 && bubble_type == BUBBLE_CYLINDER && wind[2] != 0.) { 136 wind[2] = 0; 137 ierr = PetscPrintf(comm, 138 "Warning! Background wind in the z direction should be zero (-wind_translation x,x,0) with -bubble_type cylinder\n"); 139 CHKERRQ(ierr); 140 } 141 if (stab == STAB_NONE && CtauS != 0) { 142 ierr = PetscPrintf(comm, 143 "Warning! Use -CtauS only with -stab su or -stab supg\n"); 144 CHKERRQ(ierr); 145 } 146 if (stab == STAB_SUPG && !implicit) { 147 ierr = PetscPrintf(comm, 148 "Warning! Use -stab supg only with -implicit\n"); 149 CHKERRQ(ierr); 150 } 151 152 PetscOptionsEnd(); 153 154 // ------------------------------------------------------ 155 // Set up the PETSc context 156 // ------------------------------------------------------ 157 // -- Define derived units 158 Joule = kilogram * PetscSqr(meter) / PetscSqr(second); 159 160 user->units->meter = meter; 161 user->units->kilogram = kilogram; 162 user->units->second = second; 163 user->units->Joule = Joule; 164 165 // ------------------------------------------------------ 166 // Set up the libCEED context 167 // ------------------------------------------------------ 168 // -- Scale variables to desired units 169 E_wind *= Joule; 170 rc = fabs(rc) * meter; 171 for (PetscInt i=0; i<3; i++) { 172 wind[i] *= (meter/second); 173 domain_size[i] *= meter; 174 } 175 problem->dm_scale = meter; 176 177 // -- Setup Context 178 setup_context->rc = rc; 179 setup_context->lx = domain_size[0]; 180 setup_context->ly = domain_size[1]; 181 setup_context->lz = domain_size[2]; 182 setup_context->wind[0] = wind[0]; 183 setup_context->wind[1] = wind[1]; 184 setup_context->wind[2] = wind[2]; 185 setup_context->wind_type = wind_type; 186 setup_context->bubble_type = bubble_type; 187 setup_context->bubble_continuity_type = bubble_continuity_type; 188 setup_context->time = 0; 189 190 // -- QFunction Context 191 user->phys->stab = stab; 192 user->phys->wind_type = wind_type; 193 user->phys->bubble_type = bubble_type; 194 user->phys->bubble_continuity_type = bubble_continuity_type; 195 // if passed correctly 196 user->phys->implicit = implicit; 197 user->phys->has_curr_time = has_curr_time; 198 advection_ctx->CtauS = CtauS; 199 advection_ctx->E_wind = E_wind; 200 advection_ctx->implicit = implicit; 201 advection_ctx->strong_form = strong_form; 202 advection_ctx->stabilization = stab; 203 204 CeedQFunctionContextCreate(user->ceed, &problem->ics.qfunction_context); 205 CeedQFunctionContextSetData(problem->ics.qfunction_context, CEED_MEM_HOST, 206 CEED_USE_POINTER, sizeof(*setup_context), setup_context); 207 208 CeedQFunctionContextCreate(user->ceed, &advection_context); 209 CeedQFunctionContextSetData(advection_context, CEED_MEM_HOST, 210 CEED_USE_POINTER, 211 sizeof(*advection_ctx), advection_ctx); 212 CeedQFunctionContextSetDataDestroy(advection_context, CEED_MEM_HOST, 213 FreeContextPetsc); 214 problem->apply_vol_rhs.qfunction_context = advection_context; 215 CeedQFunctionContextReferenceCopy(advection_context, 216 &problem->apply_vol_ifunction.qfunction_context); 217 CeedQFunctionContextReferenceCopy(advection_context, 218 &problem->apply_inflow.qfunction_context); 219 PetscFunctionReturn(0); 220 } 221 222 PetscErrorCode PRINT_ADVECTION(ProblemData *problem, AppCtx app_ctx) { 223 MPI_Comm comm = PETSC_COMM_WORLD; 224 PetscErrorCode ierr; 225 SetupContext setup_ctx; 226 AdvectionContext advection_ctx; 227 228 PetscFunctionBeginUser; 229 CeedQFunctionContextGetData(problem->ics.qfunction_context, 230 CEED_MEM_HOST, &setup_ctx); 231 CeedQFunctionContextGetData(problem->apply_vol_rhs.qfunction_context, 232 CEED_MEM_HOST, &advection_ctx); 233 ierr = PetscPrintf(comm, 234 " Problem:\n" 235 " Problem Name : %s\n" 236 " Stabilization : %s\n" 237 " Bubble Type : %s (%dD)\n" 238 " Bubble Continuity : %s\n" 239 " Wind Type : %s\n", 240 app_ctx->problem_name, StabilizationTypes[advection_ctx->stabilization], 241 BubbleTypes[setup_ctx->bubble_type], 242 setup_ctx->bubble_type == BUBBLE_SPHERE ? 3 : 2, 243 BubbleContinuityTypes[setup_ctx->bubble_continuity_type], 244 WindTypes[setup_ctx->wind_type]); CHKERRQ(ierr); 245 246 if (setup_ctx->wind_type == WIND_TRANSLATION) { 247 ierr = PetscPrintf(comm, 248 " Background Wind : %f,%f,%f\n", 249 setup_ctx->wind[0], setup_ctx->wind[1], setup_ctx->wind[2]); CHKERRQ(ierr); 250 } 251 CeedQFunctionContextRestoreData(problem->ics.qfunction_context, 252 &setup_ctx); 253 CeedQFunctionContextRestoreData(problem->apply_vol_rhs.qfunction_context, 254 &advection_ctx); 255 PetscFunctionReturn(0); 256 } 257