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