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