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