1475b2820SJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2475b2820SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3475b2820SJames Wright // 4475b2820SJames Wright // SPDX-License-Identifier: BSD-2-Clause 5475b2820SJames Wright // 6475b2820SJames Wright // This file is part of CEED: http://github.com/ceed 7475b2820SJames Wright 8475b2820SJames Wright /// @file 9475b2820SJames Wright /// Structs and helper functions regarding the state of a newtonian simulation 10475b2820SJames Wright 11475b2820SJames Wright 12475b2820SJames Wright #ifndef newtonian_state_h 13475b2820SJames Wright #define newtonian_state_h 14475b2820SJames Wright 15475b2820SJames Wright #include <math.h> 16475b2820SJames Wright #include <ceed.h> 17475b2820SJames Wright #include "newtonian_types.h" 18704b8bbeSJames Wright #include "utils.h" 19475b2820SJames Wright 20475b2820SJames Wright typedef struct { 21475b2820SJames Wright CeedScalar pressure; 22475b2820SJames Wright CeedScalar velocity[3]; 23475b2820SJames Wright CeedScalar temperature; 24475b2820SJames Wright } StatePrimitive; 25475b2820SJames Wright 26475b2820SJames Wright typedef struct { 27475b2820SJames Wright CeedScalar density; 28475b2820SJames Wright CeedScalar momentum[3]; 29475b2820SJames Wright CeedScalar E_total; 30475b2820SJames Wright } StateConservative; 31475b2820SJames Wright 32475b2820SJames Wright typedef struct { 33475b2820SJames Wright StateConservative U; 34475b2820SJames Wright StatePrimitive Y; 35475b2820SJames Wright } State; 36475b2820SJames Wright 37475b2820SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative( 38475b2820SJames Wright NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) { 39475b2820SJames Wright StatePrimitive Y; 40475b2820SJames Wright for (CeedInt i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density; 41475b2820SJames Wright CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 42475b2820SJames Wright CeedScalar e_potential = -Dot3(gas->g, x); 43475b2820SJames Wright CeedScalar e_total = U.E_total / U.density; 44475b2820SJames Wright CeedScalar e_internal = e_total - e_kinetic - e_potential; 45475b2820SJames Wright Y.temperature = e_internal / gas->cv; 46475b2820SJames Wright Y.pressure = (gas->cp / gas->cv - 1) * U.density * e_internal; 47475b2820SJames Wright return Y; 48475b2820SJames Wright } 49475b2820SJames Wright 50475b2820SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd( 51475b2820SJames Wright NewtonianIdealGasContext gas, State s, StateConservative dU, 52475b2820SJames Wright const CeedScalar x[3], const CeedScalar dx[3]) { 53475b2820SJames Wright StatePrimitive dY; 54475b2820SJames Wright for (CeedInt i=0; i<3; i++) { 55475b2820SJames Wright dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density; 56475b2820SJames Wright } 57475b2820SJames Wright CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 58475b2820SJames Wright CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 59475b2820SJames Wright CeedScalar e_potential = -Dot3(gas->g, x); 60475b2820SJames Wright CeedScalar de_potential = -Dot3(gas->g, dx); 61475b2820SJames Wright CeedScalar e_total = s.U.E_total / s.U.density; 62475b2820SJames Wright CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density; 63475b2820SJames Wright CeedScalar e_internal = e_total - e_kinetic - e_potential; 64475b2820SJames Wright CeedScalar de_internal = de_total - de_kinetic - de_potential; 65475b2820SJames Wright dY.temperature = de_internal / gas->cv; 66475b2820SJames Wright dY.pressure = (gas->cp / gas->cv - 1) 67475b2820SJames Wright * (dU.density * e_internal + s.U.density * de_internal); 68475b2820SJames Wright return dY; 69475b2820SJames Wright } 70475b2820SJames Wright 71*cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive( 72*cbe60e31SLeila Ghaffari NewtonianIdealGasContext gas, StatePrimitive Y, const CeedScalar x[3]) { 73*cbe60e31SLeila Ghaffari StateConservative U; 74*cbe60e31SLeila Ghaffari CeedScalar R = gas->cp - gas->cv; 75*cbe60e31SLeila Ghaffari U.density = Y.pressure / (R * Y.temperature); 76*cbe60e31SLeila Ghaffari for (int i=0; i<3; i++) U.momentum[i] = U.density*Y.velocity[i]; 77*cbe60e31SLeila Ghaffari CeedScalar e_internal = gas->cv * Y.temperature; 78*cbe60e31SLeila Ghaffari CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 79*cbe60e31SLeila Ghaffari CeedScalar e_potential = -Dot3(gas->g, x); 80*cbe60e31SLeila Ghaffari CeedScalar e_total = e_internal + e_kinetic + e_potential; 81*cbe60e31SLeila Ghaffari U.E_total = U.density*e_total; 82*cbe60e31SLeila Ghaffari return U; 83*cbe60e31SLeila Ghaffari } 84*cbe60e31SLeila Ghaffari 85*cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd( 86*cbe60e31SLeila Ghaffari NewtonianIdealGasContext gas, State s, StatePrimitive dY, 87*cbe60e31SLeila Ghaffari const CeedScalar x[3], const CeedScalar dx[3]) { 88*cbe60e31SLeila Ghaffari StateConservative dU; 89*cbe60e31SLeila Ghaffari CeedScalar R = gas->cp - gas->cv; 90*cbe60e31SLeila Ghaffari dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / 91*cbe60e31SLeila Ghaffari (R * s.Y.temperature * s.Y.temperature); 92*cbe60e31SLeila Ghaffari for (int i=0; i<3; i++) { 93*cbe60e31SLeila Ghaffari dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i]; 94*cbe60e31SLeila Ghaffari } 95*cbe60e31SLeila Ghaffari CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 96*cbe60e31SLeila Ghaffari CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 97*cbe60e31SLeila Ghaffari CeedScalar e_potential = -Dot3(gas->g, x); 98*cbe60e31SLeila Ghaffari CeedScalar de_potential = -Dot3(gas->g, dx); 99*cbe60e31SLeila Ghaffari CeedScalar e_internal = gas->cv * s.Y.temperature; 100*cbe60e31SLeila Ghaffari CeedScalar de_internal = gas->cv * dY.temperature; 101*cbe60e31SLeila Ghaffari CeedScalar e_total = e_internal + e_kinetic + e_potential; 102*cbe60e31SLeila Ghaffari CeedScalar de_total = de_internal + de_kinetic + de_potential; 103*cbe60e31SLeila Ghaffari dU.E_total = dU.density*e_total + s.U.density*de_total; 104*cbe60e31SLeila Ghaffari return dU; 105*cbe60e31SLeila Ghaffari } 106*cbe60e31SLeila Ghaffari 107475b2820SJames Wright CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, 108475b2820SJames Wright const CeedScalar U[5], const CeedScalar x[3]) { 109475b2820SJames Wright State s; 110475b2820SJames Wright s.U.density = U[0]; 111475b2820SJames Wright s.U.momentum[0] = U[1]; 112475b2820SJames Wright s.U.momentum[1] = U[2]; 113475b2820SJames Wright s.U.momentum[2] = U[3]; 114475b2820SJames Wright s.U.E_total = U[4]; 115475b2820SJames Wright s.Y = StatePrimitiveFromConservative(gas, s.U, x); 116475b2820SJames Wright return s; 117475b2820SJames Wright } 118475b2820SJames Wright 119475b2820SJames Wright CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, 120475b2820SJames Wright State s, const CeedScalar dU[5], 121475b2820SJames Wright const CeedScalar x[3], const CeedScalar dx[3]) { 122475b2820SJames Wright State ds; 123475b2820SJames Wright ds.U.density = dU[0]; 124475b2820SJames Wright ds.U.momentum[0] = dU[1]; 125475b2820SJames Wright ds.U.momentum[1] = dU[2]; 126475b2820SJames Wright ds.U.momentum[2] = dU[3]; 127475b2820SJames Wright ds.U.E_total = dU[4]; 128475b2820SJames Wright ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx); 129475b2820SJames Wright return ds; 130475b2820SJames Wright } 131475b2820SJames Wright 132*cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, 133*cbe60e31SLeila Ghaffari const CeedScalar Y[5], const CeedScalar x[3]) { 134*cbe60e31SLeila Ghaffari State s; 135*cbe60e31SLeila Ghaffari s.Y.pressure = Y[0]; 136*cbe60e31SLeila Ghaffari s.Y.velocity[0] = Y[1]; 137*cbe60e31SLeila Ghaffari s.Y.velocity[1] = Y[2]; 138*cbe60e31SLeila Ghaffari s.Y.velocity[2] = Y[3]; 139*cbe60e31SLeila Ghaffari s.Y.temperature = Y[4]; 140*cbe60e31SLeila Ghaffari s.U = StateConservativeFromPrimitive(gas, s.Y, x); 141*cbe60e31SLeila Ghaffari return s; 142*cbe60e31SLeila Ghaffari } 143*cbe60e31SLeila Ghaffari 144*cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, 145*cbe60e31SLeila Ghaffari State s, const CeedScalar dY[5], 146*cbe60e31SLeila Ghaffari const CeedScalar x[3], const CeedScalar dx[3]) { 147*cbe60e31SLeila Ghaffari State ds; 148*cbe60e31SLeila Ghaffari ds.Y.pressure = dY[0]; 149*cbe60e31SLeila Ghaffari ds.Y.velocity[0] = dY[1]; 150*cbe60e31SLeila Ghaffari ds.Y.velocity[1] = dY[2]; 151*cbe60e31SLeila Ghaffari ds.Y.velocity[2] = dY[3]; 152*cbe60e31SLeila Ghaffari ds.Y.temperature = dY[4]; 153*cbe60e31SLeila Ghaffari ds.U = StateConservativeFromPrimitive_fwd(gas, s, ds.Y, x, dx); 154*cbe60e31SLeila Ghaffari return ds; 155*cbe60e31SLeila Ghaffari } 156*cbe60e31SLeila Ghaffari 157475b2820SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, 158475b2820SJames Wright StateConservative Flux[3]) { 159475b2820SJames Wright for (CeedInt i=0; i<3; i++) { 160475b2820SJames Wright Flux[i].density = s.U.momentum[i]; 161475b2820SJames Wright for (CeedInt j=0; j<3; j++) 162475b2820SJames Wright Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j] 163475b2820SJames Wright + s.Y.pressure * (i == j); 164475b2820SJames Wright Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i]; 165475b2820SJames Wright } 166475b2820SJames Wright } 167475b2820SJames Wright 168475b2820SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, 169475b2820SJames Wright State s, State ds, StateConservative dFlux[3]) { 170475b2820SJames Wright for (CeedInt i=0; i<3; i++) { 171475b2820SJames Wright dFlux[i].density = ds.U.momentum[i]; 172475b2820SJames Wright for (CeedInt j=0; j<3; j++) 173475b2820SJames Wright dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] + 174475b2820SJames Wright s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j); 175475b2820SJames Wright dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] + 176475b2820SJames Wright (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i]; 177475b2820SJames Wright } 178475b2820SJames Wright } 179475b2820SJames Wright 180475b2820SJames Wright // Kelvin-Mandel notation 181475b2820SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3], 182475b2820SJames Wright CeedScalar strain_rate[6]) { 183475b2820SJames Wright const CeedScalar weight = 1 / sqrt(2.); 184475b2820SJames Wright strain_rate[0] = grad_s[0].Y.velocity[0]; 185475b2820SJames Wright strain_rate[1] = grad_s[1].Y.velocity[1]; 186475b2820SJames Wright strain_rate[2] = grad_s[2].Y.velocity[2]; 187475b2820SJames Wright strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]); 188475b2820SJames Wright strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]); 189475b2820SJames Wright strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]); 190475b2820SJames Wright } 191475b2820SJames Wright 192475b2820SJames Wright CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, 193475b2820SJames Wright const CeedScalar strain_rate[6], CeedScalar stress[6]) { 194475b2820SJames Wright CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2]; 195475b2820SJames Wright for (CeedInt i=0; i<6; i++) { 196475b2820SJames Wright stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3)); 197475b2820SJames Wright } 198475b2820SJames Wright } 199475b2820SJames Wright 200475b2820SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, 201475b2820SJames Wright StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3], 202475b2820SJames Wright CeedScalar Fe[3]) { 203475b2820SJames Wright for (CeedInt i=0; i<3; i++) { 204475b2820SJames Wright Fe[i] = - Y.velocity[0] * stress[0][i] 205475b2820SJames Wright - Y.velocity[1] * stress[1][i] 206475b2820SJames Wright - Y.velocity[2] * stress[2][i] 207475b2820SJames Wright - gas->k * grad_s[i].Y.temperature; 208475b2820SJames Wright } 209475b2820SJames Wright } 210475b2820SJames Wright 211475b2820SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, 212475b2820SJames Wright StatePrimitive Y, StatePrimitive dY, const State grad_ds[3], 213475b2820SJames Wright const CeedScalar stress[3][3], 214475b2820SJames Wright const CeedScalar dstress[3][3], 215475b2820SJames Wright CeedScalar dFe[3]) { 216475b2820SJames Wright for (CeedInt i=0; i<3; i++) { 217475b2820SJames Wright dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i] 218475b2820SJames Wright - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i] 219475b2820SJames Wright - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] 220475b2820SJames Wright - gas->k * grad_ds[i].Y.temperature; 221475b2820SJames Wright } 222475b2820SJames Wright } 223475b2820SJames Wright 224475b2820SJames Wright #endif // newtonian_state_h 225