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 /// Advection initial condition and operator for Navier-Stokes example using PETSc 10 11 #ifndef advection_h 12 #define advection_h 13 14 #include <math.h> 15 16 typedef struct SetupContext_ *SetupContext; 17 struct SetupContext_ { 18 CeedScalar rc; 19 CeedScalar lx; 20 CeedScalar ly; 21 CeedScalar lz; 22 CeedScalar wind[3]; 23 CeedScalar time; 24 int wind_type; // See WindType: 0=ROTATION, 1=TRANSLATION 25 int bubble_type; // See BubbleType: 0=SPHERE, 1=CYLINDER 26 int bubble_continuity_type; // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK 27 }; 28 29 typedef struct AdvectionContext_ *AdvectionContext; 30 struct AdvectionContext_ { 31 CeedScalar CtauS; 32 CeedScalar strong_form; 33 CeedScalar E_wind; 34 bool implicit; 35 int stabilization; // See StabilizationType: 0=none, 1=SU, 2=SUPG 36 }; 37 38 CEED_QFUNCTION_HELPER CeedScalar Square(CeedScalar x) { return x*x; } 39 40 // ***************************************************************************** 41 // This QFunction sets the initial conditions and the boundary conditions 42 // for two test cases: ROTATION and TRANSLATION 43 // 44 // -- ROTATION (default) 45 // Initial Conditions: 46 // Mass Density: 47 // Constant mass density of 1.0 48 // Momentum Density: 49 // Rotational field in x,y 50 // Energy Density: 51 // Maximum of 1. x0 decreasing linearly to 0. as radial distance 52 // increases to (1.-r/rc), then 0. everywhere else 53 // 54 // Boundary Conditions: 55 // Mass Density: 56 // 0.0 flux 57 // Momentum Density: 58 // 0.0 59 // Energy Density: 60 // 0.0 flux 61 // 62 // -- TRANSLATION 63 // Initial Conditions: 64 // Mass Density: 65 // Constant mass density of 1.0 66 // Momentum Density: 67 // Constant rectilinear field in x,y 68 // Energy Density: 69 // Maximum of 1. x0 decreasing linearly to 0. as radial distance 70 // increases to (1.-r/rc), then 0. everywhere else 71 // 72 // Boundary Conditions: 73 // Mass Density: 74 // 0.0 flux 75 // Momentum Density: 76 // 0.0 77 // Energy Density: 78 // Inflow BCs: 79 // E = E_wind 80 // Outflow BCs: 81 // E = E(boundary) 82 // Both In/Outflow BCs for E are applied weakly in the 83 // QFunction "Advection_Sur" 84 // 85 // ***************************************************************************** 86 87 // ***************************************************************************** 88 // This helper function provides support for the exact, time-dependent solution 89 // (currently not implemented) and IC formulation for 3D advection 90 // ***************************************************************************** 91 CEED_QFUNCTION_HELPER int Exact_Advection(CeedInt dim, CeedScalar time, 92 const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) { 93 const SetupContext context = (SetupContext)ctx; 94 const CeedScalar rc = context->rc; 95 const CeedScalar lx = context->lx; 96 const CeedScalar ly = context->ly; 97 const CeedScalar lz = context->lz; 98 const CeedScalar *wind = context->wind; 99 100 // Setup 101 const CeedScalar x0[3] = {0.25*lx, 0.5*ly, 0.5*lz}; 102 const CeedScalar center[3] = {0.5*lx, 0.5*ly, 0.5*lz}; 103 104 // -- Coordinates 105 const CeedScalar x = X[0]; 106 const CeedScalar y = X[1]; 107 const CeedScalar z = X[2]; 108 109 // -- Energy 110 CeedScalar r = 0.; 111 switch (context->bubble_type) { 112 // original sphere 113 case 0: { // (dim=3) 114 r = sqrt(Square(x - x0[0]) + 115 Square(y - x0[1]) + 116 Square(z - x0[2])); 117 } break; 118 // cylinder (needs periodicity to work properly) 119 case 1: { // (dim=2) 120 r = sqrt(Square(x - x0[0]) + Square(y - x0[1])); 121 } break; 122 } 123 124 // Initial Conditions 125 switch (context->wind_type) { 126 case 0: // Rotation 127 q[0] = 1.; 128 q[1] = -(y - center[1]); 129 q[2] = (x - center[0]); 130 q[3] = 0; 131 break; 132 case 1: // Translation 133 q[0] = 1.; 134 q[1] = wind[0]; 135 q[2] = wind[1]; 136 q[3] = wind[2]; 137 break; 138 } 139 140 switch (context->bubble_continuity_type) { 141 // original continuous, smooth shape 142 case 0: { 143 q[4] = r <= rc ? (1.-r/rc) : 0.; 144 } break; 145 // discontinuous, sharp back half shape 146 case 1: { 147 q[4] = ((r <= rc) && (y<center[1])) ? (1.-r/rc) : 0.; 148 } break; 149 // attempt to define a finite thickness that will get resolved under grid refinement 150 case 2: { 151 q[4] = ((r <= rc) 152 && (y<center[1])) ? (1.-r/rc)*fmin(1.0,(center[1]-y)/1.25) : 0.; 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, 162 const CeedScalar *const *in, 163 CeedScalar *const *out) { 164 // Inputs 165 const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 166 // Outputs 167 CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 168 169 CeedPragmaSIMD 170 // Quadrature Point Loop 171 for (CeedInt i=0; i<Q; i++) { 172 const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]}; 173 CeedScalar q[5] = {0.}; 174 175 Exact_Advection(3, 0., x, 5, q, ctx); 176 for (CeedInt j=0; j<5; j++) q0[j][i] = q[j]; 177 } // End of Quadrature Point Loop 178 179 // Return 180 return 0; 181 } 182 183 // ***************************************************************************** 184 // This QFunction implements the following formulation of the advection equation 185 // 186 // This is 3D advection given in two formulations based upon the weak form. 187 // 188 // State Variables: q = ( rho, U1, U2, U3, E ) 189 // rho - Mass Density 190 // Ui - Momentum Density , Ui = rho ui 191 // E - Total Energy Density 192 // 193 // Advection Equation: 194 // dE/dt + div( E u ) = 0 195 // 196 // ***************************************************************************** 197 CEED_QFUNCTION(Advection)(void *ctx, CeedInt Q, 198 const CeedScalar *const *in, CeedScalar *const *out) { 199 // Inputs 200 // *INDENT-OFF* 201 const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], 202 (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1], 203 (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2]; 204 205 // Outputs 206 CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0], 207 (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; 208 // *INDENT-ON* 209 210 // Context 211 AdvectionContext context = (AdvectionContext)ctx; 212 const CeedScalar CtauS = context->CtauS; 213 const CeedScalar strong_form = context->strong_form; 214 215 CeedPragmaSIMD 216 // Quadrature Point Loop 217 for (CeedInt i=0; i<Q; i++) { 218 // Setup 219 // -- Interp in 220 const CeedScalar rho = q[0][i]; 221 const CeedScalar u[3] = {q[1][i] / rho, 222 q[2][i] / rho, 223 q[3][i] / rho 224 }; 225 const CeedScalar E = q[4][i]; 226 // -- Grad in 227 const CeedScalar drho[3] = {dq[0][0][i], 228 dq[1][0][i], 229 dq[2][0][i] 230 }; 231 // *INDENT-OFF* 232 const CeedScalar du[3][3] = {{(dq[0][1][i] - drho[0]*u[0]) / rho, 233 (dq[1][1][i] - drho[1]*u[0]) / rho, 234 (dq[2][1][i] - drho[2]*u[0]) / rho}, 235 {(dq[0][2][i] - drho[0]*u[1]) / rho, 236 (dq[1][2][i] - drho[1]*u[1]) / rho, 237 (dq[2][2][i] - drho[2]*u[1]) / rho}, 238 {(dq[0][3][i] - drho[0]*u[2]) / rho, 239 (dq[1][3][i] - drho[1]*u[2]) / rho, 240 (dq[2][3][i] - drho[2]*u[2]) / rho} 241 }; 242 // *INDENT-ON* 243 const CeedScalar dE[3] = {dq[0][4][i], 244 dq[1][4][i], 245 dq[2][4][i] 246 }; 247 // -- Interp-to-Interp q_data 248 const CeedScalar wdetJ = q_data[0][i]; 249 // -- Interp-to-Grad q_data 250 // ---- Inverse of change of coordinate matrix: X_i,j 251 // *INDENT-OFF* 252 const CeedScalar dXdx[3][3] = {{q_data[1][i], 253 q_data[2][i], 254 q_data[3][i]}, 255 {q_data[4][i], 256 q_data[5][i], 257 q_data[6][i]}, 258 {q_data[7][i], 259 q_data[8][i], 260 q_data[9][i]} 261 }; 262 // *INDENT-ON* 263 // The Physics 264 // Note with the order that du was filled and the order that dXdx was filled 265 // du[j][k]= du_j / dX_K (note cap K to be clear this is u_{j,xi_k}) 266 // dXdx[k][j] = dX_K / dx_j 267 // X_K=Kth reference element coordinate (note cap X and K instead of xi_k} 268 // x_j and u_j are jth physical position and velocity components 269 270 // No Change in density or momentum 271 for (CeedInt f=0; f<4; f++) { 272 for (CeedInt j=0; j<3; j++) 273 dv[j][f][i] = 0; 274 v[f][i] = 0; 275 } 276 277 // -- Total Energy 278 // Evaluate the strong form using div(E u) = u . grad(E) + E div(u) 279 // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j} 280 CeedScalar div_u = 0, u_dot_grad_E = 0; 281 for (CeedInt j=0; j<3; j++) { 282 CeedScalar dEdx_j = 0; 283 for (CeedInt k=0; k<3; k++) { 284 div_u += du[j][k] * dXdx[k][j]; // u_{j,j} = u_{j,K} X_{K,j} 285 dEdx_j += dE[k] * dXdx[k][j]; 286 } 287 u_dot_grad_E += u[j] * dEdx_j; 288 } 289 CeedScalar strong_conv = E*div_u + u_dot_grad_E; 290 291 // Weak Galerkin convection term: dv \cdot (E u) 292 for (CeedInt j=0; j<3; j++) 293 dv[j][4][i] = (1 - strong_form) * wdetJ * E * (u[0]*dXdx[j][0] + 294 u[1]*dXdx[j][1] + 295 u[2]*dXdx[j][2]); 296 v[4][i] = 0; 297 298 // Strong Galerkin convection term: - v div(E u) 299 v[4][i] = -strong_form * wdetJ * strong_conv; 300 301 // Stabilization requires a measure of element transit time in the velocity 302 // field u. 303 CeedScalar uX[3]; 304 for (CeedInt j=0; j<3; 305 j++) uX[j] = dXdx[j][0]*u[0] + dXdx[j][1]*u[1] + dXdx[j][2]*u[2]; 306 const CeedScalar TauS = CtauS / sqrt(uX[0]*uX[0] + uX[1]*uX[1] + uX[2]*uX[2]); 307 for (CeedInt j=0; j<3; j++) 308 dv[j][4][i] -= wdetJ * TauS * strong_conv * uX[j]; 309 } // End Quadrature Point Loop 310 311 return 0; 312 } 313 314 // ***************************************************************************** 315 // This QFunction implements 3D (mentioned above) with 316 // implicit time stepping method 317 // 318 // ***************************************************************************** 319 CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, 320 const CeedScalar *const *in, 321 CeedScalar *const *out) { 322 // *INDENT-OFF* 323 // Inputs 324 const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], 325 (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1], 326 (*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2], 327 (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3]; 328 // Outputs 329 CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0], 330 (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; 331 // *INDENT-ON* 332 AdvectionContext context = (AdvectionContext)ctx; 333 const CeedScalar CtauS = context->CtauS; 334 const CeedScalar strong_form = context->strong_form; 335 336 CeedPragmaSIMD 337 // Quadrature Point Loop 338 for (CeedInt i=0; i<Q; i++) { 339 // Setup 340 // -- Interp in 341 const CeedScalar rho = q[0][i]; 342 const CeedScalar u[3] = {q[1][i] / rho, 343 q[2][i] / rho, 344 q[3][i] / rho 345 }; 346 const CeedScalar E = q[4][i]; 347 // -- Grad in 348 const CeedScalar drho[3] = {dq[0][0][i], 349 dq[1][0][i], 350 dq[2][0][i] 351 }; 352 // *INDENT-OFF* 353 const CeedScalar du[3][3] = {{(dq[0][1][i] - drho[0]*u[0]) / rho, 354 (dq[1][1][i] - drho[1]*u[0]) / rho, 355 (dq[2][1][i] - drho[2]*u[0]) / rho}, 356 {(dq[0][2][i] - drho[0]*u[1]) / rho, 357 (dq[1][2][i] - drho[1]*u[1]) / rho, 358 (dq[2][2][i] - drho[2]*u[1]) / rho}, 359 {(dq[0][3][i] - drho[0]*u[2]) / rho, 360 (dq[1][3][i] - drho[1]*u[2]) / rho, 361 (dq[2][3][i] - drho[2]*u[2]) / rho} 362 }; 363 // *INDENT-ON* 364 const CeedScalar dE[3] = {dq[0][4][i], 365 dq[1][4][i], 366 dq[2][4][i] 367 }; 368 // -- Interp-to-Interp q_data 369 const CeedScalar wdetJ = q_data[0][i]; 370 // -- Interp-to-Grad q_data 371 // ---- Inverse of change of coordinate matrix: X_i,j 372 // *INDENT-OFF* 373 const CeedScalar dXdx[3][3] = {{q_data[1][i], 374 q_data[2][i], 375 q_data[3][i]}, 376 {q_data[4][i], 377 q_data[5][i], 378 q_data[6][i]}, 379 {q_data[7][i], 380 q_data[8][i], 381 q_data[9][i]} 382 }; 383 // *INDENT-ON* 384 // The Physics 385 // Note with the order that du was filled and the order that dXdx was filled 386 // du[j][k]= du_j / dX_K (note cap K to be clear this is u_{j,xi_k} ) 387 // dXdx[k][j] = dX_K / dx_j 388 // X_K=Kth reference element coordinate (note cap X and K instead of xi_k} 389 // x_j and u_j are jth physical position and velocity components 390 391 // No Change in density or momentum 392 for (CeedInt f=0; f<4; f++) { 393 for (CeedInt j=0; j<3; j++) 394 dv[j][f][i] = 0; 395 v[f][i] = wdetJ * q_dot[f][i]; //K Mass/transient term 396 } 397 398 // -- Total Energy 399 // Evaluate the strong form using div(E u) = u . grad(E) + E div(u) 400 // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j} 401 CeedScalar div_u = 0, u_dot_grad_E = 0; 402 for (CeedInt j=0; j<3; j++) { 403 CeedScalar dEdx_j = 0; 404 for (CeedInt k=0; k<3; k++) { 405 div_u += du[j][k] * dXdx[k][j]; // u_{j,j} = u_{j,K} X_{K,j} 406 dEdx_j += dE[k] * dXdx[k][j]; 407 } 408 u_dot_grad_E += u[j] * dEdx_j; 409 } 410 CeedScalar strong_conv = E*div_u + u_dot_grad_E; 411 CeedScalar strong_res = q_dot[4][i] + strong_conv; 412 413 v[4][i] = wdetJ * q_dot[4][i]; // transient part (ALWAYS) 414 415 // Weak Galerkin convection term: -dv \cdot (E u) 416 for (CeedInt j=0; j<3; j++) 417 dv[j][4][i] = -wdetJ * (1 - strong_form) * E * (u[0]*dXdx[j][0] + 418 u[1]*dXdx[j][1] + 419 u[2]*dXdx[j][2]); 420 421 // Strong Galerkin convection term: v div(E u) 422 v[4][i] += wdetJ * strong_form * strong_conv; 423 424 // Stabilization requires a measure of element transit time in the velocity 425 // field u. 426 CeedScalar uX[3]; 427 for (CeedInt j=0; j<3; 428 j++) uX[j] = dXdx[j][0]*u[0] + dXdx[j][1]*u[1] + dXdx[j][2]*u[2]; 429 const CeedScalar TauS = CtauS / sqrt(uX[0]*uX[0] + uX[1]*uX[1] + uX[2]*uX[2]); 430 431 for (CeedInt j=0; j<3; j++) 432 switch (context->stabilization) { 433 case 0: 434 break; 435 case 1: dv[j][4][i] += wdetJ * TauS * strong_conv * uX[j]; //SU 436 break; 437 case 2: dv[j][4][i] += wdetJ * TauS * strong_res * uX[j]; //SUPG 438 break; 439 } 440 } // End Quadrature Point Loop 441 442 return 0; 443 } 444 445 // ***************************************************************************** 446 // This QFunction implements consistent outflow and inflow BCs 447 // for 3D advection 448 // 449 // Inflow and outflow faces are determined based on sign(dot(wind, normal)): 450 // sign(dot(wind, normal)) > 0 : outflow BCs 451 // sign(dot(wind, normal)) < 0 : inflow BCs 452 // 453 // Outflow BCs: 454 // The validity of the weak form of the governing equations is extended 455 // to the outflow and the current values of E are applied. 456 // 457 // Inflow BCs: 458 // A prescribed Total Energy (E_wind) is applied weakly. 459 // 460 // ***************************************************************************** 461 CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, 462 const CeedScalar *const *in, 463 CeedScalar *const *out) { 464 // *INDENT-OFF* 465 // Inputs 466 const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], 467 (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1]; 468 // Outputs 469 CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 470 // *INDENT-ON* 471 AdvectionContext context = (AdvectionContext)ctx; 472 const CeedScalar E_wind = context->E_wind; 473 const CeedScalar strong_form = context->strong_form; 474 const bool implicit = context->implicit; 475 476 CeedPragmaSIMD 477 // Quadrature Point Loop 478 for (CeedInt i=0; i<Q; i++) { 479 // Setup 480 // -- Interp in 481 const CeedScalar rho = q[0][i]; 482 const CeedScalar u[3] = {q[1][i] / rho, 483 q[2][i] / rho, 484 q[3][i] / rho 485 }; 486 const CeedScalar E = q[4][i]; 487 488 // -- Interp-to-Interp q_data 489 // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q). 490 // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q). 491 // We can effect this by swapping the sign on this weight 492 const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i]; 493 494 // ---- Normal vectors 495 const CeedScalar norm[3] = {q_data_sur[1][i], 496 q_data_sur[2][i], 497 q_data_sur[3][i] 498 }; 499 // Normal velocity 500 const CeedScalar u_normal = norm[0]*u[0] + norm[1]*u[1] + norm[2]*u[2]; 501 502 // No Change in density or momentum 503 for (CeedInt j=0; j<4; j++) { 504 v[j][i] = 0; 505 } 506 // Implementing in/outflow BCs 507 if (u_normal > 0) { // outflow 508 v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal; 509 } else { // inflow 510 v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal; 511 } 512 } // End Quadrature Point Loop 513 return 0; 514 } 515 // ***************************************************************************** 516 517 #endif // advection_h 518