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 const CeedScalar boundary_threshold = 20 * CEED_EPSILON; 161 Cross3(skewed_barrier, inflow_to_point, cross_product); 162 163 q[4] = cross_product[2] > boundary_threshold ? 0 : 1; 164 if ((x < boundary_threshold && wind[0] < boundary_threshold) || // outflow at -x boundary 165 (y < boundary_threshold && wind[1] < boundary_threshold) || // outflow at -y boundary 166 (x > context->lx - boundary_threshold && wind[0] > boundary_threshold) || // outflow at +x boundary 167 (y > context->ly - boundary_threshold && wind[1] > boundary_threshold) // outflow at +y boundary 168 ) { 169 q[4] = 0; 170 } 171 } break; 172 } 173 174 return 0; 175 } 176 177 // ***************************************************************************** 178 // This QFunction sets the initial conditions for 3D advection 179 // ***************************************************************************** 180 CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 181 // Inputs 182 const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 183 // Outputs 184 CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 185 186 // Quadrature Point Loop 187 CeedPragmaSIMD 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 CEED_QFUNCTION(Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 213 // Inputs 214 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 215 const CeedScalar(*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1]; 216 const CeedScalar(*q_data) = in[2]; 217 218 // Outputs 219 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 220 CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; 221 222 // Context 223 AdvectionContext context = (AdvectionContext)ctx; 224 const CeedScalar CtauS = context->CtauS; 225 const CeedScalar strong_form = context->strong_form; 226 227 // Quadrature Point Loop 228 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 229 // Setup 230 // -- Interp in 231 const CeedScalar rho = q[0][i]; 232 const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho}; 233 const CeedScalar E = q[4][i]; 234 // -- Grad in 235 const CeedScalar drho[3] = {dq[0][0][i], dq[1][0][i], dq[2][0][i]}; 236 const CeedScalar du[3][3] = { 237 {(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}, 238 {(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}, 239 {(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} 240 }; 241 const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]}; 242 CeedScalar wdetJ, dXdx[3][3]; 243 QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx); 244 // The Physics 245 // Note with the order that du was filled and the order that dXdx was filled 246 // du[j][k]= du_j / dX_K (note cap K to be clear this is u_{j,xi_k}) 247 // dXdx[k][j] = dX_K / dx_j 248 // X_K=Kth reference element coordinate (note cap X and K instead of xi_k} 249 // x_j and u_j are jth physical position and velocity components 250 251 // No Change in density or momentum 252 for (CeedInt f = 0; f < 4; f++) { 253 for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0; 254 v[f][i] = 0; 255 } 256 257 // -- Total Energy 258 // Evaluate the strong form using div(E u) = u . grad(E) + E div(u) 259 // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j} 260 CeedScalar div_u = 0, u_dot_grad_E = 0; 261 for (CeedInt j = 0; j < 3; j++) { 262 CeedScalar dEdx_j = 0; 263 for (CeedInt k = 0; k < 3; k++) { 264 div_u += du[j][k] * dXdx[k][j]; // u_{j,j} = u_{j,K} X_{K,j} 265 dEdx_j += dE[k] * dXdx[k][j]; 266 } 267 u_dot_grad_E += u[j] * dEdx_j; 268 } 269 CeedScalar strong_conv = E * div_u + u_dot_grad_E; 270 271 // Weak Galerkin convection term: dv \cdot (E u) 272 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]); 273 v[4][i] = 0; 274 275 // Strong Galerkin convection term: - v div(E u) 276 v[4][i] = -strong_form * wdetJ * strong_conv; 277 278 // Stabilization requires a measure of element transit time in the velocity 279 // field u. 280 CeedScalar uX[3]; 281 for (CeedInt j = 0; j < 3; j++) uX[j] = dXdx[j][0] * u[0] + dXdx[j][1] * u[1] + dXdx[j][2] * u[2]; 282 const CeedScalar TauS = CtauS / sqrt(Dot3(uX, uX)); 283 for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * TauS * strong_conv * uX[j]; 284 } // End Quadrature Point Loop 285 286 return 0; 287 } 288 289 // ***************************************************************************** 290 // This QFunction implements 3D (mentioned above) with implicit time stepping method 291 // ***************************************************************************** 292 CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 293 // Inputs 294 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 295 const CeedScalar(*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1]; 296 const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2]; 297 const CeedScalar(*q_data) = in[3]; 298 299 // Outputs 300 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 301 CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; 302 CeedScalar *jac_data = out[2]; 303 304 AdvectionContext context = (AdvectionContext)ctx; 305 const CeedScalar CtauS = context->CtauS; 306 const CeedScalar strong_form = context->strong_form; 307 const CeedScalar zeros[14] = {0.}; 308 309 // Quadrature Point Loop 310 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 311 // Setup 312 // -- Interp in 313 const CeedScalar rho = q[0][i]; 314 const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho}; 315 const CeedScalar E = q[4][i]; 316 // -- Grad in 317 const CeedScalar drho[3] = {dq[0][0][i], dq[1][0][i], dq[2][0][i]}; 318 const CeedScalar du[3][3] = { 319 {(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}, 320 {(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}, 321 {(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} 322 }; 323 const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]}; 324 CeedScalar wdetJ, dXdx[3][3]; 325 QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx); 326 // The Physics 327 // Note with the order that du was filled and the order that dXdx was filled 328 // du[j][k]= du_j / dX_K (note cap K to be clear this is u_{j,xi_k} ) 329 // dXdx[k][j] = dX_K / dx_j 330 // X_K=Kth reference element coordinate (note cap X and K instead of xi_k} 331 // x_j and u_j are jth physical position and velocity components 332 333 // No Change in density or momentum 334 for (CeedInt f = 0; f < 4; f++) { 335 for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0; 336 v[f][i] = wdetJ * q_dot[f][i]; // K Mass/transient term 337 } 338 339 // -- Total Energy 340 // Evaluate the strong form using div(E u) = u . grad(E) + E div(u) 341 // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j} 342 CeedScalar div_u = 0, u_dot_grad_E = 0; 343 for (CeedInt j = 0; j < 3; j++) { 344 CeedScalar dEdx_j = 0; 345 for (CeedInt k = 0; k < 3; k++) { 346 div_u += du[j][k] * dXdx[k][j]; // u_{j,j} = u_{j,K} X_{K,j} 347 dEdx_j += dE[k] * dXdx[k][j]; 348 } 349 u_dot_grad_E += u[j] * dEdx_j; 350 } 351 CeedScalar strong_conv = E * div_u + u_dot_grad_E; 352 CeedScalar strong_res = q_dot[4][i] + strong_conv; 353 354 v[4][i] = wdetJ * q_dot[4][i]; // transient part (ALWAYS) 355 356 // Weak Galerkin convection term: -dv \cdot (E u) 357 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]); 358 359 // Strong Galerkin convection term: v div(E u) 360 v[4][i] += wdetJ * strong_form * strong_conv; 361 362 // Stabilization requires a measure of element transit time in the velocity 363 // field u. 364 CeedScalar uX[3]; 365 for (CeedInt j = 0; j < 3; j++) uX[j] = dXdx[j][0] * u[0] + dXdx[j][1] * u[1] + dXdx[j][2] * u[2]; 366 const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]); 367 368 for (CeedInt j = 0; j < 3; j++) switch (context->stabilization) { 369 case STAB_NONE: 370 break; 371 case STAB_SU: 372 dv[j][4][i] += wdetJ * TauS * strong_conv * uX[j]; 373 break; 374 case STAB_SUPG: 375 dv[j][4][i] += wdetJ * TauS * strong_res * uX[j]; 376 break; 377 } 378 StoredValuesPack(Q, i, 0, 14, zeros, jac_data); 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 to the outflow and the current values of E are applied. 394 // 395 // Inflow BCs: 396 // A prescribed Total Energy (E_wind) is applied weakly. 397 // ***************************************************************************** 398 CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 399 // Inputs 400 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 401 const CeedScalar(*q_data_sur) = in[2]; 402 403 // Outputs 404 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 405 AdvectionContext context = (AdvectionContext)ctx; 406 const CeedScalar E_wind = context->E_wind; 407 const CeedScalar strong_form = context->strong_form; 408 const bool is_implicit = context->implicit; 409 410 // Quadrature Point Loop 411 CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 412 // Setup 413 // -- Interp in 414 const CeedScalar rho = q[0][i]; 415 const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho}; 416 const CeedScalar E = q[4][i]; 417 418 CeedScalar wdetJb, norm[3]; 419 QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm); 420 wdetJb *= is_implicit ? -1. : 1.; 421 422 // Normal velocity 423 const CeedScalar u_normal = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2]; 424 425 // No Change in density or momentum 426 for (CeedInt j = 0; j < 4; j++) { 427 v[j][i] = 0; 428 } 429 // Implementing in/outflow BCs 430 if (u_normal > 0) { // outflow 431 v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal; 432 } else { // inflow 433 v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal; 434 } 435 } // End Quadrature Point Loop 436 return 0; 437 } 438 // ***************************************************************************** 439 440 #endif // advection_h 441