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