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