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 37*d1b9ef12SLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_U(StateConservative s, CeedScalar U[5]) { 38*d1b9ef12SLeila Ghaffari U[0] = s.density; 39*d1b9ef12SLeila Ghaffari for (int i=0; i<3; i++) U[i+1] = s.momentum[i]; 40*d1b9ef12SLeila Ghaffari U[4] = s.E_total; 41*d1b9ef12SLeila Ghaffari } 42*d1b9ef12SLeila Ghaffari 43*d1b9ef12SLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) { 44*d1b9ef12SLeila Ghaffari Y[0] = s.pressure; 45*d1b9ef12SLeila Ghaffari for (int i=0; i<3; i++) Y[i+1] = s.velocity[i]; 46*d1b9ef12SLeila Ghaffari Y[4] = s.temperature; 47*d1b9ef12SLeila Ghaffari } 48*d1b9ef12SLeila Ghaffari 49475b2820SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative( 50475b2820SJames Wright NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) { 51475b2820SJames Wright StatePrimitive Y; 52475b2820SJames Wright for (CeedInt i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density; 53475b2820SJames Wright CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 54475b2820SJames Wright CeedScalar e_potential = -Dot3(gas->g, x); 55475b2820SJames Wright CeedScalar e_total = U.E_total / U.density; 56475b2820SJames Wright CeedScalar e_internal = e_total - e_kinetic - e_potential; 57475b2820SJames Wright Y.temperature = e_internal / gas->cv; 58475b2820SJames Wright Y.pressure = (gas->cp / gas->cv - 1) * U.density * e_internal; 59475b2820SJames Wright return Y; 60475b2820SJames Wright } 61475b2820SJames Wright 62475b2820SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd( 63475b2820SJames Wright NewtonianIdealGasContext gas, State s, StateConservative dU, 64475b2820SJames Wright const CeedScalar x[3], const CeedScalar dx[3]) { 65475b2820SJames Wright StatePrimitive dY; 66475b2820SJames Wright for (CeedInt i=0; i<3; i++) { 67475b2820SJames Wright dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density; 68475b2820SJames Wright } 69475b2820SJames Wright CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 70475b2820SJames Wright CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 71475b2820SJames Wright CeedScalar e_potential = -Dot3(gas->g, x); 72475b2820SJames Wright CeedScalar de_potential = -Dot3(gas->g, dx); 73475b2820SJames Wright CeedScalar e_total = s.U.E_total / s.U.density; 74475b2820SJames Wright CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density; 75475b2820SJames Wright CeedScalar e_internal = e_total - e_kinetic - e_potential; 76475b2820SJames Wright CeedScalar de_internal = de_total - de_kinetic - de_potential; 77475b2820SJames Wright dY.temperature = de_internal / gas->cv; 78475b2820SJames Wright dY.pressure = (gas->cp / gas->cv - 1) 79475b2820SJames Wright * (dU.density * e_internal + s.U.density * de_internal); 80475b2820SJames Wright return dY; 81475b2820SJames Wright } 82475b2820SJames Wright 83cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive( 84cbe60e31SLeila Ghaffari NewtonianIdealGasContext gas, StatePrimitive Y, const CeedScalar x[3]) { 85cbe60e31SLeila Ghaffari StateConservative U; 86cbe60e31SLeila Ghaffari CeedScalar R = gas->cp - gas->cv; 87cbe60e31SLeila Ghaffari U.density = Y.pressure / (R * Y.temperature); 88cbe60e31SLeila Ghaffari for (int i=0; i<3; i++) U.momentum[i] = U.density*Y.velocity[i]; 89cbe60e31SLeila Ghaffari CeedScalar e_internal = gas->cv * Y.temperature; 90cbe60e31SLeila Ghaffari CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 91cbe60e31SLeila Ghaffari CeedScalar e_potential = -Dot3(gas->g, x); 92cbe60e31SLeila Ghaffari CeedScalar e_total = e_internal + e_kinetic + e_potential; 93cbe60e31SLeila Ghaffari U.E_total = U.density*e_total; 94cbe60e31SLeila Ghaffari return U; 95cbe60e31SLeila Ghaffari } 96cbe60e31SLeila Ghaffari 97cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd( 98cbe60e31SLeila Ghaffari NewtonianIdealGasContext gas, State s, StatePrimitive dY, 99cbe60e31SLeila Ghaffari const CeedScalar x[3], const CeedScalar dx[3]) { 100cbe60e31SLeila Ghaffari StateConservative dU; 101cbe60e31SLeila Ghaffari CeedScalar R = gas->cp - gas->cv; 102cbe60e31SLeila Ghaffari dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / 103cbe60e31SLeila Ghaffari (R * s.Y.temperature * s.Y.temperature); 104cbe60e31SLeila Ghaffari for (int i=0; i<3; i++) { 105cbe60e31SLeila Ghaffari dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i]; 106cbe60e31SLeila Ghaffari } 107cbe60e31SLeila Ghaffari CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 108cbe60e31SLeila Ghaffari CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 109cbe60e31SLeila Ghaffari CeedScalar e_potential = -Dot3(gas->g, x); 110cbe60e31SLeila Ghaffari CeedScalar de_potential = -Dot3(gas->g, dx); 111cbe60e31SLeila Ghaffari CeedScalar e_internal = gas->cv * s.Y.temperature; 112cbe60e31SLeila Ghaffari CeedScalar de_internal = gas->cv * dY.temperature; 113cbe60e31SLeila Ghaffari CeedScalar e_total = e_internal + e_kinetic + e_potential; 114cbe60e31SLeila Ghaffari CeedScalar de_total = de_internal + de_kinetic + de_potential; 115cbe60e31SLeila Ghaffari dU.E_total = dU.density*e_total + s.U.density*de_total; 116cbe60e31SLeila Ghaffari return dU; 117cbe60e31SLeila Ghaffari } 118cbe60e31SLeila Ghaffari 119475b2820SJames Wright CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, 120475b2820SJames Wright const CeedScalar U[5], const CeedScalar x[3]) { 121475b2820SJames Wright State s; 122475b2820SJames Wright s.U.density = U[0]; 123475b2820SJames Wright s.U.momentum[0] = U[1]; 124475b2820SJames Wright s.U.momentum[1] = U[2]; 125475b2820SJames Wright s.U.momentum[2] = U[3]; 126475b2820SJames Wright s.U.E_total = U[4]; 127475b2820SJames Wright s.Y = StatePrimitiveFromConservative(gas, s.U, x); 128475b2820SJames Wright return s; 129475b2820SJames Wright } 130475b2820SJames Wright 131475b2820SJames Wright CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, 132475b2820SJames Wright State s, const CeedScalar dU[5], 133475b2820SJames Wright const CeedScalar x[3], const CeedScalar dx[3]) { 134475b2820SJames Wright State ds; 135475b2820SJames Wright ds.U.density = dU[0]; 136475b2820SJames Wright ds.U.momentum[0] = dU[1]; 137475b2820SJames Wright ds.U.momentum[1] = dU[2]; 138475b2820SJames Wright ds.U.momentum[2] = dU[3]; 139475b2820SJames Wright ds.U.E_total = dU[4]; 140475b2820SJames Wright ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx); 141475b2820SJames Wright return ds; 142475b2820SJames Wright } 143475b2820SJames Wright 144cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, 145cbe60e31SLeila Ghaffari const CeedScalar Y[5], const CeedScalar x[3]) { 146cbe60e31SLeila Ghaffari State s; 147cbe60e31SLeila Ghaffari s.Y.pressure = Y[0]; 148cbe60e31SLeila Ghaffari s.Y.velocity[0] = Y[1]; 149cbe60e31SLeila Ghaffari s.Y.velocity[1] = Y[2]; 150cbe60e31SLeila Ghaffari s.Y.velocity[2] = Y[3]; 151cbe60e31SLeila Ghaffari s.Y.temperature = Y[4]; 152cbe60e31SLeila Ghaffari s.U = StateConservativeFromPrimitive(gas, s.Y, x); 153cbe60e31SLeila Ghaffari return s; 154cbe60e31SLeila Ghaffari } 155cbe60e31SLeila Ghaffari 156cbe60e31SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, 157cbe60e31SLeila Ghaffari State s, const CeedScalar dY[5], 158cbe60e31SLeila Ghaffari const CeedScalar x[3], const CeedScalar dx[3]) { 159cbe60e31SLeila Ghaffari State ds; 160cbe60e31SLeila Ghaffari ds.Y.pressure = dY[0]; 161cbe60e31SLeila Ghaffari ds.Y.velocity[0] = dY[1]; 162cbe60e31SLeila Ghaffari ds.Y.velocity[1] = dY[2]; 163cbe60e31SLeila Ghaffari ds.Y.velocity[2] = dY[3]; 164cbe60e31SLeila Ghaffari ds.Y.temperature = dY[4]; 165cbe60e31SLeila Ghaffari ds.U = StateConservativeFromPrimitive_fwd(gas, s, ds.Y, x, dx); 166cbe60e31SLeila Ghaffari return ds; 167cbe60e31SLeila Ghaffari } 168cbe60e31SLeila Ghaffari 169475b2820SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, 170475b2820SJames Wright StateConservative Flux[3]) { 171475b2820SJames Wright for (CeedInt i=0; i<3; i++) { 172475b2820SJames Wright Flux[i].density = s.U.momentum[i]; 173475b2820SJames Wright for (CeedInt j=0; j<3; j++) 174475b2820SJames Wright Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j] 175475b2820SJames Wright + s.Y.pressure * (i == j); 176475b2820SJames Wright Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i]; 177475b2820SJames Wright } 178475b2820SJames Wright } 179475b2820SJames Wright 180475b2820SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, 181475b2820SJames Wright State s, State ds, StateConservative dFlux[3]) { 182475b2820SJames Wright for (CeedInt i=0; i<3; i++) { 183475b2820SJames Wright dFlux[i].density = ds.U.momentum[i]; 184475b2820SJames Wright for (CeedInt j=0; j<3; j++) 185475b2820SJames Wright dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] + 186475b2820SJames Wright s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j); 187475b2820SJames Wright dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] + 188475b2820SJames Wright (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i]; 189475b2820SJames Wright } 190475b2820SJames Wright } 191475b2820SJames Wright 192*d1b9ef12SLeila Ghaffari CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, 193*d1b9ef12SLeila Ghaffari State s, State ds[3], CeedScalar strong_conv[5]) { 194*d1b9ef12SLeila Ghaffari for (CeedInt i=0; i<5; i++) strong_conv[i] = 0; 195*d1b9ef12SLeila Ghaffari for (CeedInt i=0; i<3; i++) { 196*d1b9ef12SLeila Ghaffari StateConservative dF[3]; 197*d1b9ef12SLeila Ghaffari FluxInviscid_fwd(gas, s, ds[i], dF); 198*d1b9ef12SLeila Ghaffari CeedScalar dF_i[5]; 199*d1b9ef12SLeila Ghaffari UnpackState_U(dF[i], dF_i); 200*d1b9ef12SLeila Ghaffari for (CeedInt j=0; j<5; j++) 201*d1b9ef12SLeila Ghaffari strong_conv[j] += dF_i[j]; 202*d1b9ef12SLeila Ghaffari } 203*d1b9ef12SLeila Ghaffari } 204*d1b9ef12SLeila Ghaffari 205*d1b9ef12SLeila Ghaffari CEED_QFUNCTION_HELPER void FluxTotal(StateConservative F_inviscid[3], 206*d1b9ef12SLeila Ghaffari CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) { 207*d1b9ef12SLeila Ghaffari for (CeedInt j=0; j<3; j++) { 208*d1b9ef12SLeila Ghaffari Flux[0][j] = F_inviscid[j].density; 209*d1b9ef12SLeila Ghaffari for (CeedInt k=0; k<3; k++) 210*d1b9ef12SLeila Ghaffari Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j]; 211*d1b9ef12SLeila Ghaffari Flux[4][j] = F_inviscid[j].E_total + Fe[j]; 212*d1b9ef12SLeila Ghaffari } 213*d1b9ef12SLeila Ghaffari } 214*d1b9ef12SLeila Ghaffari 215475b2820SJames Wright // Kelvin-Mandel notation 216475b2820SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3], 217475b2820SJames Wright CeedScalar strain_rate[6]) { 218475b2820SJames Wright const CeedScalar weight = 1 / sqrt(2.); 219475b2820SJames Wright strain_rate[0] = grad_s[0].Y.velocity[0]; 220475b2820SJames Wright strain_rate[1] = grad_s[1].Y.velocity[1]; 221475b2820SJames Wright strain_rate[2] = grad_s[2].Y.velocity[2]; 222475b2820SJames Wright strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]); 223475b2820SJames Wright strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]); 224475b2820SJames Wright strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]); 225475b2820SJames Wright } 226475b2820SJames Wright 227475b2820SJames Wright CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, 228475b2820SJames Wright const CeedScalar strain_rate[6], CeedScalar stress[6]) { 229475b2820SJames Wright CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2]; 230475b2820SJames Wright for (CeedInt i=0; i<6; i++) { 231475b2820SJames Wright stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3)); 232475b2820SJames Wright } 233475b2820SJames Wright } 234475b2820SJames Wright 235475b2820SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, 236475b2820SJames Wright StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3], 237475b2820SJames Wright CeedScalar Fe[3]) { 238475b2820SJames Wright for (CeedInt i=0; i<3; i++) { 239475b2820SJames Wright Fe[i] = - Y.velocity[0] * stress[0][i] 240475b2820SJames Wright - Y.velocity[1] * stress[1][i] 241475b2820SJames Wright - Y.velocity[2] * stress[2][i] 242475b2820SJames Wright - gas->k * grad_s[i].Y.temperature; 243475b2820SJames Wright } 244475b2820SJames Wright } 245475b2820SJames Wright 246475b2820SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, 247475b2820SJames Wright StatePrimitive Y, StatePrimitive dY, const State grad_ds[3], 248475b2820SJames Wright const CeedScalar stress[3][3], 249475b2820SJames Wright const CeedScalar dstress[3][3], 250475b2820SJames Wright CeedScalar dFe[3]) { 251475b2820SJames Wright for (CeedInt i=0; i<3; i++) { 252475b2820SJames Wright dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i] 253475b2820SJames Wright - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i] 254475b2820SJames Wright - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] 255475b2820SJames Wright - gas->k * grad_ds[i].Y.temperature; 256475b2820SJames Wright } 257475b2820SJames Wright } 258475b2820SJames Wright 259475b2820SJames Wright #endif // newtonian_state_h 260