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