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 /// Structs and helper functions regarding the state of a newtonian simulation 10 11 #ifndef newtonian_state_h 12 #define newtonian_state_h 13 14 #include <ceed.h> 15 #include <math.h> 16 17 #include "newtonian_types.h" 18 #include "utils.h" 19 20 typedef struct { 21 CeedScalar pressure; 22 CeedScalar velocity[3]; 23 CeedScalar temperature; 24 } StatePrimitive; 25 26 typedef struct { 27 CeedScalar density; 28 CeedScalar momentum[3]; 29 CeedScalar E_total; 30 } StateConservative; 31 32 typedef struct { 33 StateConservative U; 34 StatePrimitive Y; 35 } State; 36 37 CEED_QFUNCTION_HELPER void UnpackState_U(StateConservative s, CeedScalar U[5]) { 38 U[0] = s.density; 39 for (int i = 0; i < 3; i++) U[i + 1] = s.momentum[i]; 40 U[4] = s.E_total; 41 } 42 43 CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) { 44 Y[0] = s.pressure; 45 for (int i = 0; i < 3; i++) Y[i + 1] = s.velocity[i]; 46 Y[4] = s.temperature; 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 // Ignoring potential energy 61 CeedScalar e_internal = gas->cv * s.Y.temperature; 62 CeedScalar e_kinetic = 0.5 * Dot3(s.Y.velocity, s.Y.velocity); 63 return e_internal + e_kinetic + s.Y.pressure / s.U.density; 64 } 65 66 CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy_fwd(NewtonianIdealGasContext gas, const State s, const State ds) { 67 // Ignoring potential energy 68 CeedScalar de_kinetic = Dot3(ds.Y.velocity, s.Y.velocity); 69 CeedScalar de_internal = gas->cv * ds.Y.temperature; 70 return de_internal + de_kinetic + ds.Y.pressure / s.U.density - s.Y.pressure / Square(s.U.density) * ds.U.density; 71 } 72 73 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) { 74 StatePrimitive Y; 75 for (CeedInt i = 0; i < 3; i++) Y.velocity[i] = U.momentum[i] / U.density; 76 CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 77 CeedScalar e_potential = -Dot3(gas->g, x); 78 CeedScalar e_total = U.E_total / U.density; 79 CeedScalar e_internal = e_total - e_kinetic - e_potential; 80 Y.temperature = e_internal / gas->cv; 81 Y.pressure = (HeatCapacityRatio(gas) - 1) * U.density * e_internal; 82 return Y; 83 } 84 85 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU, 86 const CeedScalar x[3], const CeedScalar dx[3]) { 87 StatePrimitive dY; 88 for (CeedInt i = 0; i < 3; i++) { 89 dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density; 90 } 91 CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 92 CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 93 CeedScalar e_potential = -Dot3(gas->g, x); 94 CeedScalar de_potential = -Dot3(gas->g, dx); 95 CeedScalar e_total = s.U.E_total / s.U.density; 96 CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density; 97 CeedScalar e_internal = e_total - e_kinetic - e_potential; 98 CeedScalar de_internal = de_total - de_kinetic - de_potential; 99 dY.temperature = de_internal / gas->cv; 100 dY.pressure = (HeatCapacityRatio(gas) - 1) * (dU.density * e_internal + s.U.density * de_internal); 101 return dY; 102 } 103 104 CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y, const CeedScalar x[3]) { 105 StateConservative U; 106 U.density = Y.pressure / (GasConstant(gas) * Y.temperature); 107 for (int i = 0; i < 3; i++) U.momentum[i] = U.density * Y.velocity[i]; 108 CeedScalar e_internal = gas->cv * Y.temperature; 109 CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 110 CeedScalar e_potential = -Dot3(gas->g, x); 111 CeedScalar e_total = e_internal + e_kinetic + e_potential; 112 U.E_total = U.density * e_total; 113 return U; 114 } 115 116 CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY, 117 const CeedScalar x[3], const CeedScalar dx[3]) { 118 StateConservative dU; 119 dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature); 120 for (int i = 0; i < 3; i++) { 121 dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i]; 122 } 123 CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 124 CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 125 CeedScalar e_potential = -Dot3(gas->g, x); 126 CeedScalar de_potential = -Dot3(gas->g, dx); 127 CeedScalar e_internal = gas->cv * s.Y.temperature; 128 CeedScalar de_internal = gas->cv * dY.temperature; 129 CeedScalar e_total = e_internal + e_kinetic + e_potential; 130 CeedScalar de_total = de_internal + de_kinetic + de_potential; 131 dU.E_total = dU.density * e_total + s.U.density * de_total; 132 return dU; 133 } 134 135 CEED_QFUNCTION_HELPER StateConservative StateConservativeAXPBY(CeedScalar a, StateConservative X, CeedScalar b, StateConservative Y) { 136 StateConservative R; 137 R.density = a * X.density + b * Y.density; 138 for (int i = 0; i < 3; i++) R.momentum[i] = a * X.momentum[i] + b * Y.momentum[i]; 139 R.E_total = a * X.E_total + b * Y.E_total; 140 return R; 141 } 142 143 CEED_QFUNCTION_HELPER StateConservative StateConservativeAXPBYPCZ(CeedScalar a, StateConservative X, CeedScalar b, StateConservative Y, CeedScalar c, 144 StateConservative Z) { 145 StateConservative R; 146 R.density = a * X.density + b * Y.density + c * Z.density; 147 for (int i = 0; i < 3; i++) R.momentum[i] = a * X.momentum[i] + b * Y.momentum[i] + c * Z.momentum[i]; 148 R.E_total = a * X.E_total + b * Y.E_total + c * Z.E_total; 149 return R; 150 } 151 152 // Function pointer types for generic state array -> State struct functions 153 typedef State (*StateFromQi_t)(NewtonianIdealGasContext gas, const CeedScalar qi[5], const CeedScalar x[3]); 154 typedef State (*StateFromQi_fwd_t)(NewtonianIdealGasContext gas, State s, const CeedScalar dqi[5], const CeedScalar x[3], const CeedScalar dx[3]); 155 156 CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, const CeedScalar U[5], const CeedScalar x[3]) { 157 State s; 158 s.U.density = U[0]; 159 s.U.momentum[0] = U[1]; 160 s.U.momentum[1] = U[2]; 161 s.U.momentum[2] = U[3]; 162 s.U.E_total = U[4]; 163 s.Y = StatePrimitiveFromConservative(gas, s.U, x); 164 return s; 165 } 166 167 CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dU[5], const CeedScalar x[3], 168 const CeedScalar dx[3]) { 169 State ds; 170 ds.U.density = dU[0]; 171 ds.U.momentum[0] = dU[1]; 172 ds.U.momentum[1] = dU[2]; 173 ds.U.momentum[2] = dU[3]; 174 ds.U.E_total = dU[4]; 175 ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx); 176 return ds; 177 } 178 179 CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, const CeedScalar Y[5], const CeedScalar x[3]) { 180 State s; 181 s.Y.pressure = Y[0]; 182 s.Y.velocity[0] = Y[1]; 183 s.Y.velocity[1] = Y[2]; 184 s.Y.velocity[2] = Y[3]; 185 s.Y.temperature = Y[4]; 186 s.U = StateConservativeFromPrimitive(gas, s.Y, x); 187 return s; 188 } 189 190 CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dY[5], const CeedScalar x[3], 191 const CeedScalar dx[3]) { 192 State ds; 193 ds.Y.pressure = dY[0]; 194 ds.Y.velocity[0] = dY[1]; 195 ds.Y.velocity[1] = dY[2]; 196 ds.Y.velocity[2] = dY[3]; 197 ds.Y.temperature = dY[4]; 198 ds.U = StateConservativeFromPrimitive_fwd(gas, s, ds.Y, x, dx); 199 return ds; 200 } 201 202 // Function pointer types for State struct -> generic state array 203 typedef void (*StateToQi_t)(NewtonianIdealGasContext gas, const State input, CeedScalar qi[5]); 204 205 CEED_QFUNCTION_HELPER void StateToU(NewtonianIdealGasContext gas, const State input, CeedScalar U[5]) { UnpackState_U(input.U, U); } 206 207 CEED_QFUNCTION_HELPER void StateToY(NewtonianIdealGasContext gas, const State input, CeedScalar Y[5]) { UnpackState_Y(input.Y, Y); } 208 209 CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, StateConservative Flux[3]) { 210 for (CeedInt i = 0; i < 3; i++) { 211 Flux[i].density = s.U.momentum[i]; 212 for (CeedInt j = 0; j < 3; j++) Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j] + s.Y.pressure * (i == j); 213 Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i]; 214 } 215 } 216 217 CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, State s, State ds, StateConservative dFlux[3]) { 218 for (CeedInt i = 0; i < 3; i++) { 219 dFlux[i].density = ds.U.momentum[i]; 220 for (CeedInt j = 0; j < 3; j++) { 221 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); 222 } 223 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]; 224 } 225 } 226 227 CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal(NewtonianIdealGasContext gas, State s, const CeedScalar normal[3]) { 228 StateConservative Flux[3], Flux_dot_n = {0}; 229 FluxInviscid(gas, s, Flux); 230 for (CeedInt i = 0; i < 3; i++) { 231 Flux_dot_n.density += Flux[i].density * normal[i]; 232 for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += Flux[i].momentum[j] * normal[i]; 233 Flux_dot_n.E_total += Flux[i].E_total * normal[i]; 234 } 235 return Flux_dot_n; 236 } 237 238 CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal_fwd(NewtonianIdealGasContext gas, State s, State ds, const CeedScalar normal[3]) { 239 StateConservative dFlux[3], Flux_dot_n = {0}; 240 FluxInviscid_fwd(gas, s, ds, dFlux); 241 for (CeedInt i = 0; i < 3; i++) { 242 Flux_dot_n.density += dFlux[i].density * normal[i]; 243 for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += dFlux[i].momentum[j] * normal[i]; 244 Flux_dot_n.E_total += dFlux[i].E_total * normal[i]; 245 } 246 return Flux_dot_n; 247 } 248 249 CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, State s, State ds[3], CeedScalar strong_conv[5]) { 250 for (CeedInt i = 0; i < 5; i++) strong_conv[i] = 0; 251 for (CeedInt i = 0; i < 3; i++) { 252 StateConservative dF[3]; 253 FluxInviscid_fwd(gas, s, ds[i], dF); 254 CeedScalar dF_i[5]; 255 UnpackState_U(dF[i], dF_i); 256 for (CeedInt j = 0; j < 5; j++) strong_conv[j] += dF_i[j]; 257 } 258 } 259 260 CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3], CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) { 261 for (CeedInt j = 0; j < 3; j++) { 262 Flux[0][j] = F_inviscid[j].density; 263 for (CeedInt k = 0; k < 3; k++) Flux[k + 1][j] = F_inviscid[j].momentum[k] - stress[k][j]; 264 Flux[4][j] = F_inviscid[j].E_total + Fe[j]; 265 } 266 } 267 268 CEED_QFUNCTION_HELPER void FluxTotal_Boundary(const StateConservative F_inviscid[3], const CeedScalar stress[3][3], const CeedScalar Fe[3], 269 const CeedScalar normal[3], CeedScalar Flux[5]) { 270 for (CeedInt j = 0; j < 5; j++) Flux[j] = 0.; 271 for (CeedInt j = 0; j < 3; j++) { 272 Flux[0] += F_inviscid[j].density * normal[j]; 273 for (CeedInt k = 0; k < 3; k++) { 274 Flux[k + 1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j]; 275 } 276 Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j]; 277 } 278 } 279 280 // Kelvin-Mandel notation 281 CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3], CeedScalar strain_rate[6]) { 282 const CeedScalar weight = 1 / sqrt(2.); 283 strain_rate[0] = grad_s[0].Y.velocity[0]; 284 strain_rate[1] = grad_s[1].Y.velocity[1]; 285 strain_rate[2] = grad_s[2].Y.velocity[2]; 286 strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]); 287 strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]); 288 strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]); 289 } 290 291 CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, const CeedScalar strain_rate[6], CeedScalar stress[6]) { 292 CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2]; 293 for (CeedInt i = 0; i < 6; i++) { 294 stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3)); 295 } 296 } 297 298 CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3], 299 CeedScalar Fe[3]) { 300 for (CeedInt i = 0; i < 3; i++) { 301 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; 302 } 303 } 304 305 CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, StatePrimitive Y, StatePrimitive dY, const State grad_ds[3], 306 const CeedScalar stress[3][3], const CeedScalar dstress[3][3], CeedScalar dFe[3]) { 307 for (CeedInt i = 0; i < 3; i++) { 308 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] - 309 Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] - gas->k * grad_ds[i].Y.temperature; 310 } 311 } 312 313 #endif // newtonian_state_h 314