1 // Copyright (c) 2017-2025, 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 /// Structs and helper functions regarding the state of a newtonian simulation 10 #pragma once 11 12 #include <ceed/types.h> 13 #ifndef CEED_RUNNING_JIT_PASS 14 #include <math.h> 15 #endif 16 17 #include "newtonian_types.h" 18 #include "utils.h" 19 20 typedef struct { 21 CeedScalar density; 22 CeedScalar momentum[3]; 23 CeedScalar E_total; 24 } StateConservative; 25 26 typedef struct { 27 StateConservative U; 28 StatePrimitive Y; 29 } State; 30 31 CEED_QFUNCTION_HELPER void UnpackState_U(StateConservative s, CeedScalar U[5]) { 32 U[0] = s.density; 33 for (int i = 0; i < 3; i++) U[i + 1] = s.momentum[i]; 34 U[4] = s.E_total; 35 } 36 37 CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) { 38 Y[0] = s.pressure; 39 for (int i = 0; i < 3; i++) Y[i + 1] = s.velocity[i]; 40 Y[4] = s.temperature; 41 } 42 43 CEED_QFUNCTION_HELPER void UnpackState_V(StateEntropy s, CeedScalar V[5]) { 44 V[0] = s.S_density; 45 for (int i = 0; i < 3; i++) V[i + 1] = s.S_momentum[i]; 46 V[4] = s.S_energy; 47 } 48 49 CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio(NewtonianIdealGasContext gas) { return gas->cp / gas->cv; } 50 51 CEED_QFUNCTION_HELPER CeedScalar GasConstant(NewtonianIdealGasContext gas) { return gas->cp - gas->cv; } 52 53 CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) { return gas->cp * gas->mu / gas->k; } 54 55 CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas, CeedScalar T) { return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T); } 56 57 CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas, CeedScalar T, CeedScalar u) { return u / SoundSpeed(gas, T); } 58 59 CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy(NewtonianIdealGasContext gas, const State s) { 60 CeedScalar e_kinetic = 0.5 * Dot3(s.Y.velocity, s.Y.velocity); 61 CeedScalar e_internal = gas->cv * s.Y.temperature; 62 return e_internal + e_kinetic + s.Y.pressure / s.U.density; 63 } 64 65 CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy_fwd(NewtonianIdealGasContext gas, const State s, const State ds) { 66 CeedScalar de_kinetic = Dot3(ds.Y.velocity, s.Y.velocity); 67 CeedScalar de_internal = gas->cv * ds.Y.temperature; 68 return de_internal + de_kinetic + ds.Y.pressure / s.U.density - s.Y.pressure / Square(s.U.density) * ds.U.density; 69 } 70 71 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(NewtonianIdealGasContext gas, StateConservative U) { 72 StatePrimitive Y; 73 for (CeedInt i = 0; i < 3; i++) Y.velocity[i] = U.momentum[i] / U.density; 74 CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 75 CeedScalar e_total = U.E_total / U.density; 76 CeedScalar e_internal = e_total - e_kinetic; 77 Y.temperature = e_internal / gas->cv; 78 Y.pressure = (HeatCapacityRatio(gas) - 1) * U.density * e_internal; 79 return Y; 80 } 81 82 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) { 83 StatePrimitive dY; 84 for (CeedInt i = 0; i < 3; i++) { 85 dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density; 86 } 87 CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 88 CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 89 CeedScalar e_total = s.U.E_total / s.U.density; 90 CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density; 91 CeedScalar e_internal = e_total - e_kinetic; 92 CeedScalar de_internal = de_total - de_kinetic; 93 dY.temperature = de_internal / gas->cv; 94 dY.pressure = (HeatCapacityRatio(gas) - 1) * (dU.density * e_internal + s.U.density * de_internal); 95 return dY; 96 } 97 98 CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) { 99 StateEntropy V; 100 const CeedScalar gamma = HeatCapacityRatio(gas); 101 const CeedScalar rho = Y.pressure / (GasConstant(gas) * Y.temperature); 102 const CeedScalar entropy = log(Y.pressure) - gamma * log(rho); 103 const CeedScalar rho_div_p = rho / Y.pressure; 104 const CeedScalar e_kinetic = 0.5 * Dot3(Y.velocity, Y.velocity); 105 106 V.S_density = (gamma - entropy) / (gamma - 1) - rho_div_p * e_kinetic; 107 for (int i = 0; i < 3; i++) V.S_momentum[i] = rho_div_p * Y.velocity[i]; 108 V.S_energy = -rho_div_p; 109 return V; 110 } 111 112 CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) { 113 StateEntropy dV; 114 const CeedScalar gamma = HeatCapacityRatio(gas); 115 CeedScalar drho = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature); 116 117 const CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 118 const CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 119 const CeedScalar rho_div_p = s.U.density / s.Y.pressure; 120 const CeedScalar drho_div_p = (drho * s.Y.pressure - s.U.density * dY.pressure) / Square(s.Y.pressure); 121 122 CeedScalar dentropy = dY.pressure / s.Y.pressure - gamma * drho / s.U.density; 123 124 dV.S_density = -dentropy / (gamma - 1) - de_kinetic * rho_div_p - e_kinetic * drho_div_p; 125 for (CeedInt i = 0; i < 3; i++) dV.S_momentum[i] = rho_div_p * dY.velocity[i] + drho_div_p * s.Y.velocity[i]; 126 dV.S_energy = -drho_div_p; 127 return dV; 128 } 129 130 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromEntropy(NewtonianIdealGasContext gas, StateEntropy V) { 131 StatePrimitive Y; 132 for (int i = 0; i < 3; i++) Y.velocity[i] = -V.S_momentum[i] / V.S_energy; 133 Y.temperature = -1 / (GasConstant(gas) * V.S_energy); 134 const CeedScalar gamma = HeatCapacityRatio(gas); 135 const CeedScalar e_kinetic = 0.5 * Dot3(Y.velocity, Y.velocity); 136 const CeedScalar entropy = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy); 137 const CeedScalar log_P = -(entropy + gamma * log(-V.S_energy)) / (gamma - 1); 138 Y.pressure = exp(log_P); 139 return Y; 140 } 141 142 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromEntropy_fwd(NewtonianIdealGasContext gas, State s, StateEntropy dV) { 143 StatePrimitive dY; 144 StateEntropy V = StateEntropyFromPrimitive(gas, s.Y); 145 for (int i = 0; i < 3; i++) dY.velocity[i] = -(dV.S_momentum[i] - V.S_momentum[i] * dV.S_energy / V.S_energy) / V.S_energy; 146 dY.temperature = dV.S_energy / (GasConstant(gas) * V.S_energy * V.S_energy); 147 const CeedScalar gamma = HeatCapacityRatio(gas); 148 const CeedScalar e_kinetic = 0.5 * Dot3(s.Y.velocity, s.Y.velocity); 149 const CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 150 const CeedScalar dentropy = (1 - gamma) * (dV.S_density - e_kinetic * dV.S_energy - de_kinetic * V.S_energy); 151 dY.pressure = s.Y.pressure * (-dentropy - gamma * dV.S_energy / V.S_energy) / (gamma - 1); 152 return dY; 153 } 154 155 CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) { 156 StateConservative U; 157 U.density = Y.pressure / (GasConstant(gas) * Y.temperature); 158 for (int i = 0; i < 3; i++) U.momentum[i] = U.density * Y.velocity[i]; 159 CeedScalar e_internal = gas->cv * Y.temperature; 160 CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 161 CeedScalar e_total = e_internal + e_kinetic; 162 U.E_total = U.density * e_total; 163 return U; 164 } 165 166 CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) { 167 StateConservative dU; 168 dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature); 169 for (int i = 0; i < 3; i++) { 170 dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i]; 171 } 172 CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 173 CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 174 CeedScalar e_internal = gas->cv * s.Y.temperature; 175 CeedScalar de_internal = gas->cv * dY.temperature; 176 CeedScalar e_total = e_internal + e_kinetic; 177 CeedScalar de_total = de_internal + de_kinetic; 178 dU.E_total = dU.density * e_total + s.U.density * de_total; 179 return dU; 180 } 181 182 CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromConservative(NewtonianIdealGasContext gas, StateConservative U) { 183 StateEntropy V; 184 const CeedScalar gamma = HeatCapacityRatio(gas); 185 const CeedScalar e_kinetic = .5 * Dot3(U.momentum, U.momentum) / U.density; 186 const CeedScalar e_internal = U.E_total - e_kinetic; 187 const CeedScalar p = (gamma - 1) * e_internal; 188 const CeedScalar entropy = log(p) - gamma * log(U.density); 189 190 V.S_density = (gamma - entropy) / (gamma - 1) - e_kinetic / p; 191 for (int i = 0; i < 3; i++) V.S_momentum[i] = U.momentum[i] / p; 192 V.S_energy = -U.density / p; 193 return V; 194 } 195 196 CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) { 197 StateEntropy dV; 198 const CeedScalar gamma = HeatCapacityRatio(gas); 199 const CeedScalar e_kinetic = .5 * Dot3(s.U.momentum, s.U.momentum) / s.U.density; 200 const CeedScalar de_kinetic = (Dot3(s.U.momentum, dU.momentum) - e_kinetic * dU.density) / s.U.density; 201 const CeedScalar de_internal = dU.E_total - de_kinetic; 202 const CeedScalar p = s.Y.pressure; 203 const CeedScalar dp = (gamma - 1) * de_internal; 204 205 CeedScalar dentropy = dp / p - gamma * dU.density / s.U.density; 206 207 dV.S_density = -dentropy / (gamma - 1) - de_kinetic / p + dp * e_kinetic / Square(p); 208 for (CeedInt i = 0; i < 3; i++) { 209 dV.S_momentum[i] = (dU.momentum[i] - s.U.momentum[i] * dp / p) / p; 210 } 211 dV.S_energy = -(dU.density - s.U.density * dp / p) / p; 212 return dV; 213 } 214 215 CEED_QFUNCTION_HELPER StateConservative StateConservativeFromEntropy(NewtonianIdealGasContext gas, StateEntropy V) { 216 StateConservative U; 217 CeedScalar velocity[3]; 218 for (int i = 0; i < 3; i++) velocity[i] = -V.S_momentum[i] / V.S_energy; 219 const CeedScalar gamma = HeatCapacityRatio(gas); 220 const CeedScalar e_kinetic = 0.5 * Dot3(velocity, velocity); 221 const CeedScalar entropy = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy); 222 const CeedScalar log_rho = -(entropy + log(-V.S_energy)) / (gamma - 1); 223 U.density = exp(log_rho); 224 for (int i = 0; i < 3; i++) U.momentum[i] = U.density * velocity[i]; 225 226 const CeedScalar e_internal = -gas->cv / (GasConstant(gas) * V.S_energy); 227 U.E_total = U.density * (e_internal + e_kinetic); 228 return U; 229 } 230 231 CEED_QFUNCTION_HELPER StateConservative StateConservativeFromEntropy_fwd(NewtonianIdealGasContext gas, State s, StateEntropy dV) { 232 StateConservative dU; 233 CeedScalar dvelocity[3]; 234 StateEntropy V = StateEntropyFromPrimitive(gas, s.Y); 235 for (int i = 0; i < 3; i++) dvelocity[i] = (-dV.S_momentum[i] - s.Y.velocity[i] * dV.S_energy) / V.S_energy; 236 const CeedScalar gamma = HeatCapacityRatio(gas); 237 const CeedScalar e_kinetic = 0.5 * Dot3(s.Y.velocity, s.Y.velocity); 238 const CeedScalar de_kinetic = Dot3(dvelocity, s.Y.velocity); 239 const CeedScalar entropy = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy); 240 const CeedScalar dentropy = -(gamma - 1) * (dV.S_density - (de_kinetic * V.S_energy + e_kinetic * dV.S_energy)); 241 const CeedScalar log_rho = -(entropy + log(-V.S_energy)) / (gamma - 1); 242 const CeedScalar rho = exp(log_rho); 243 dU.density = -rho / (gamma - 1) * (dentropy + dV.S_energy / V.S_energy); 244 for (int i = 0; i < 3; i++) dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dvelocity[i]; 245 246 const CeedScalar e_internal = -gas->cv / (GasConstant(gas) * V.S_energy); 247 const CeedScalar de_internal = gas->cv * dV.S_energy / (GasConstant(gas) * V.S_energy * V.S_energy); 248 const CeedScalar e_total = e_internal + e_kinetic; 249 dU.E_total = dU.density * e_total + s.U.density * (de_internal + de_kinetic); 250 return dU; 251 } 252 253 CEED_QFUNCTION_HELPER State StateFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) { 254 StateConservative U = StateConservativeFromPrimitive(gas, Y); 255 State s; 256 s.U = U; 257 s.Y = Y; 258 return s; 259 } 260 261 CEED_QFUNCTION_HELPER State StateFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) { 262 StateConservative dU = StateConservativeFromPrimitive_fwd(gas, s, dY); 263 State ds; 264 ds.U = dU; 265 ds.Y = dY; 266 return ds; 267 } 268 269 // linear combination of n states 270 CEED_QFUNCTION_HELPER StateConservative StateConservativeMult(CeedInt n, const CeedScalar a[], const StateConservative X[]) { 271 StateConservative R = {0}; 272 for (CeedInt i = 0; i < n; i++) { 273 R.density += a[i] * X[i].density; 274 for (int j = 0; j < 3; j++) R.momentum[j] += a[i] * X[i].momentum[j]; 275 R.E_total += a[i] * X[i].E_total; 276 } 277 return R; 278 } 279 280 CEED_QFUNCTION_HELPER StateConservative StateConservativeAXPBYPCZ(CeedScalar a, StateConservative X, CeedScalar b, StateConservative Y, CeedScalar c, 281 StateConservative Z) { 282 StateConservative R; 283 R.density = a * X.density + b * Y.density + c * Z.density; 284 for (int i = 0; i < 3; i++) R.momentum[i] = a * X.momentum[i] + b * Y.momentum[i] + c * Z.momentum[i]; 285 R.E_total = a * X.E_total + b * Y.E_total + c * Z.E_total; 286 return R; 287 } 288 289 CEED_QFUNCTION_HELPER void StateToU(NewtonianIdealGasContext gas, const State input, CeedScalar U[5]) { UnpackState_U(input.U, U); } 290 291 CEED_QFUNCTION_HELPER void StateToY(NewtonianIdealGasContext gas, const State input, CeedScalar Y[5]) { UnpackState_Y(input.Y, Y); } 292 293 CEED_QFUNCTION_HELPER void StateToV(NewtonianIdealGasContext gas, const State input, CeedScalar V[5]) { 294 StateEntropy state_V = StateEntropyFromPrimitive(gas, input.Y); 295 UnpackState_V(state_V, V); 296 } 297 298 CEED_QFUNCTION_HELPER void StateToQ(NewtonianIdealGasContext gas, const State input, CeedScalar Q[5], StateVariable state_var) { 299 switch (state_var) { 300 case STATEVAR_CONSERVATIVE: 301 StateToU(gas, input, Q); 302 break; 303 case STATEVAR_PRIMITIVE: 304 StateToY(gas, input, Q); 305 break; 306 case STATEVAR_ENTROPY: 307 StateToV(gas, input, Q); 308 break; 309 } 310 } 311 312 CEED_QFUNCTION_HELPER void StateToQ_fwd(NewtonianIdealGasContext gas, const State input, const State dinput, CeedScalar dQ[5], 313 StateVariable state_var) { 314 switch (state_var) { 315 case STATEVAR_CONSERVATIVE: 316 case STATEVAR_PRIMITIVE: 317 StateToQ(gas, dinput, dQ, state_var); 318 break; 319 case STATEVAR_ENTROPY: { 320 StateEntropy dstate_v; 321 322 dstate_v = StateEntropyFromPrimitive_fwd(gas, input, dinput.Y); 323 UnpackState_V(dstate_v, dQ); 324 } break; 325 } 326 } 327 328 CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, const CeedScalar U[5]) { 329 State s; 330 s.U.density = U[0]; 331 s.U.momentum[0] = U[1]; 332 s.U.momentum[1] = U[2]; 333 s.U.momentum[2] = U[3]; 334 s.U.E_total = U[4]; 335 s.Y = StatePrimitiveFromConservative(gas, s.U); 336 return s; 337 } 338 339 CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dU[5]) { 340 State ds; 341 ds.U.density = dU[0]; 342 ds.U.momentum[0] = dU[1]; 343 ds.U.momentum[1] = dU[2]; 344 ds.U.momentum[2] = dU[3]; 345 ds.U.E_total = dU[4]; 346 ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U); 347 return ds; 348 } 349 350 CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, const CeedScalar Y[5]) { 351 State s; 352 s.Y.pressure = Y[0]; 353 s.Y.velocity[0] = Y[1]; 354 s.Y.velocity[1] = Y[2]; 355 s.Y.velocity[2] = Y[3]; 356 s.Y.temperature = Y[4]; 357 s.U = StateConservativeFromPrimitive(gas, s.Y); 358 return s; 359 } 360 361 CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dY[5]) { 362 State ds; 363 ds.Y.pressure = dY[0]; 364 ds.Y.velocity[0] = dY[1]; 365 ds.Y.velocity[1] = dY[2]; 366 ds.Y.velocity[2] = dY[3]; 367 ds.Y.temperature = dY[4]; 368 ds.U = StateConservativeFromPrimitive_fwd(gas, s, ds.Y); 369 return ds; 370 } 371 372 CEED_QFUNCTION_HELPER State StateFromV(NewtonianIdealGasContext gas, const CeedScalar V[5]) { 373 State s; 374 StateEntropy state_V; 375 state_V.S_density = V[0]; 376 state_V.S_momentum[0] = V[1]; 377 state_V.S_momentum[1] = V[2]; 378 state_V.S_momentum[2] = V[3]; 379 state_V.S_energy = V[4]; 380 s.U = StateConservativeFromEntropy(gas, state_V); 381 s.Y = StatePrimitiveFromEntropy(gas, state_V); 382 return s; 383 } 384 385 CEED_QFUNCTION_HELPER State StateFromV_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dV[5]) { 386 State ds; 387 StateEntropy state_dV; 388 state_dV.S_density = dV[0]; 389 state_dV.S_momentum[0] = dV[1]; 390 state_dV.S_momentum[1] = dV[2]; 391 state_dV.S_momentum[2] = dV[3]; 392 state_dV.S_energy = dV[4]; 393 ds.U = StateConservativeFromEntropy_fwd(gas, s, state_dV); 394 ds.Y = StatePrimitiveFromEntropy_fwd(gas, s, state_dV); 395 return ds; 396 } 397 398 CEED_QFUNCTION_HELPER State StateFromQ(NewtonianIdealGasContext gas, const CeedScalar Q[5], StateVariable state_var) { 399 State s; 400 switch (state_var) { 401 case STATEVAR_CONSERVATIVE: 402 s = StateFromU(gas, Q); 403 break; 404 case STATEVAR_PRIMITIVE: 405 s = StateFromY(gas, Q); 406 break; 407 case STATEVAR_ENTROPY: 408 s = StateFromV(gas, Q); 409 break; 410 } 411 return s; 412 } 413 414 CEED_QFUNCTION_HELPER State StateFromQ_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dQ[5], StateVariable state_var) { 415 State ds; 416 switch (state_var) { 417 case STATEVAR_CONSERVATIVE: 418 ds = StateFromU_fwd(gas, s, dQ); 419 break; 420 case STATEVAR_PRIMITIVE: 421 ds = StateFromY_fwd(gas, s, dQ); 422 break; 423 case STATEVAR_ENTROPY: 424 ds = StateFromV_fwd(gas, s, dQ); 425 break; 426 } 427 return ds; 428 } 429 430 CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, StateConservative Flux[3]) { 431 for (CeedInt i = 0; i < 3; i++) { 432 Flux[i].density = s.U.momentum[i]; 433 for (CeedInt j = 0; j < 3; j++) Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j] + s.Y.pressure * (i == j); 434 Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i]; 435 } 436 } 437 438 CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, State s, State ds, StateConservative dFlux[3]) { 439 for (CeedInt i = 0; i < 3; i++) { 440 dFlux[i].density = ds.U.momentum[i]; 441 for (CeedInt j = 0; j < 3; j++) { 442 dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] + s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j); 443 } 444 dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] + (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i]; 445 } 446 } 447 448 CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal(NewtonianIdealGasContext gas, State s, const CeedScalar normal[3]) { 449 StateConservative Flux[3], Flux_dot_n = {0}; 450 FluxInviscid(gas, s, Flux); 451 for (CeedInt i = 0; i < 3; i++) { 452 Flux_dot_n.density += Flux[i].density * normal[i]; 453 for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += Flux[i].momentum[j] * normal[i]; 454 Flux_dot_n.E_total += Flux[i].E_total * normal[i]; 455 } 456 return Flux_dot_n; 457 } 458 459 CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal_fwd(NewtonianIdealGasContext gas, State s, State ds, const CeedScalar normal[3]) { 460 StateConservative dFlux[3], Flux_dot_n = {0}; 461 FluxInviscid_fwd(gas, s, ds, dFlux); 462 for (CeedInt i = 0; i < 3; i++) { 463 Flux_dot_n.density += dFlux[i].density * normal[i]; 464 for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += dFlux[i].momentum[j] * normal[i]; 465 Flux_dot_n.E_total += dFlux[i].E_total * normal[i]; 466 } 467 return Flux_dot_n; 468 } 469 470 CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, State s, State ds[3], CeedScalar strong_conv[5]) { 471 for (CeedInt i = 0; i < 5; i++) strong_conv[i] = 0; 472 for (CeedInt i = 0; i < 3; i++) { 473 StateConservative dF[3]; 474 FluxInviscid_fwd(gas, s, ds[i], dF); 475 CeedScalar dF_i[5]; 476 UnpackState_U(dF[i], dF_i); 477 for (CeedInt j = 0; j < 5; j++) strong_conv[j] += dF_i[j]; 478 } 479 } 480 481 CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3], CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) { 482 for (CeedInt j = 0; j < 3; j++) { 483 Flux[0][j] = F_inviscid[j].density; 484 for (CeedInt k = 0; k < 3; k++) Flux[k + 1][j] = F_inviscid[j].momentum[k] - stress[k][j]; 485 Flux[4][j] = F_inviscid[j].E_total + Fe[j]; 486 } 487 } 488 489 CEED_QFUNCTION_HELPER void FluxTotal_Boundary(const StateConservative F_inviscid[3], const CeedScalar stress[3][3], const CeedScalar Fe[3], 490 const CeedScalar normal[3], CeedScalar Flux[5]) { 491 for (CeedInt j = 0; j < 5; j++) Flux[j] = 0.; 492 for (CeedInt j = 0; j < 3; j++) { 493 Flux[0] += F_inviscid[j].density * normal[j]; 494 for (CeedInt k = 0; k < 3; k++) { 495 Flux[k + 1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j]; 496 } 497 Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j]; 498 } 499 } 500 501 CEED_QFUNCTION_HELPER void FluxTotal_RiemannBoundary(const StateConservative F_inviscid_normal, const CeedScalar stress[3][3], const CeedScalar Fe[3], 502 const CeedScalar normal[3], CeedScalar Flux[5]) { 503 Flux[0] = F_inviscid_normal.density; 504 for (CeedInt k = 0; k < 3; k++) Flux[k + 1] = F_inviscid_normal.momentum[k]; 505 Flux[4] = F_inviscid_normal.E_total; 506 for (CeedInt j = 0; j < 3; j++) { 507 for (CeedInt k = 0; k < 3; k++) { 508 Flux[k + 1] -= stress[k][j] * normal[j]; 509 } 510 Flux[4] += Fe[j] * normal[j]; 511 } 512 } 513 514 CEED_QFUNCTION_HELPER void VelocityGradient(const State grad_s[3], CeedScalar grad_velocity[3][3]) { 515 grad_velocity[0][0] = grad_s[0].Y.velocity[0]; 516 grad_velocity[0][1] = grad_s[1].Y.velocity[0]; 517 grad_velocity[0][2] = grad_s[2].Y.velocity[0]; 518 grad_velocity[1][0] = grad_s[0].Y.velocity[1]; 519 grad_velocity[1][1] = grad_s[1].Y.velocity[1]; 520 grad_velocity[1][2] = grad_s[2].Y.velocity[1]; 521 grad_velocity[2][0] = grad_s[0].Y.velocity[2]; 522 grad_velocity[2][1] = grad_s[1].Y.velocity[2]; 523 grad_velocity[2][2] = grad_s[2].Y.velocity[2]; 524 } 525 526 CEED_QFUNCTION_HELPER void KMStrainRate(const CeedScalar grad_velocity[3][3], CeedScalar strain_rate[6]) { 527 const CeedScalar weight = 1 / sqrt(2.); // Really sqrt(2.) / 2 528 strain_rate[0] = grad_velocity[0][0]; 529 strain_rate[1] = grad_velocity[1][1]; 530 strain_rate[2] = grad_velocity[2][2]; 531 strain_rate[3] = weight * (grad_velocity[1][2] + grad_velocity[2][1]); 532 strain_rate[4] = weight * (grad_velocity[0][2] + grad_velocity[2][0]); 533 strain_rate[5] = weight * (grad_velocity[0][1] + grad_velocity[1][0]); 534 } 535 536 // Kelvin-Mandel notation 537 CEED_QFUNCTION_HELPER void KMStrainRate_State(const State grad_s[3], CeedScalar strain_rate[6]) { 538 CeedScalar grad_velocity[3][3]; 539 VelocityGradient(grad_s, grad_velocity); 540 KMStrainRate(grad_velocity, strain_rate); 541 } 542 543 //@brief Given velocity gradient du_i/dx_j, return 0.5*(du_i/dx_j - du_j/dx_i) 544 CEED_QFUNCTION_HELPER void RotationRate(const CeedScalar grad_velocity[3][3], CeedScalar rotation_rate[3][3]) { 545 rotation_rate[0][0] = 0; 546 rotation_rate[1][1] = 0; 547 rotation_rate[2][2] = 0; 548 rotation_rate[1][2] = 0.5 * (grad_velocity[1][2] - grad_velocity[2][1]); 549 rotation_rate[0][2] = 0.5 * (grad_velocity[0][2] - grad_velocity[2][0]); 550 rotation_rate[0][1] = 0.5 * (grad_velocity[0][1] - grad_velocity[1][0]); 551 rotation_rate[2][1] = -rotation_rate[1][2]; 552 rotation_rate[2][0] = -rotation_rate[0][2]; 553 rotation_rate[1][0] = -rotation_rate[0][1]; 554 } 555 556 CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, const CeedScalar strain_rate[6], CeedScalar stress[6]) { 557 CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2]; 558 for (CeedInt i = 0; i < 6; i++) { 559 stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3)); 560 } 561 } 562 563 CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3], 564 CeedScalar Fe[3]) { 565 for (CeedInt i = 0; i < 3; i++) { 566 Fe[i] = -Y.velocity[0] * stress[0][i] - Y.velocity[1] * stress[1][i] - Y.velocity[2] * stress[2][i] - gas->k * grad_s[i].Y.temperature; 567 } 568 } 569 570 CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, StatePrimitive Y, StatePrimitive dY, const State grad_ds[3], 571 const CeedScalar stress[3][3], const CeedScalar dstress[3][3], CeedScalar dFe[3]) { 572 for (CeedInt i = 0; i < 3; i++) { 573 dFe[i] = -Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i] - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i] - 574 Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] - gas->k * grad_ds[i].Y.temperature; 575 } 576 } 577 578 CEED_QFUNCTION_HELPER void Vorticity(const State grad_s[3], CeedScalar vorticity[3]) { 579 CeedScalar grad_velocity[3][3]; 580 VelocityGradient(grad_s, grad_velocity); 581 Curl3(grad_velocity, vorticity); 582 } 583 584 CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s, StateVariable state_var, 585 const CeedScalar *grad_q, const CeedScalar dXdx[3][3], State grad_s[3]) { 586 for (CeedInt k = 0; k < 3; k++) { 587 CeedScalar dqi[5]; 588 for (CeedInt j = 0; j < 5; j++) { 589 dqi[j] = 590 grad_q[(Q * 5) * 0 + Q * j + i] * dXdx[0][k] + grad_q[(Q * 5) * 1 + Q * j + i] * dXdx[1][k] + grad_q[(Q * 5) * 2 + Q * j + i] * dXdx[2][k]; 591 } 592 grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var); 593 } 594 } 595 596 CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_Boundary(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s, 597 StateVariable state_var, const CeedScalar *grad_q, const CeedScalar dXdx[2][3], 598 State grad_s[3]) { 599 for (CeedInt k = 0; k < 3; k++) { 600 CeedScalar dqi[5]; 601 for (CeedInt j = 0; j < 5; j++) { 602 dqi[j] = grad_q[(Q * 5) * 0 + Q * j + i] * dXdx[0][k] + grad_q[(Q * 5) * 1 + Q * j + i] * dXdx[1][k]; 603 } 604 grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var); 605 } 606 } 607