1c6e8c570SJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2c6e8c570SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3c6e8c570SJames Wright // 4c6e8c570SJames Wright // SPDX-License-Identifier: BSD-2-Clause 5c6e8c570SJames Wright // 6c6e8c570SJames Wright // This file is part of CEED: http://github.com/ceed 7c6e8c570SJames Wright 8c6e8c570SJames Wright /// @file 9c6e8c570SJames Wright /// Structs and helper functions regarding the state of a newtonian simulation 10c6e8c570SJames Wright 11c6e8c570SJames Wright 12c6e8c570SJames Wright #ifndef newtonian_state_h 13c6e8c570SJames Wright #define newtonian_state_h 14c6e8c570SJames Wright 15c6e8c570SJames Wright #include <ceed.h> 16c9c2c079SJeremy L Thompson #include <math.h> 17c6e8c570SJames Wright #include "newtonian_types.h" 1813fa47b2SJames Wright #include "utils.h" 19c6e8c570SJames Wright 20c6e8c570SJames Wright typedef struct { 21c6e8c570SJames Wright CeedScalar pressure; 22c6e8c570SJames Wright CeedScalar velocity[3]; 23c6e8c570SJames Wright CeedScalar temperature; 24c6e8c570SJames Wright } StatePrimitive; 25c6e8c570SJames Wright 26c6e8c570SJames Wright typedef struct { 27c6e8c570SJames Wright CeedScalar density; 28c6e8c570SJames Wright CeedScalar momentum[3]; 29c6e8c570SJames Wright CeedScalar E_total; 30c6e8c570SJames Wright } StateConservative; 31c6e8c570SJames Wright 32c6e8c570SJames Wright typedef struct { 33c6e8c570SJames Wright StateConservative U; 34c6e8c570SJames Wright StatePrimitive Y; 35c6e8c570SJames Wright } State; 36c6e8c570SJames Wright 372b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_U(StateConservative s, CeedScalar U[5]) { 382b89d87eSLeila Ghaffari U[0] = s.density; 392b89d87eSLeila Ghaffari for (int i=0; i<3; i++) U[i+1] = s.momentum[i]; 402b89d87eSLeila Ghaffari U[4] = s.E_total; 412b89d87eSLeila Ghaffari } 422b89d87eSLeila Ghaffari 432b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) { 442b89d87eSLeila Ghaffari Y[0] = s.pressure; 452b89d87eSLeila Ghaffari for (int i=0; i<3; i++) Y[i+1] = s.velocity[i]; 462b89d87eSLeila Ghaffari Y[4] = s.temperature; 472b89d87eSLeila Ghaffari } 482b89d87eSLeila Ghaffari 49*2518f336SLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio( 50*2518f336SLeila Ghaffari NewtonianIdealGasContext gas) { 51*2518f336SLeila Ghaffari return gas->cp / gas->cv; 52*2518f336SLeila Ghaffari } 53*2518f336SLeila Ghaffari 54*2518f336SLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar GasConstant( 55*2518f336SLeila Ghaffari NewtonianIdealGasContext gas) { 56*2518f336SLeila Ghaffari return gas->cp - gas->cv; 57*2518f336SLeila Ghaffari } 58*2518f336SLeila Ghaffari 59*2518f336SLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) { 60*2518f336SLeila Ghaffari return gas->cp * gas->mu / gas->k; 61*2518f336SLeila Ghaffari } 62*2518f336SLeila Ghaffari 63*2518f336SLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas, 64*2518f336SLeila Ghaffari CeedScalar T) { 65*2518f336SLeila Ghaffari return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T); 66*2518f336SLeila Ghaffari } 67*2518f336SLeila Ghaffari 68*2518f336SLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas, 69*2518f336SLeila Ghaffari CeedScalar T, CeedScalar u) { 70*2518f336SLeila Ghaffari return u / SoundSpeed(gas, T); 71*2518f336SLeila Ghaffari } 72*2518f336SLeila Ghaffari 73c6e8c570SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative( 74c6e8c570SJames Wright NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) { 75c6e8c570SJames Wright StatePrimitive Y; 76c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density; 77c6e8c570SJames Wright CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 78c6e8c570SJames Wright CeedScalar e_potential = -Dot3(gas->g, x); 79c6e8c570SJames Wright CeedScalar e_total = U.E_total / U.density; 80c6e8c570SJames Wright CeedScalar e_internal = e_total - e_kinetic - e_potential; 81c6e8c570SJames Wright Y.temperature = e_internal / gas->cv; 82*2518f336SLeila Ghaffari Y.pressure = (HeatCapacityRatio(gas) - 1) * U.density * e_internal; 83c6e8c570SJames Wright return Y; 84c6e8c570SJames Wright } 85c6e8c570SJames Wright 86c6e8c570SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd( 87c6e8c570SJames Wright NewtonianIdealGasContext gas, State s, StateConservative dU, 88c6e8c570SJames Wright const CeedScalar x[3], const CeedScalar dx[3]) { 89c6e8c570SJames Wright StatePrimitive dY; 90c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 91c6e8c570SJames Wright dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density; 92c6e8c570SJames Wright } 93c6e8c570SJames Wright CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 94c6e8c570SJames Wright CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 95c6e8c570SJames Wright CeedScalar e_potential = -Dot3(gas->g, x); 96c6e8c570SJames Wright CeedScalar de_potential = -Dot3(gas->g, dx); 97c6e8c570SJames Wright CeedScalar e_total = s.U.E_total / s.U.density; 98c6e8c570SJames Wright CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density; 99c6e8c570SJames Wright CeedScalar e_internal = e_total - e_kinetic - e_potential; 100c6e8c570SJames Wright CeedScalar de_internal = de_total - de_kinetic - de_potential; 101c6e8c570SJames Wright dY.temperature = de_internal / gas->cv; 102*2518f336SLeila Ghaffari dY.pressure = (HeatCapacityRatio(gas) - 1) 103c6e8c570SJames Wright * (dU.density * e_internal + s.U.density * de_internal); 104c6e8c570SJames Wright return dY; 105c6e8c570SJames Wright } 106c6e8c570SJames Wright 107dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive( 108dc805cc4SLeila Ghaffari NewtonianIdealGasContext gas, StatePrimitive Y, const CeedScalar x[3]) { 109dc805cc4SLeila Ghaffari StateConservative U; 110*2518f336SLeila Ghaffari U.density = Y.pressure / (GasConstant(gas) * Y.temperature); 111dc805cc4SLeila Ghaffari for (int i=0; i<3; i++) U.momentum[i] = U.density*Y.velocity[i]; 112dc805cc4SLeila Ghaffari CeedScalar e_internal = gas->cv * Y.temperature; 113dc805cc4SLeila Ghaffari CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 114dc805cc4SLeila Ghaffari CeedScalar e_potential = -Dot3(gas->g, x); 115dc805cc4SLeila Ghaffari CeedScalar e_total = e_internal + e_kinetic + e_potential; 116dc805cc4SLeila Ghaffari U.E_total = U.density*e_total; 117dc805cc4SLeila Ghaffari return U; 118dc805cc4SLeila Ghaffari } 119dc805cc4SLeila Ghaffari 120dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd( 121dc805cc4SLeila Ghaffari NewtonianIdealGasContext gas, State s, StatePrimitive dY, 122dc805cc4SLeila Ghaffari const CeedScalar x[3], const CeedScalar dx[3]) { 123dc805cc4SLeila Ghaffari StateConservative dU; 124dc805cc4SLeila Ghaffari dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / 125*2518f336SLeila Ghaffari (GasConstant(gas) * s.Y.temperature * s.Y.temperature); 126dc805cc4SLeila Ghaffari for (int i=0; i<3; i++) { 127dc805cc4SLeila Ghaffari dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i]; 128dc805cc4SLeila Ghaffari } 129dc805cc4SLeila Ghaffari CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 130dc805cc4SLeila Ghaffari CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 131dc805cc4SLeila Ghaffari CeedScalar e_potential = -Dot3(gas->g, x); 132dc805cc4SLeila Ghaffari CeedScalar de_potential = -Dot3(gas->g, dx); 133dc805cc4SLeila Ghaffari CeedScalar e_internal = gas->cv * s.Y.temperature; 134dc805cc4SLeila Ghaffari CeedScalar de_internal = gas->cv * dY.temperature; 135dc805cc4SLeila Ghaffari CeedScalar e_total = e_internal + e_kinetic + e_potential; 136dc805cc4SLeila Ghaffari CeedScalar de_total = de_internal + de_kinetic + de_potential; 137dc805cc4SLeila Ghaffari dU.E_total = dU.density*e_total + s.U.density*de_total; 138dc805cc4SLeila Ghaffari return dU; 139dc805cc4SLeila Ghaffari } 140dc805cc4SLeila Ghaffari 14120840d50SJames Wright // Function pointer types for generic state array -> State struct functions 14220840d50SJames Wright typedef State (*StateFromQi_t)(NewtonianIdealGasContext gas, 14320840d50SJames Wright const CeedScalar qi[5], const CeedScalar x[3]); 14420840d50SJames Wright typedef State (*StateFromQi_fwd_t)(NewtonianIdealGasContext gas, 14520840d50SJames Wright State s, const CeedScalar dqi[5], 14620840d50SJames Wright const CeedScalar x[3], const CeedScalar dx[3]); 14720840d50SJames Wright 148c6e8c570SJames Wright CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, 149c6e8c570SJames Wright const CeedScalar U[5], const CeedScalar x[3]) { 150c6e8c570SJames Wright State s; 151c6e8c570SJames Wright s.U.density = U[0]; 152c6e8c570SJames Wright s.U.momentum[0] = U[1]; 153c6e8c570SJames Wright s.U.momentum[1] = U[2]; 154c6e8c570SJames Wright s.U.momentum[2] = U[3]; 155c6e8c570SJames Wright s.U.E_total = U[4]; 156c6e8c570SJames Wright s.Y = StatePrimitiveFromConservative(gas, s.U, x); 157c6e8c570SJames Wright return s; 158c6e8c570SJames Wright } 159c6e8c570SJames Wright 160c6e8c570SJames Wright CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, 161c6e8c570SJames Wright State s, const CeedScalar dU[5], 162c6e8c570SJames Wright const CeedScalar x[3], const CeedScalar dx[3]) { 163c6e8c570SJames Wright State ds; 164c6e8c570SJames Wright ds.U.density = dU[0]; 165c6e8c570SJames Wright ds.U.momentum[0] = dU[1]; 166c6e8c570SJames Wright ds.U.momentum[1] = dU[2]; 167c6e8c570SJames Wright ds.U.momentum[2] = dU[3]; 168c6e8c570SJames Wright ds.U.E_total = dU[4]; 169c6e8c570SJames Wright ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx); 170c6e8c570SJames Wright return ds; 171c6e8c570SJames Wright } 172c6e8c570SJames Wright 173dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, 174dc805cc4SLeila Ghaffari const CeedScalar Y[5], const CeedScalar x[3]) { 175dc805cc4SLeila Ghaffari State s; 176dc805cc4SLeila Ghaffari s.Y.pressure = Y[0]; 177dc805cc4SLeila Ghaffari s.Y.velocity[0] = Y[1]; 178dc805cc4SLeila Ghaffari s.Y.velocity[1] = Y[2]; 179dc805cc4SLeila Ghaffari s.Y.velocity[2] = Y[3]; 180dc805cc4SLeila Ghaffari s.Y.temperature = Y[4]; 181dc805cc4SLeila Ghaffari s.U = StateConservativeFromPrimitive(gas, s.Y, x); 182dc805cc4SLeila Ghaffari return s; 183dc805cc4SLeila Ghaffari } 184dc805cc4SLeila Ghaffari 185dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, 186dc805cc4SLeila Ghaffari State s, const CeedScalar dY[5], 187dc805cc4SLeila Ghaffari const CeedScalar x[3], const CeedScalar dx[3]) { 188dc805cc4SLeila Ghaffari State ds; 189dc805cc4SLeila Ghaffari ds.Y.pressure = dY[0]; 190dc805cc4SLeila Ghaffari ds.Y.velocity[0] = dY[1]; 191dc805cc4SLeila Ghaffari ds.Y.velocity[1] = dY[2]; 192dc805cc4SLeila Ghaffari ds.Y.velocity[2] = dY[3]; 193dc805cc4SLeila Ghaffari ds.Y.temperature = dY[4]; 194dc805cc4SLeila Ghaffari ds.U = StateConservativeFromPrimitive_fwd(gas, s, ds.Y, x, dx); 195dc805cc4SLeila Ghaffari return ds; 196dc805cc4SLeila Ghaffari } 197dc805cc4SLeila Ghaffari 198c6e8c570SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, 199c6e8c570SJames Wright StateConservative Flux[3]) { 200c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 201c6e8c570SJames Wright Flux[i].density = s.U.momentum[i]; 202c6e8c570SJames Wright for (CeedInt j=0; j<3; j++) 203c6e8c570SJames Wright Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j] 204c6e8c570SJames Wright + s.Y.pressure * (i == j); 205c6e8c570SJames Wright Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i]; 206c6e8c570SJames Wright } 207c6e8c570SJames Wright } 208c6e8c570SJames Wright 209c6e8c570SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, 210c6e8c570SJames Wright State s, State ds, StateConservative dFlux[3]) { 211c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 212c6e8c570SJames Wright dFlux[i].density = ds.U.momentum[i]; 213c6e8c570SJames Wright for (CeedInt j=0; j<3; j++) 214c6e8c570SJames Wright dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] + 215c6e8c570SJames Wright s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j); 216c6e8c570SJames Wright dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] + 217c6e8c570SJames Wright (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i]; 218c6e8c570SJames Wright } 219c6e8c570SJames Wright } 220c6e8c570SJames Wright 2212b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, 2222b89d87eSLeila Ghaffari State s, State ds[3], CeedScalar strong_conv[5]) { 2232b89d87eSLeila Ghaffari for (CeedInt i=0; i<5; i++) strong_conv[i] = 0; 2242b89d87eSLeila Ghaffari for (CeedInt i=0; i<3; i++) { 2252b89d87eSLeila Ghaffari StateConservative dF[3]; 2262b89d87eSLeila Ghaffari FluxInviscid_fwd(gas, s, ds[i], dF); 2272b89d87eSLeila Ghaffari CeedScalar dF_i[5]; 2282b89d87eSLeila Ghaffari UnpackState_U(dF[i], dF_i); 2292b89d87eSLeila Ghaffari for (CeedInt j=0; j<5; j++) 2302b89d87eSLeila Ghaffari strong_conv[j] += dF_i[j]; 2312b89d87eSLeila Ghaffari } 2322b89d87eSLeila Ghaffari } 2332b89d87eSLeila Ghaffari 23420840d50SJames Wright CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3], 2352b89d87eSLeila Ghaffari CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) { 2362b89d87eSLeila Ghaffari for (CeedInt j=0; j<3; j++) { 2372b89d87eSLeila Ghaffari Flux[0][j] = F_inviscid[j].density; 2382b89d87eSLeila Ghaffari for (CeedInt k=0; k<3; k++) 2392b89d87eSLeila Ghaffari Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j]; 2402b89d87eSLeila Ghaffari Flux[4][j] = F_inviscid[j].E_total + Fe[j]; 2412b89d87eSLeila Ghaffari } 2422b89d87eSLeila Ghaffari } 2432b89d87eSLeila Ghaffari 2445bce47c7SJames Wright CEED_QFUNCTION_HELPER void FluxTotal_Boundary( 2455bce47c7SJames Wright const StateConservative F_inviscid[3], const CeedScalar stress[3][3], 2465bce47c7SJames Wright const CeedScalar Fe[3], const CeedScalar normal[3], CeedScalar Flux[5]) { 2475bce47c7SJames Wright 2485bce47c7SJames Wright for(CeedInt j=0; j<5; j++) Flux[j] = 0.; 2495bce47c7SJames Wright for (CeedInt j=0; j<3; j++) { 2505bce47c7SJames Wright Flux[0] += F_inviscid[j].density * normal[j]; 2515bce47c7SJames Wright for (CeedInt k=0; k<3; k++) { 2525bce47c7SJames Wright Flux[k+1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j]; 2535bce47c7SJames Wright } 2545bce47c7SJames Wright Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j]; 2555bce47c7SJames Wright } 2565bce47c7SJames Wright } 2575bce47c7SJames Wright 258c6e8c570SJames Wright // Kelvin-Mandel notation 259c6e8c570SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3], 260c6e8c570SJames Wright CeedScalar strain_rate[6]) { 261c6e8c570SJames Wright const CeedScalar weight = 1 / sqrt(2.); 262c6e8c570SJames Wright strain_rate[0] = grad_s[0].Y.velocity[0]; 263c6e8c570SJames Wright strain_rate[1] = grad_s[1].Y.velocity[1]; 264c6e8c570SJames Wright strain_rate[2] = grad_s[2].Y.velocity[2]; 265c6e8c570SJames Wright strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]); 266c6e8c570SJames Wright strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]); 267c6e8c570SJames Wright strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]); 268c6e8c570SJames Wright } 269c6e8c570SJames Wright 270c6e8c570SJames Wright CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, 271c6e8c570SJames Wright const CeedScalar strain_rate[6], CeedScalar stress[6]) { 272c6e8c570SJames Wright CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2]; 273c6e8c570SJames Wright for (CeedInt i=0; i<6; i++) { 274c6e8c570SJames Wright stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3)); 275c6e8c570SJames Wright } 276c6e8c570SJames Wright } 277c6e8c570SJames Wright 278c6e8c570SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, 279c6e8c570SJames Wright StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3], 280c6e8c570SJames Wright CeedScalar Fe[3]) { 281c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 282c6e8c570SJames Wright Fe[i] = - Y.velocity[0] * stress[0][i] 283c6e8c570SJames Wright - Y.velocity[1] * stress[1][i] 284c6e8c570SJames Wright - Y.velocity[2] * stress[2][i] 285c6e8c570SJames Wright - gas->k * grad_s[i].Y.temperature; 286c6e8c570SJames Wright } 287c6e8c570SJames Wright } 288c6e8c570SJames Wright 289c6e8c570SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, 290c6e8c570SJames Wright StatePrimitive Y, StatePrimitive dY, const State grad_ds[3], 291c6e8c570SJames Wright const CeedScalar stress[3][3], 292c6e8c570SJames Wright const CeedScalar dstress[3][3], 293c6e8c570SJames Wright CeedScalar dFe[3]) { 294c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 295c6e8c570SJames Wright dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i] 296c6e8c570SJames Wright - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i] 297c6e8c570SJames Wright - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] 298c6e8c570SJames Wright - gas->k * grad_ds[i].Y.temperature; 299c6e8c570SJames Wright } 300c6e8c570SJames Wright } 301c6e8c570SJames Wright 302c6e8c570SJames Wright #endif // newtonian_state_h 303