1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors. 2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause 3 4 /// @file 5 /// Advection initial condition and operator for Navier-Stokes example using PETSc 6 #include <ceed.h> 7 #include <math.h> 8 9 #include "advection_types.h" 10 #include "newtonian_state.h" 11 #include "newtonian_types.h" 12 #include "stabilization_types.h" 13 #include "utils.h" 14 15 // ***************************************************************************** 16 // This QFunction sets the initial conditions and the boundary conditions 17 // for two test cases: ROTATION and TRANSLATION 18 // 19 // -- ROTATION (default) 20 // Initial Conditions: 21 // Mass Density: 22 // Constant mass density of 1.0 23 // Momentum Density: 24 // Rotational field in x,y 25 // Energy Density: 26 // Maximum of 1. x0 decreasing linearly to 0. as radial distance 27 // increases to (1.-r/rc), then 0. everywhere else 28 // 29 // Boundary Conditions: 30 // Mass Density: 31 // 0.0 flux 32 // Momentum Density: 33 // 0.0 34 // Energy Density: 35 // 0.0 flux 36 // 37 // -- TRANSLATION 38 // Initial Conditions: 39 // Mass Density: 40 // Constant mass density of 1.0 41 // Momentum Density: 42 // Constant rectilinear field in x,y 43 // Energy Density: 44 // Maximum of 1. x0 decreasing linearly to 0. as radial distance 45 // increases to (1.-r/rc), then 0. everywhere else 46 // 47 // Boundary Conditions: 48 // Mass Density: 49 // 0.0 flux 50 // Momentum Density: 51 // 0.0 52 // Energy Density: 53 // Inflow BCs: 54 // E = E_wind 55 // Outflow BCs: 56 // E = E(boundary) 57 // Both In/Outflow BCs for E are applied weakly in the 58 // QFunction "Advection2d_Sur" 59 // 60 // ***************************************************************************** 61 62 // ***************************************************************************** 63 // This helper function provides the exact, time-dependent solution and IC formulation for 2D advection 64 // ***************************************************************************** 65 CEED_QFUNCTION_HELPER CeedInt Exact_AdvectionGeneric(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) { 66 const SetupContextAdv context = (SetupContextAdv)ctx; 67 const CeedScalar rc = context->rc; 68 const CeedScalar lx = context->lx; 69 const CeedScalar ly = context->ly; 70 const CeedScalar lz = dim == 2 ? 0. : context->lz; 71 const CeedScalar *wind = context->wind; 72 73 const CeedScalar center[3] = {0.5 * lx, 0.5 * ly, 0.5 * lz}; 74 const CeedScalar theta = dim == 2 ? M_PI / 3 : M_PI; 75 const CeedScalar x0[3] = {center[0] + .25 * lx * cos(theta + time), center[1] + .25 * ly * sin(theta + time), 0.5 * lz}; 76 77 const CeedScalar x = X[0], y = X[1], z = dim == 2 ? 0. : X[2]; 78 79 CeedScalar r = 0.; 80 switch (context->initial_condition_type) { 81 case ADVECTIONIC_BUBBLE_SPHERE: 82 case ADVECTIONIC_BUBBLE_CYLINDER: 83 r = sqrt(Square(x - x0[0]) + Square(y - x0[1]) + Square(z - x0[2])); 84 break; 85 case ADVECTIONIC_COSINE_HILL: 86 r = sqrt(Square(x - center[0]) + Square(y - center[1])); 87 break; 88 case ADVECTIONIC_SKEW: 89 break; 90 } 91 92 switch (context->wind_type) { 93 case WIND_ROTATION: 94 q[0] = 1.; 95 q[1] = -(y - center[1]); 96 q[2] = (x - center[0]); 97 q[3] = 0; 98 break; 99 case WIND_TRANSLATION: 100 q[0] = 1.; 101 q[1] = wind[0]; 102 q[2] = wind[1]; 103 q[3] = dim == 2 ? 0. : wind[2]; 104 break; 105 default: 106 return 1; 107 } 108 109 switch (context->initial_condition_type) { 110 case ADVECTIONIC_BUBBLE_SPHERE: 111 case ADVECTIONIC_BUBBLE_CYLINDER: 112 switch (context->bubble_continuity_type) { 113 // original continuous, smooth shape 114 case BUBBLE_CONTINUITY_SMOOTH: 115 q[4] = r <= rc ? (1. - r / rc) : 0.; 116 break; 117 // discontinuous, sharp back half shape 118 case BUBBLE_CONTINUITY_BACK_SHARP: 119 q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) : 0.; 120 break; 121 // attempt to define a finite thickness that will get resolved under grid refinement 122 case BUBBLE_CONTINUITY_THICK: 123 q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) * fmin(1.0, (center[1] - y) / 1.25) : 0.; 124 break; 125 case BUBBLE_CONTINUITY_COSINE: 126 q[4] = r <= rc ? .5 + .5 * cos(r * M_PI / rc) : 0; 127 break; 128 } 129 break; 130 case ADVECTIONIC_COSINE_HILL: { 131 CeedScalar half_width = context->lx / 2; 132 q[4] = r > half_width ? 0. : cos(2 * M_PI * r / half_width + M_PI) + 1.; 133 } break; 134 case ADVECTIONIC_SKEW: { 135 CeedScalar skewed_barrier[3] = {wind[0], wind[1], 0}; 136 CeedScalar inflow_to_point[3] = {x - context->lx / 2, y, 0}; 137 CeedScalar cross_product[3] = {0}; 138 const CeedScalar boundary_threshold = 20 * CEED_EPSILON; 139 Cross3(skewed_barrier, inflow_to_point, cross_product); 140 141 q[4] = cross_product[2] > boundary_threshold ? 0 : 1; 142 if ((x < boundary_threshold && wind[0] < boundary_threshold) || // outflow at -x boundary 143 (y < boundary_threshold && wind[1] < boundary_threshold) || // outflow at -y boundary 144 (x > context->lx - boundary_threshold && wind[0] > boundary_threshold) || // outflow at +x boundary 145 (y > context->ly - boundary_threshold && wind[1] > boundary_threshold) // outflow at +y boundary 146 ) { 147 q[4] = 0; 148 } 149 } break; 150 } 151 return 0; 152 } 153 154 // ***************************************************************************** 155 // This QFunction sets the initial conditions for 3D advection 156 // ***************************************************************************** 157 CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 158 const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 159 CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 160 161 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 162 const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]}; 163 CeedScalar q[5] = {0.}; 164 165 Exact_AdvectionGeneric(3, 0., x, 5, q, ctx); 166 for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j]; 167 } 168 return 0; 169 } 170 171 // ***************************************************************************** 172 // This QFunction sets the initial conditions for 2D advection 173 // ***************************************************************************** 174 CEED_QFUNCTION(ICsAdvection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 175 const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 176 CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 177 const SetupContextAdv context = (SetupContextAdv)ctx; 178 179 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 180 const CeedScalar x[] = {X[0][i], X[1][i]}; 181 CeedScalar q[5] = {0.}; 182 183 Exact_AdvectionGeneric(2, context->time, x, 5, q, ctx); 184 for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j]; 185 } 186 return 0; 187 } 188 189 CEED_QFUNCTION_HELPER void QdataUnpack_ND(CeedInt N, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar *dXdx) { 190 // Cannot directly use QdataUnpack* helper functions due to SYCL online compiler incompatabilities 191 switch (N) { 192 case 2: 193 StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ); 194 StoredValuesUnpack(Q, i, 1, 4, q_data, dXdx); 195 break; 196 case 3: 197 StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ); 198 StoredValuesUnpack(Q, i, 1, 9, q_data, dXdx); 199 break; 200 } 201 } 202 203 CEED_QFUNCTION_HELPER int QdataBoundaryUnpack_ND(CeedInt N, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar *dXdx, 204 CeedScalar *normal) { 205 // Cannot directly use QdataBoundaryUnpack* helper functions due to SYCL online compiler incompatabilities 206 switch (N) { 207 case 2: 208 if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ); 209 if (normal) StoredValuesUnpack(Q, i, 1, 2, q_data, normal); 210 break; 211 case 3: 212 if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ); 213 if (normal) StoredValuesUnpack(Q, i, 1, 3, q_data, normal); 214 if (dXdx) StoredValuesUnpack(Q, i, 4, 6, q_data, (CeedScalar *)dXdx); 215 break; 216 } 217 return CEED_ERROR_SUCCESS; 218 } 219 220 CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_ND(CeedInt N, CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s, 221 StateVariable state_var, const CeedScalar *grad_q, const CeedScalar *dXdx, 222 State *grad_s) { 223 switch (N) { 224 case 2: { 225 for (CeedInt k = 0; k < 2; k++) { 226 CeedScalar dqi[5]; 227 for (CeedInt j = 0; j < 5; j++) { 228 dqi[j] = grad_q[(Q * 5) * 0 + Q * j + i] * dXdx[0 * N + k] + grad_q[(Q * 5) * 1 + Q * j + i] * dXdx[1 * N + k]; 229 } 230 grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var); 231 } 232 CeedScalar U[5] = {0.}; 233 grad_s[2] = StateFromU(gas, U); 234 } break; 235 case 3: 236 // Cannot directly use StatePhysicalGradientFromReference helper functions due to SYCL online compiler incompatabilities 237 for (CeedInt k = 0; k < 3; k++) { 238 CeedScalar dqi[5]; 239 for (CeedInt j = 0; j < 5; j++) { 240 dqi[j] = grad_q[(Q * 5) * 0 + Q * j + i] * dXdx[0 * N + k] + grad_q[(Q * 5) * 1 + Q * j + i] * dXdx[1 * N + k] + 241 grad_q[(Q * 5) * 2 + Q * j + i] * dXdx[2 * N + k]; 242 } 243 grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var); 244 } 245 break; 246 } 247 } 248 249 // @brief Calculate the stabilization constant \tau 250 CEED_QFUNCTION_HELPER CeedScalar Tau(AdvectionContext context, const State s, const CeedScalar *dXdx, CeedInt dim) { 251 switch (context->stabilization_tau) { 252 case STAB_TAU_CTAU: { 253 CeedScalar uX[3] = {0.}; 254 255 MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX); 256 return context->CtauS / sqrt(DotN(uX, uX, dim)); 257 } break; 258 case STAB_TAU_ADVDIFF_SHAKIB: { 259 CeedScalar gijd_mat[9] = {0.}, gij_uj[3] = {0.}; 260 261 MatMatN(dXdx, dXdx, dim, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat); 262 MatVecNM(gijd_mat, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, gij_uj); 263 return 1 / sqrt(Square(2 * context->Ctau_t / context->dt) + DotN(s.Y.velocity, gij_uj, dim) * context->Ctau_a); 264 } break; 265 default: 266 return 0.; 267 } 268 } 269 270 // ***************************************************************************** 271 // This QFunction implements Advection for implicit time stepping method 272 // ***************************************************************************** 273 CEED_QFUNCTION_HELPER void IFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) { 274 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 275 const CeedScalar(*grad_q) = in[1]; 276 const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2]; 277 const CeedScalar(*q_data) = in[3]; 278 279 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 280 CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; 281 282 AdvectionContext context = (AdvectionContext)ctx; 283 NewtonianIdealGasContext gas; 284 struct NewtonianIdealGasContext_ gas_struct = {0}; 285 gas = &gas_struct; 286 287 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 288 const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]}; 289 const State s = StateFromU(gas, qi); 290 291 CeedScalar wdetJ, dXdx[9]; 292 QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx); 293 State grad_s[3]; 294 StatePhysicalGradientFromReference_ND(dim, Q, i, gas, s, STATEVAR_CONSERVATIVE, grad_q, dXdx, grad_s); 295 296 const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total}; 297 298 for (CeedInt f = 0; f < 4; f++) { 299 for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0; // No Change in density or momentum 300 v[f][i] = wdetJ * q_dot[f][i]; // K Mass/transient term 301 } 302 303 CeedScalar div_u = 0; 304 for (CeedInt j = 0; j < dim; j++) { 305 for (CeedInt k = 0; k < dim; k++) { 306 div_u += grad_s[k].Y.velocity[j]; 307 } 308 } 309 CeedScalar strong_conv = s.U.E_total * div_u + DotN(s.Y.velocity, Grad_E, dim); 310 CeedScalar strong_res = q_dot[4][i] + strong_conv; 311 312 v[4][i] = wdetJ * q_dot[4][i]; // transient part (ALWAYS) 313 314 CeedScalar uX[3] = {0.}; 315 MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX); 316 317 if (context->strong_form) { // Strong Galerkin convection term: v div(E u) 318 v[4][i] += wdetJ * strong_conv; 319 } else { // Weak Galerkin convection term: -dv \cdot (E u) 320 for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = -wdetJ * s.U.E_total * uX[j]; 321 } 322 323 { // Diffusion 324 CeedScalar Fe[3], Fe_dXdx[3] = {0.}; 325 326 for (CeedInt i = 0; i < dim; i++) Fe[i] = -context->diffusion_coeff * grad_s[i].U.E_total; 327 MatVecNM(dXdx, Fe, dim, dim, CEED_NOTRANSPOSE, Fe_dXdx); 328 for (CeedInt k = 0; k < dim; k++) grad_v[k][4][i] -= wdetJ * Fe_dXdx[k]; 329 } 330 331 const CeedScalar TauS = Tau(context, s, dXdx, dim); 332 for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) { 333 case STAB_NONE: 334 break; 335 case STAB_SU: 336 grad_v[j][4][i] += wdetJ * TauS * strong_conv * uX[j]; 337 break; 338 case STAB_SUPG: 339 grad_v[j][4][i] += wdetJ * TauS * strong_res * uX[j]; 340 break; 341 } 342 } 343 } 344 345 CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 346 IFunction_AdvectionGeneric(ctx, Q, in, out, 3); 347 return 0; 348 } 349 350 CEED_QFUNCTION(IFunction_Advection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 351 IFunction_AdvectionGeneric(ctx, Q, in, out, 2); 352 return 0; 353 } 354 355 CEED_QFUNCTION_HELPER void MassFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) { 356 const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 357 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1]; 358 const CeedScalar(*q_data) = in[2]; 359 360 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 361 CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; 362 363 AdvectionContext context = (AdvectionContext)ctx; 364 struct NewtonianIdealGasContext_ gas_struct = {0}; 365 NewtonianIdealGasContext gas = &gas_struct; 366 367 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 368 const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]}; 369 const State s = StateFromU(gas, qi); 370 CeedScalar wdetJ, dXdx[9]; 371 QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx); 372 373 for (CeedInt f = 0; f < 4; f++) { 374 for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0; // No Change in density or momentum 375 v[f][i] = wdetJ * q_dot[f][i]; // K Mass/transient term 376 } 377 378 // Unstabilized mass term 379 v[4][i] = wdetJ * q_dot[4][i]; 380 381 // Stabilized mass term 382 CeedScalar uX[3] = {0.}; 383 MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX); 384 const CeedScalar TauS = Tau(context, s, dXdx, dim); 385 for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) { 386 case STAB_NONE: 387 case STAB_SU: 388 grad_v[j][4][i] = 0; 389 break; // These should be run with the unstabilized mass matrix anyways 390 case STAB_SUPG: 391 grad_v[j][4][i] = wdetJ * TauS * q_dot[4][i] * uX[j]; 392 break; 393 } 394 } 395 } 396 397 CEED_QFUNCTION(MassFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 398 MassFunction_AdvectionGeneric(ctx, Q, in, out, 3); 399 return 0; 400 } 401 402 CEED_QFUNCTION(MassFunction_Advection2D)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 403 MassFunction_AdvectionGeneric(ctx, Q, in, out, 2); 404 return 0; 405 } 406 407 // ***************************************************************************** 408 // This QFunction implements Advection for explicit time stepping method 409 // ***************************************************************************** 410 CEED_QFUNCTION_HELPER void RHSFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) { 411 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 412 const CeedScalar(*grad_q) = in[1]; 413 const CeedScalar(*q_data) = in[2]; 414 415 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 416 CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; 417 418 AdvectionContext context = (AdvectionContext)ctx; 419 struct NewtonianIdealGasContext_ gas_struct = {0}; 420 NewtonianIdealGasContext gas = &gas_struct; 421 422 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 423 const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]}; 424 const State s = StateFromU(gas, qi); 425 426 CeedScalar wdetJ, dXdx[9]; 427 QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx); 428 State grad_s[3]; 429 StatePhysicalGradientFromReference_ND(dim, Q, i, gas, s, STATEVAR_CONSERVATIVE, grad_q, dXdx, grad_s); 430 431 const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total}; 432 433 for (CeedInt f = 0; f < 4; f++) { 434 for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0; // No Change in density or momentum 435 v[f][i] = 0.; 436 } 437 438 CeedScalar div_u = 0; 439 for (CeedInt j = 0; j < dim; j++) { 440 for (CeedInt k = 0; k < dim; k++) { 441 div_u += grad_s[k].Y.velocity[j]; 442 } 443 } 444 CeedScalar strong_conv = s.U.E_total * div_u + DotN(s.Y.velocity, Grad_E, dim); 445 446 CeedScalar uX[3] = {0.}; 447 MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX); 448 449 if (context->strong_form) { // Strong Galerkin convection term: v div(E u) 450 v[4][i] = -wdetJ * strong_conv; 451 for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = 0; 452 } else { // Weak Galerkin convection term: -dv \cdot (E u) 453 for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = wdetJ * s.U.E_total * uX[j]; 454 v[4][i] = 0.; 455 } 456 457 { // Diffusion 458 CeedScalar Fe[3], Fe_dXdx[3] = {0.}; 459 460 for (CeedInt i = 0; i < dim; i++) Fe[i] = -context->diffusion_coeff * grad_s[i].U.E_total; 461 MatVecNM(dXdx, Fe, dim, dim, CEED_NOTRANSPOSE, Fe_dXdx); 462 for (CeedInt k = 0; k < dim; k++) grad_v[k][4][i] += wdetJ * Fe_dXdx[k]; 463 } 464 465 const CeedScalar TauS = Tau(context, s, dXdx, dim); 466 for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) { 467 case STAB_NONE: 468 break; 469 case STAB_SU: 470 case STAB_SUPG: 471 grad_v[j][4][i] -= wdetJ * TauS * strong_conv * uX[j]; 472 break; 473 } 474 } 475 } 476 477 CEED_QFUNCTION(RHS_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 478 RHSFunction_AdvectionGeneric(ctx, Q, in, out, 3); 479 return 0; 480 } 481 482 CEED_QFUNCTION(RHS_Advection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 483 RHSFunction_AdvectionGeneric(ctx, Q, in, out, 2); 484 return 0; 485 } 486 487 // ***************************************************************************** 488 // This QFunction implements consistent outflow and inflow BCs 489 // for advection 490 // 491 // Inflow and outflow faces are determined based on sign(dot(wind, normal)): 492 // sign(dot(wind, normal)) > 0 : outflow BCs 493 // sign(dot(wind, normal)) < 0 : inflow BCs 494 // 495 // Outflow BCs: 496 // The validity of the weak form of the governing equations is extended to the outflow and the current values of E are applied. 497 // 498 // Inflow BCs: 499 // A prescribed Total Energy (E_wind) is applied weakly. 500 // ***************************************************************************** 501 CEED_QFUNCTION(Advection_InOutFlowGeneric)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) { 502 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 503 const CeedScalar(*q_data_sur) = in[2]; 504 505 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 506 AdvectionContext context = (AdvectionContext)ctx; 507 const CeedScalar E_wind = context->E_wind; 508 const CeedScalar strong_form = context->strong_form; 509 const bool is_implicit = context->implicit; 510 511 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 512 const CeedScalar rho = q[0][i]; 513 const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho}; 514 const CeedScalar E = q[4][i]; 515 516 CeedScalar wdetJb, normal[3]; 517 QdataBoundaryUnpack_ND(dim, Q, i, q_data_sur, &wdetJb, NULL, normal); 518 wdetJb *= is_implicit ? -1. : 1.; 519 520 const CeedScalar u_normal = DotN(normal, u, dim); 521 522 // No Change in density or momentum 523 for (CeedInt j = 0; j < 4; j++) { 524 v[j][i] = 0; 525 } 526 // Implementing in/outflow BCs 527 if (u_normal > 0) { // outflow 528 v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal; 529 } else { // inflow 530 v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal; 531 } 532 } 533 return 0; 534 } 535 536 CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 537 Advection_InOutFlowGeneric(ctx, Q, in, out, 3); 538 return 0; 539 } 540 541 CEED_QFUNCTION(Advection2d_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 542 Advection_InOutFlowGeneric(ctx, Q, in, out, 2); 543 return 0; 544 } 545