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