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