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, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) { 93 const SetupContextAdv context = (SetupContextAdv)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]) + Square(y - x0[1]) + Square(z - x0[2])); 115 } break; 116 // cylinder (needs periodicity to work properly) 117 case 1: { // (dim=2) 118 r = sqrt(Square(x - x0[0]) + Square(y - x0[1])); 119 } break; 120 } 121 122 // Initial Conditions 123 switch (context->wind_type) { 124 case 0: // Rotation 125 q[0] = 1.; 126 q[1] = -(y - center[1]); 127 q[2] = (x - center[0]); 128 q[3] = 0; 129 break; 130 case 1: // Translation 131 q[0] = 1.; 132 q[1] = wind[0]; 133 q[2] = wind[1]; 134 q[3] = wind[2]; 135 break; 136 } 137 138 switch (context->bubble_continuity_type) { 139 // original continuous, smooth shape 140 case 0: { 141 q[4] = r <= rc ? (1. - r / rc) : 0.; 142 } break; 143 // discontinuous, sharp back half shape 144 case 1: { 145 q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) : 0.; 146 } break; 147 // attempt to define a finite thickness that will get resolved under grid refinement 148 case 2: { 149 q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) * fmin(1.0, (center[1] - y) / 1.25) : 0.; 150 } break; 151 } 152 return 0; 153 } 154 155 // ***************************************************************************** 156 // This QFunction sets the initial conditions for 3D advection 157 // ***************************************************************************** 158 CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 159 // Inputs 160 const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 161 // Outputs 162 CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 163 164 CeedPragmaSIMD 165 // Quadrature Point Loop 166 for (CeedInt i = 0; i < Q; i++) { 167 const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]}; 168 CeedScalar q[5] = {0.}; 169 170 Exact_Advection(3, 0., x, 5, q, ctx); 171 for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j]; 172 } // End of Quadrature Point Loop 173 174 // Return 175 return 0; 176 } 177 178 // ***************************************************************************** 179 // This QFunction implements the following formulation of the advection equation 180 // 181 // This is 3D advection given in two formulations based upon the weak form. 182 // 183 // State Variables: q = ( rho, U1, U2, U3, E ) 184 // rho - Mass Density 185 // Ui - Momentum Density , Ui = rho ui 186 // E - Total Energy Density 187 // 188 // Advection Equation: 189 // dE/dt + div( E u ) = 0 190 // 191 // ***************************************************************************** 192 CEED_QFUNCTION(Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 193 // Inputs 194 // *INDENT-OFF* 195 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1], 196 (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2]; 197 198 // Outputs 199 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0], (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; 200 // *INDENT-ON* 201 202 // Context 203 AdvectionContext context = (AdvectionContext)ctx; 204 const CeedScalar CtauS = context->CtauS; 205 const CeedScalar strong_form = context->strong_form; 206 207 CeedPragmaSIMD 208 // Quadrature Point Loop 209 for (CeedInt i = 0; i < Q; i++) { 210 // Setup 211 // -- Interp in 212 const CeedScalar rho = q[0][i]; 213 const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho}; 214 const CeedScalar E = q[4][i]; 215 // -- Grad in 216 const CeedScalar drho[3] = {dq[0][0][i], dq[1][0][i], dq[2][0][i]}; 217 // *INDENT-OFF* 218 const CeedScalar du[3][3] = { 219 {(dq[0][1][i] - drho[0] * u[0]) / rho, (dq[1][1][i] - drho[1] * u[0]) / rho, (dq[2][1][i] - drho[2] * u[0]) / rho}, 220 {(dq[0][2][i] - drho[0] * u[1]) / rho, (dq[1][2][i] - drho[1] * u[1]) / rho, (dq[2][2][i] - drho[2] * u[1]) / rho}, 221 {(dq[0][3][i] - drho[0] * u[2]) / rho, (dq[1][3][i] - drho[1] * u[2]) / rho, (dq[2][3][i] - drho[2] * u[2]) / rho} 222 }; 223 // *INDENT-ON* 224 const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]}; 225 // -- Interp-to-Interp q_data 226 const CeedScalar wdetJ = q_data[0][i]; 227 // -- Interp-to-Grad q_data 228 // ---- Inverse of change of coordinate matrix: X_i,j 229 // *INDENT-OFF* 230 const CeedScalar dXdx[3][3] = { 231 {q_data[1][i], q_data[2][i], q_data[3][i]}, 232 {q_data[4][i], q_data[5][i], q_data[6][i]}, 233 {q_data[7][i], q_data[8][i], q_data[9][i]} 234 }; 235 // *INDENT-ON* 236 // The Physics 237 // Note with the order that du was filled and the order that dXdx was filled 238 // du[j][k]= du_j / dX_K (note cap K to be clear this is u_{j,xi_k}) 239 // dXdx[k][j] = dX_K / dx_j 240 // X_K=Kth reference element coordinate (note cap X and K instead of xi_k} 241 // x_j and u_j are jth physical position and velocity components 242 243 // No Change in density or momentum 244 for (CeedInt f = 0; f < 4; f++) { 245 for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0; 246 v[f][i] = 0; 247 } 248 249 // -- Total Energy 250 // Evaluate the strong form using div(E u) = u . grad(E) + E div(u) 251 // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j} 252 CeedScalar div_u = 0, u_dot_grad_E = 0; 253 for (CeedInt j = 0; j < 3; j++) { 254 CeedScalar dEdx_j = 0; 255 for (CeedInt k = 0; k < 3; k++) { 256 div_u += du[j][k] * dXdx[k][j]; // u_{j,j} = u_{j,K} X_{K,j} 257 dEdx_j += dE[k] * dXdx[k][j]; 258 } 259 u_dot_grad_E += u[j] * dEdx_j; 260 } 261 CeedScalar strong_conv = E * div_u + u_dot_grad_E; 262 263 // Weak Galerkin convection term: dv \cdot (E u) 264 for (CeedInt j = 0; j < 3; j++) dv[j][4][i] = (1 - strong_form) * wdetJ * E * (u[0] * dXdx[j][0] + u[1] * dXdx[j][1] + u[2] * dXdx[j][2]); 265 v[4][i] = 0; 266 267 // Strong Galerkin convection term: - v div(E u) 268 v[4][i] = -strong_form * wdetJ * strong_conv; 269 270 // Stabilization requires a measure of element transit time in the velocity 271 // field u. 272 CeedScalar uX[3]; 273 for (CeedInt j = 0; j < 3; j++) uX[j] = dXdx[j][0] * u[0] + dXdx[j][1] * u[1] + dXdx[j][2] * u[2]; 274 const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]); 275 for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * TauS * strong_conv * uX[j]; 276 } // End Quadrature Point Loop 277 278 return 0; 279 } 280 281 // ***************************************************************************** 282 // This QFunction implements 3D (mentioned above) with 283 // implicit time stepping method 284 // 285 // ***************************************************************************** 286 CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 287 // *INDENT-OFF* 288 // Inputs 289 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1], 290 (*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2], (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3]; 291 // Outputs 292 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0], (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; 293 // *INDENT-ON* 294 AdvectionContext context = (AdvectionContext)ctx; 295 const CeedScalar CtauS = context->CtauS; 296 const CeedScalar strong_form = context->strong_form; 297 298 CeedPragmaSIMD 299 // Quadrature Point Loop 300 for (CeedInt i = 0; i < Q; i++) { 301 // Setup 302 // -- Interp in 303 const CeedScalar rho = q[0][i]; 304 const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho}; 305 const CeedScalar E = q[4][i]; 306 // -- Grad in 307 const CeedScalar drho[3] = {dq[0][0][i], dq[1][0][i], dq[2][0][i]}; 308 // *INDENT-OFF* 309 const CeedScalar du[3][3] = { 310 {(dq[0][1][i] - drho[0] * u[0]) / rho, (dq[1][1][i] - drho[1] * u[0]) / rho, (dq[2][1][i] - drho[2] * u[0]) / rho}, 311 {(dq[0][2][i] - drho[0] * u[1]) / rho, (dq[1][2][i] - drho[1] * u[1]) / rho, (dq[2][2][i] - drho[2] * u[1]) / rho}, 312 {(dq[0][3][i] - drho[0] * u[2]) / rho, (dq[1][3][i] - drho[1] * u[2]) / rho, (dq[2][3][i] - drho[2] * u[2]) / rho} 313 }; 314 // *INDENT-ON* 315 const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]}; 316 // -- Interp-to-Interp q_data 317 const CeedScalar wdetJ = q_data[0][i]; 318 // -- Interp-to-Grad q_data 319 // ---- Inverse of change of coordinate matrix: X_i,j 320 // *INDENT-OFF* 321 const CeedScalar dXdx[3][3] = { 322 {q_data[1][i], q_data[2][i], q_data[3][i]}, 323 {q_data[4][i], q_data[5][i], q_data[6][i]}, 324 {q_data[7][i], q_data[8][i], q_data[9][i]} 325 }; 326 // *INDENT-ON* 327 // The Physics 328 // Note with the order that du was filled and the order that dXdx was filled 329 // du[j][k]= du_j / dX_K (note cap K to be clear this is u_{j,xi_k} ) 330 // dXdx[k][j] = dX_K / dx_j 331 // X_K=Kth reference element coordinate (note cap X and K instead of xi_k} 332 // x_j and u_j are jth physical position and velocity components 333 334 // No Change in density or momentum 335 for (CeedInt f = 0; f < 4; f++) { 336 for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0; 337 v[f][i] = wdetJ * q_dot[f][i]; // K Mass/transient term 338 } 339 340 // -- Total Energy 341 // Evaluate the strong form using div(E u) = u . grad(E) + E div(u) 342 // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j} 343 CeedScalar div_u = 0, u_dot_grad_E = 0; 344 for (CeedInt j = 0; j < 3; j++) { 345 CeedScalar dEdx_j = 0; 346 for (CeedInt k = 0; k < 3; k++) { 347 div_u += du[j][k] * dXdx[k][j]; // u_{j,j} = u_{j,K} X_{K,j} 348 dEdx_j += dE[k] * dXdx[k][j]; 349 } 350 u_dot_grad_E += u[j] * dEdx_j; 351 } 352 CeedScalar strong_conv = E * div_u + u_dot_grad_E; 353 CeedScalar strong_res = q_dot[4][i] + strong_conv; 354 355 v[4][i] = wdetJ * q_dot[4][i]; // transient part (ALWAYS) 356 357 // Weak Galerkin convection term: -dv \cdot (E u) 358 for (CeedInt j = 0; j < 3; j++) dv[j][4][i] = -wdetJ * (1 - strong_form) * E * (u[0] * dXdx[j][0] + u[1] * dXdx[j][1] + u[2] * dXdx[j][2]); 359 360 // Strong Galerkin convection term: v div(E u) 361 v[4][i] += wdetJ * strong_form * strong_conv; 362 363 // Stabilization requires a measure of element transit time in the velocity 364 // field u. 365 CeedScalar uX[3]; 366 for (CeedInt j = 0; j < 3; j++) uX[j] = dXdx[j][0] * u[0] + dXdx[j][1] * u[1] + dXdx[j][2] * u[2]; 367 const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]); 368 369 for (CeedInt j = 0; j < 3; j++) switch (context->stabilization) { 370 case 0: 371 break; 372 case 1: 373 dv[j][4][i] += wdetJ * TauS * strong_conv * uX[j]; // SU 374 break; 375 case 2: 376 dv[j][4][i] += wdetJ * TauS * strong_res * uX[j]; // SUPG 377 break; 378 } 379 } // End Quadrature Point Loop 380 381 return 0; 382 } 383 384 // ***************************************************************************** 385 // This QFunction implements consistent outflow and inflow BCs 386 // for 3D advection 387 // 388 // Inflow and outflow faces are determined based on sign(dot(wind, normal)): 389 // sign(dot(wind, normal)) > 0 : outflow BCs 390 // sign(dot(wind, normal)) < 0 : inflow BCs 391 // 392 // Outflow BCs: 393 // The validity of the weak form of the governing equations is extended 394 // to the outflow and the current values of E are applied. 395 // 396 // Inflow BCs: 397 // A prescribed Total Energy (E_wind) is applied weakly. 398 // 399 // ***************************************************************************** 400 CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 401 // *INDENT-OFF* 402 // Inputs 403 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2]; 404 // Outputs 405 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 406 // *INDENT-ON* 407 AdvectionContext context = (AdvectionContext)ctx; 408 const CeedScalar E_wind = context->E_wind; 409 const CeedScalar strong_form = context->strong_form; 410 const bool implicit = context->implicit; 411 412 CeedPragmaSIMD 413 // Quadrature Point Loop 414 for (CeedInt i = 0; i < Q; i++) { 415 // Setup 416 // -- Interp in 417 const CeedScalar rho = q[0][i]; 418 const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho}; 419 const CeedScalar E = q[4][i]; 420 421 // -- Interp-to-Interp q_data 422 // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q). 423 // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q). 424 // We can effect this by swapping the sign on this weight 425 const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i]; 426 427 // ---- Normal vectors 428 const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]}; 429 // Normal velocity 430 const CeedScalar u_normal = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2]; 431 432 // No Change in density or momentum 433 for (CeedInt j = 0; j < 4; j++) { 434 v[j][i] = 0; 435 } 436 // Implementing in/outflow BCs 437 if (u_normal > 0) { // outflow 438 v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal; 439 } else { // inflow 440 v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal; 441 } 442 } // End Quadrature Point Loop 443 return 0; 444 } 445 // ***************************************************************************** 446 447 #endif // advection_h 448