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 /// Shock tube initial condition and Euler equation operator for Navier-Stokes example using PETSc - modified from eulervortex.h 10 11 // Model from: 12 // On the Order of Accuracy and Numerical Performance of Two Classes of Finite Volume WENO Schemes, Zhang, Zhang, and Shu (2011). 13 14 #ifndef shocktube_h 15 #define shocktube_h 16 17 #include <ceed.h> 18 #include <math.h> 19 20 #include "utils.h" 21 22 typedef struct SetupContextShock_ *SetupContextShock; 23 struct SetupContextShock_ { 24 CeedScalar theta0; 25 CeedScalar thetaC; 26 CeedScalar P0; 27 CeedScalar N; 28 CeedScalar cv; 29 CeedScalar cp; 30 CeedScalar time; 31 CeedScalar mid_point; 32 CeedScalar P_high; 33 CeedScalar rho_high; 34 CeedScalar P_low; 35 CeedScalar rho_low; 36 int wind_type; // See WindType: 0=ROTATION, 1=TRANSLATION 37 int bubble_type; // See BubbleType: 0=SPHERE, 1=CYLINDER 38 int bubble_continuity_type; // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK 39 }; 40 41 typedef struct ShockTubeContext_ *ShockTubeContext; 42 struct ShockTubeContext_ { 43 CeedScalar Cyzb; 44 CeedScalar Byzb; 45 CeedScalar c_tau; 46 bool implicit; 47 bool yzb; 48 int stabilization; 49 }; 50 51 // ***************************************************************************** 52 // This function sets the initial conditions 53 // 54 // Temperature: 55 // T = P / (rho * R) 56 // Density: 57 // rho = 1.0 if x <= mid_point 58 // = 0.125 if x > mid_point 59 // Pressure: 60 // P = 1.0 if x <= mid_point 61 // = 0.1 if x > mid_point 62 // Velocity: 63 // u = 0 64 // Velocity/Momentum Density: 65 // Ui = rho ui 66 // Total Energy: 67 // E = P / (gamma - 1) + rho (u u)/2 68 // 69 // Constants: 70 // cv , Specific heat, constant volume 71 // cp , Specific heat, constant pressure 72 // mid_point , Location of initial domain mid_point 73 // gamma = cp / cv, Specific heat ratio 74 // 75 // ***************************************************************************** 76 77 // ***************************************************************************** 78 // This helper function provides support for the exact, time-dependent solution (currently not implemented) and IC formulation for Euler traveling 79 // vortex 80 // ***************************************************************************** 81 CEED_QFUNCTION_HELPER CeedInt Exact_ShockTube(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) { 82 // Context 83 const SetupContextShock context = (SetupContextShock)ctx; 84 const CeedScalar mid_point = context->mid_point; // Midpoint of the domain 85 const CeedScalar P_high = context->P_high; // Driver section pressure 86 const CeedScalar rho_high = context->rho_high; // Driver section density 87 const CeedScalar P_low = context->P_low; // Driven section pressure 88 const CeedScalar rho_low = context->rho_low; // Driven section density 89 90 // Setup 91 const CeedScalar gamma = 1.4; // ratio of specific heats 92 const CeedScalar x = X[0]; // Coordinates 93 94 CeedScalar rho, P, u[3] = {0.}; 95 96 // Initial Conditions 97 if (x <= mid_point) { 98 rho = rho_high; 99 P = P_high; 100 } else { 101 rho = rho_low; 102 P = P_low; 103 } 104 105 // Assign exact solution 106 q[0] = rho; 107 q[1] = rho * u[0]; 108 q[2] = rho * u[1]; 109 q[3] = rho * u[2]; 110 q[4] = P / (gamma - 1.0) + rho * (u[0] * u[0]) / 2.; 111 112 // Return 113 return 0; 114 } 115 116 // ***************************************************************************** 117 // Helper function for computing flux Jacobian 118 // ***************************************************************************** 119 CEED_QFUNCTION_HELPER void ConvectiveFluxJacobian_Euler(CeedScalar dF[3][5][5], const CeedScalar rho, const CeedScalar u[3], const CeedScalar E, 120 const CeedScalar gamma) { 121 CeedScalar u_sq = u[0] * u[0] + u[1] * u[1] + u[2] * u[2]; // Velocity square 122 for (CeedInt i = 0; i < 3; i++) { // Jacobian matrices for 3 directions 123 for (CeedInt j = 0; j < 3; j++) { // Rows of each Jacobian matrix 124 dF[i][j + 1][0] = ((i == j) ? ((gamma - 1.) * (u_sq / 2.)) : 0.) - u[i] * u[j]; 125 for (CeedInt k = 0; k < 3; k++) { // Columns of each Jacobian matrix 126 dF[i][0][k + 1] = ((i == k) ? 1. : 0.); 127 dF[i][j + 1][k + 1] = ((j == k) ? u[i] : 0.) + ((i == k) ? u[j] : 0.) - ((i == j) ? u[k] : 0.) * (gamma - 1.); 128 dF[i][4][k + 1] = ((i == k) ? (E * gamma / rho - (gamma - 1.) * u_sq / 2.) : 0.) - (gamma - 1.) * u[i] * u[k]; 129 } 130 dF[i][j + 1][4] = ((i == j) ? (gamma - 1.) : 0.); 131 } 132 dF[i][4][0] = u[i] * ((gamma - 1.) * u_sq - E * gamma / rho); 133 dF[i][4][4] = u[i] * gamma; 134 } 135 } 136 137 // ***************************************************************************** 138 // Helper function for calculating the covariant length scale in the direction of some 3 element input vector 139 // 140 // Where 141 // vec = vector that length is measured in the direction of 142 // h = covariant element length along vec 143 // ***************************************************************************** 144 CEED_QFUNCTION_HELPER CeedScalar Covariant_length_along_vector(CeedScalar vec[3], const CeedScalar dXdx[3][3]) { 145 CeedScalar vec_norm = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]); 146 CeedScalar vec_dot_jacobian[3] = {0.0}; 147 for (CeedInt i = 0; i < 3; i++) { 148 for (CeedInt j = 0; j < 3; j++) { 149 vec_dot_jacobian[i] += dXdx[j][i] * vec[i]; 150 } 151 } 152 CeedScalar norm_vec_dot_jacobian = 153 sqrt(vec_dot_jacobian[0] * vec_dot_jacobian[0] + vec_dot_jacobian[1] * vec_dot_jacobian[1] + vec_dot_jacobian[2] * vec_dot_jacobian[2]); 154 CeedScalar h = 2.0 * vec_norm / norm_vec_dot_jacobian; 155 return h; 156 } 157 158 // ***************************************************************************** 159 // Helper function for computing Tau elements (stabilization constant) 160 // Model from: 161 // Stabilized Methods for Compressible Flows, Hughes et al 2010 162 // 163 // Spatial criterion #2 - Tau is a 3x3 diagonal matrix 164 // Tau[i] = c_tau h[i] Xi(Pe) / rho(A[i]) (no sum) 165 // 166 // Where 167 // c_tau = stabilization constant (0.5 is reported as "optimal") 168 // h[i] = 2 length(dxdX[i]) 169 // Pe = Peclet number ( Pe = sqrt(u u) / dot(dXdx,u) diffusivity ) 170 // Xi(Pe) = coth Pe - 1. / Pe (1. at large local Peclet number ) 171 // rho(A[i]) = spectral radius of the convective flux Jacobian i, wave speed in direction i 172 // ***************************************************************************** 173 CEED_QFUNCTION_HELPER void Tau_spatial(CeedScalar Tau_x[3], const CeedScalar dXdx[3][3], const CeedScalar u[3], const CeedScalar sound_speed, 174 const CeedScalar c_tau) { 175 for (CeedInt i = 0; i < 3; i++) { 176 // length of element in direction i 177 CeedScalar h = 2 / sqrt(dXdx[0][i] * dXdx[0][i] + dXdx[1][i] * dXdx[1][i] + dXdx[2][i] * dXdx[2][i]); 178 // fastest wave in direction i 179 CeedScalar fastest_wave = fabs(u[i]) + sound_speed; 180 Tau_x[i] = c_tau * h / fastest_wave; 181 } 182 } 183 184 // ***************************************************************************** 185 // This QFunction sets the initial conditions for shock tube 186 // ***************************************************************************** 187 CEED_QFUNCTION(ICsShockTube)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 188 // Inputs 189 const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 190 191 // Outputs 192 CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 193 194 CeedPragmaSIMD 195 // Quadrature Point Loop 196 for (CeedInt i = 0; i < Q; i++) { 197 const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]}; 198 CeedScalar q[5]; 199 200 Exact_ShockTube(3, 0., x, 5, q, ctx); 201 202 for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j]; 203 } // End of Quadrature Point Loop 204 205 // Return 206 return 0; 207 } 208 209 // ***************************************************************************** 210 // This QFunction implements the following formulation of Euler equations with explicit time stepping method 211 // 212 // This is 3D Euler for compressible gas dynamics in conservation form with state variables of density, momentum density, and total energy density. 213 // 214 // State Variables: q = ( rho, U1, U2, U3, E ) 215 // rho - Mass Density 216 // Ui - Momentum Density, Ui = rho ui 217 // E - Total Energy Density, E = P / (gamma - 1) + rho (u u)/2 218 // 219 // Euler Equations: 220 // drho/dt + div( U ) = 0 221 // dU/dt + div( rho (u x u) + P I3 ) = 0 222 // dE/dt + div( (E + P) u ) = 0 223 // 224 // Equation of State: 225 // P = (gamma - 1) (E - rho (u u) / 2) 226 // 227 // Constants: 228 // cv , Specific heat, constant volume 229 // cp , Specific heat, constant pressure 230 // g , Gravity 231 // gamma = cp / cv, Specific heat ratio 232 // ***************************************************************************** 233 CEED_QFUNCTION(EulerShockTube)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 234 // Inputs 235 const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 236 const CeedScalar(*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1]; 237 const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2]; 238 239 // Outputs 240 CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 241 CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1]; 242 243 const CeedScalar gamma = 1.4; 244 245 ShockTubeContext context = (ShockTubeContext)ctx; 246 const CeedScalar Cyzb = context->Cyzb; 247 const CeedScalar Byzb = context->Byzb; 248 const CeedScalar c_tau = context->c_tau; 249 250 CeedPragmaSIMD 251 // Quadrature Point Loop 252 for (CeedInt i = 0; i < Q; i++) { 253 // Setup 254 // -- Interp in 255 const CeedScalar rho = q[0][i]; 256 const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho}; 257 const CeedScalar E = q[4][i]; 258 const CeedScalar drho[3] = {dq[0][0][i], dq[1][0][i], dq[2][0][i]}; 259 const CeedScalar dU[3][3] = { 260 {dq[0][1][i], dq[1][1][i], dq[2][1][i]}, 261 {dq[0][2][i], dq[1][2][i], dq[2][2][i]}, 262 {dq[0][3][i], dq[1][3][i], dq[2][3][i]} 263 }; 264 const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]}; 265 // -- Interp-to-Interp q_data 266 const CeedScalar wdetJ = q_data[0][i]; 267 // -- Interp-to-Grad q_data 268 // ---- Inverse of change of coordinate matrix: X_i,j 269 const CeedScalar dXdx[3][3] = { 270 {q_data[1][i], q_data[2][i], q_data[3][i]}, 271 {q_data[4][i], q_data[5][i], q_data[6][i]}, 272 {q_data[7][i], q_data[8][i], q_data[9][i]} 273 }; 274 // dU/dx 275 CeedScalar du[3][3] = {{0}}; 276 CeedScalar drhodx[3] = {0}; 277 CeedScalar dEdx[3] = {0}; 278 CeedScalar dUdx[3][3] = {{0}}; 279 CeedScalar dXdxdXdxT[3][3] = {{0}}; 280 for (CeedInt j = 0; j < 3; j++) { 281 for (CeedInt k = 0; k < 3; k++) { 282 du[j][k] = (dU[j][k] - drho[k] * u[j]) / rho; 283 drhodx[j] += drho[k] * dXdx[k][j]; 284 dEdx[j] += dE[k] * dXdx[k][j]; 285 for (CeedInt l = 0; l < 3; l++) { 286 dUdx[j][k] += dU[j][l] * dXdx[l][k]; 287 dXdxdXdxT[j][k] += dXdx[j][l] * dXdx[k][l]; // dXdx_j,k * dXdx_k,j 288 } 289 } 290 } 291 292 const CeedScalar E_kinetic = 0.5 * rho * (u[0] * u[0] + u[1] * u[1] + u[2] * u[2]), E_internal = E - E_kinetic, 293 P = E_internal * (gamma - 1); // P = pressure 294 295 // The Physics 296 // Zero v and dv so all future terms can safely sum into it 297 for (CeedInt j = 0; j < 5; j++) { 298 v[j][i] = 0; 299 for (CeedInt k = 0; k < 3; k++) dv[k][j][i] = 0; 300 } 301 302 // -- Density 303 // ---- u rho 304 for (CeedInt j = 0; j < 3; j++) dv[j][0][i] += wdetJ * (rho * u[0] * dXdx[j][0] + rho * u[1] * dXdx[j][1] + rho * u[2] * dXdx[j][2]); 305 // -- Momentum 306 // ---- rho (u x u) + P I3 307 for (CeedInt j = 0; j < 3; j++) { 308 for (CeedInt k = 0; k < 3; k++) { 309 dv[k][j + 1][i] += wdetJ * ((rho * u[j] * u[0] + (j == 0 ? P : 0)) * dXdx[k][0] + (rho * u[j] * u[1] + (j == 1 ? P : 0)) * dXdx[k][1] + 310 (rho * u[j] * u[2] + (j == 2 ? P : 0)) * dXdx[k][2]); 311 } 312 } 313 // -- Total Energy Density 314 // ---- (E + P) u 315 for (CeedInt j = 0; j < 3; j++) dv[j][4][i] += wdetJ * (E + P) * (u[0] * dXdx[j][0] + u[1] * dXdx[j][1] + u[2] * dXdx[j][2]); 316 317 // -- YZB stabilization 318 if (context->yzb) { 319 CeedScalar drho_norm = 0.0; // magnitude of the density gradient 320 CeedScalar j_vec[3] = {0.0}; // unit vector aligned with the density gradient 321 CeedScalar h_shock = 0.0; // element lengthscale 322 CeedScalar acoustic_vel = 0.0; // characteristic velocity, acoustic speed 323 CeedScalar tau_shock = 0.0; // timescale 324 CeedScalar nu_shock = 0.0; // artificial diffusion 325 326 // Unit vector aligned with the density gradient 327 drho_norm = sqrt(drhodx[0] * drhodx[0] + drhodx[1] * drhodx[1] + drhodx[2] * drhodx[2]); 328 for (CeedInt j = 0; j < 3; j++) j_vec[j] = drhodx[j] / (drho_norm + 1e-20); 329 330 if (drho_norm == 0.0) { 331 nu_shock = 0.0; 332 } else { 333 h_shock = Covariant_length_along_vector(j_vec, dXdx); 334 h_shock /= Cyzb; 335 acoustic_vel = sqrt(gamma * P / rho); 336 tau_shock = h_shock / (2 * acoustic_vel) * pow(drho_norm * h_shock / rho, Byzb); 337 nu_shock = fabs(tau_shock * acoustic_vel * acoustic_vel); 338 } 339 340 for (CeedInt j = 0; j < 3; j++) dv[j][0][i] -= wdetJ * nu_shock * drhodx[j]; 341 342 for (CeedInt k = 0; k < 3; k++) { 343 for (CeedInt j = 0; j < 3; j++) dv[j][k][i] -= wdetJ * nu_shock * du[k][j]; 344 } 345 346 for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * nu_shock * dEdx[j]; 347 } 348 349 // Stabilization 350 // Need the Jacobian for the advective fluxes for stabilization 351 // indexed as: jacob_F_conv[direction][flux component][solution component] 352 CeedScalar jacob_F_conv[3][5][5] = {{{0.}}}; 353 ConvectiveFluxJacobian_Euler(jacob_F_conv, rho, u, E, gamma); 354 355 // dqdx collects drhodx, dUdx and dEdx in one vector 356 CeedScalar dqdx[5][3]; 357 for (CeedInt j = 0; j < 3; j++) { 358 dqdx[0][j] = drhodx[j]; 359 dqdx[4][j] = dEdx[j]; 360 for (CeedInt k = 0; k < 3; k++) dqdx[k + 1][j] = dUdx[k][j]; 361 } 362 363 // strong_conv = dF/dq * dq/dx (Strong convection) 364 CeedScalar strong_conv[5] = {0}; 365 for (CeedInt j = 0; j < 3; j++) { 366 for (CeedInt k = 0; k < 5; k++) { 367 for (CeedInt l = 0; l < 5; l++) strong_conv[k] += jacob_F_conv[j][k][l] * dqdx[l][j]; 368 } 369 } 370 371 // Stabilization 372 // -- Tau elements 373 const CeedScalar sound_speed = sqrt(gamma * P / rho); 374 CeedScalar Tau_x[3] = {0.}; 375 Tau_spatial(Tau_x, dXdx, u, sound_speed, c_tau); 376 377 CeedScalar stab[5][3] = {0}; 378 switch (context->stabilization) { 379 case 0: // Galerkin 380 break; 381 case 1: // SU 382 for (CeedInt j = 0; j < 3; j++) { 383 for (CeedInt k = 0; k < 5; k++) { 384 for (CeedInt l = 0; l < 5; l++) { 385 stab[k][j] += jacob_F_conv[j][k][l] * Tau_x[j] * strong_conv[l]; 386 } 387 } 388 } 389 for (CeedInt j = 0; j < 5; j++) { 390 for (CeedInt k = 0; k < 3; k++) dv[k][j][i] -= wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]); 391 } 392 break; 393 } 394 395 } // End Quadrature Point Loop 396 397 // Return 398 return 0; 399 } 400 401 #endif // shocktube_h 402