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 49c6e8c570SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative( 50c6e8c570SJames Wright NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) { 51c6e8c570SJames Wright StatePrimitive Y; 52c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density; 53c6e8c570SJames Wright CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 54c6e8c570SJames Wright CeedScalar e_potential = -Dot3(gas->g, x); 55c6e8c570SJames Wright CeedScalar e_total = U.E_total / U.density; 56c6e8c570SJames Wright CeedScalar e_internal = e_total - e_kinetic - e_potential; 57c6e8c570SJames Wright Y.temperature = e_internal / gas->cv; 58c6e8c570SJames Wright Y.pressure = (gas->cp / gas->cv - 1) * U.density * e_internal; 59c6e8c570SJames Wright return Y; 60c6e8c570SJames Wright } 61c6e8c570SJames Wright 62c6e8c570SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd( 63c6e8c570SJames Wright NewtonianIdealGasContext gas, State s, StateConservative dU, 64c6e8c570SJames Wright const CeedScalar x[3], const CeedScalar dx[3]) { 65c6e8c570SJames Wright StatePrimitive dY; 66c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 67c6e8c570SJames Wright dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density; 68c6e8c570SJames Wright } 69c6e8c570SJames Wright CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 70c6e8c570SJames Wright CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 71c6e8c570SJames Wright CeedScalar e_potential = -Dot3(gas->g, x); 72c6e8c570SJames Wright CeedScalar de_potential = -Dot3(gas->g, dx); 73c6e8c570SJames Wright CeedScalar e_total = s.U.E_total / s.U.density; 74c6e8c570SJames Wright CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density; 75c6e8c570SJames Wright CeedScalar e_internal = e_total - e_kinetic - e_potential; 76c6e8c570SJames Wright CeedScalar de_internal = de_total - de_kinetic - de_potential; 77c6e8c570SJames Wright dY.temperature = de_internal / gas->cv; 78c6e8c570SJames Wright dY.pressure = (gas->cp / gas->cv - 1) 79c6e8c570SJames Wright * (dU.density * e_internal + s.U.density * de_internal); 80c6e8c570SJames Wright return dY; 81c6e8c570SJames Wright } 82c6e8c570SJames Wright 83dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive( 84dc805cc4SLeila Ghaffari NewtonianIdealGasContext gas, StatePrimitive Y, const CeedScalar x[3]) { 85dc805cc4SLeila Ghaffari StateConservative U; 86dc805cc4SLeila Ghaffari CeedScalar R = gas->cp - gas->cv; 87dc805cc4SLeila Ghaffari U.density = Y.pressure / (R * Y.temperature); 88dc805cc4SLeila Ghaffari for (int i=0; i<3; i++) U.momentum[i] = U.density*Y.velocity[i]; 89dc805cc4SLeila Ghaffari CeedScalar e_internal = gas->cv * Y.temperature; 90dc805cc4SLeila Ghaffari CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 91dc805cc4SLeila Ghaffari CeedScalar e_potential = -Dot3(gas->g, x); 92dc805cc4SLeila Ghaffari CeedScalar e_total = e_internal + e_kinetic + e_potential; 93dc805cc4SLeila Ghaffari U.E_total = U.density*e_total; 94dc805cc4SLeila Ghaffari return U; 95dc805cc4SLeila Ghaffari } 96dc805cc4SLeila Ghaffari 97dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd( 98dc805cc4SLeila Ghaffari NewtonianIdealGasContext gas, State s, StatePrimitive dY, 99dc805cc4SLeila Ghaffari const CeedScalar x[3], const CeedScalar dx[3]) { 100dc805cc4SLeila Ghaffari StateConservative dU; 101dc805cc4SLeila Ghaffari CeedScalar R = gas->cp - gas->cv; 102dc805cc4SLeila Ghaffari dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / 103dc805cc4SLeila Ghaffari (R * s.Y.temperature * s.Y.temperature); 104dc805cc4SLeila Ghaffari for (int i=0; i<3; i++) { 105dc805cc4SLeila Ghaffari dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i]; 106dc805cc4SLeila Ghaffari } 107dc805cc4SLeila Ghaffari CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 108dc805cc4SLeila Ghaffari CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 109dc805cc4SLeila Ghaffari CeedScalar e_potential = -Dot3(gas->g, x); 110dc805cc4SLeila Ghaffari CeedScalar de_potential = -Dot3(gas->g, dx); 111dc805cc4SLeila Ghaffari CeedScalar e_internal = gas->cv * s.Y.temperature; 112dc805cc4SLeila Ghaffari CeedScalar de_internal = gas->cv * dY.temperature; 113dc805cc4SLeila Ghaffari CeedScalar e_total = e_internal + e_kinetic + e_potential; 114dc805cc4SLeila Ghaffari CeedScalar de_total = de_internal + de_kinetic + de_potential; 115dc805cc4SLeila Ghaffari dU.E_total = dU.density*e_total + s.U.density*de_total; 116dc805cc4SLeila Ghaffari return dU; 117dc805cc4SLeila Ghaffari } 118dc805cc4SLeila Ghaffari 119*20840d50SJames Wright // Function pointer types for generic state array -> State struct functions 120*20840d50SJames Wright typedef State (*StateFromQi_t)(NewtonianIdealGasContext gas, 121*20840d50SJames Wright const CeedScalar qi[5], const CeedScalar x[3]); 122*20840d50SJames Wright typedef State (*StateFromQi_fwd_t)(NewtonianIdealGasContext gas, 123*20840d50SJames Wright State s, const CeedScalar dqi[5], 124*20840d50SJames Wright const CeedScalar x[3], const CeedScalar dx[3]); 125*20840d50SJames Wright 126c6e8c570SJames Wright CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, 127c6e8c570SJames Wright const CeedScalar U[5], const CeedScalar x[3]) { 128c6e8c570SJames Wright State s; 129c6e8c570SJames Wright s.U.density = U[0]; 130c6e8c570SJames Wright s.U.momentum[0] = U[1]; 131c6e8c570SJames Wright s.U.momentum[1] = U[2]; 132c6e8c570SJames Wright s.U.momentum[2] = U[3]; 133c6e8c570SJames Wright s.U.E_total = U[4]; 134c6e8c570SJames Wright s.Y = StatePrimitiveFromConservative(gas, s.U, x); 135c6e8c570SJames Wright return s; 136c6e8c570SJames Wright } 137c6e8c570SJames Wright 138c6e8c570SJames Wright CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, 139c6e8c570SJames Wright State s, const CeedScalar dU[5], 140c6e8c570SJames Wright const CeedScalar x[3], const CeedScalar dx[3]) { 141c6e8c570SJames Wright State ds; 142c6e8c570SJames Wright ds.U.density = dU[0]; 143c6e8c570SJames Wright ds.U.momentum[0] = dU[1]; 144c6e8c570SJames Wright ds.U.momentum[1] = dU[2]; 145c6e8c570SJames Wright ds.U.momentum[2] = dU[3]; 146c6e8c570SJames Wright ds.U.E_total = dU[4]; 147c6e8c570SJames Wright ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx); 148c6e8c570SJames Wright return ds; 149c6e8c570SJames Wright } 150c6e8c570SJames Wright 151dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, 152dc805cc4SLeila Ghaffari const CeedScalar Y[5], const CeedScalar x[3]) { 153dc805cc4SLeila Ghaffari State s; 154dc805cc4SLeila Ghaffari s.Y.pressure = Y[0]; 155dc805cc4SLeila Ghaffari s.Y.velocity[0] = Y[1]; 156dc805cc4SLeila Ghaffari s.Y.velocity[1] = Y[2]; 157dc805cc4SLeila Ghaffari s.Y.velocity[2] = Y[3]; 158dc805cc4SLeila Ghaffari s.Y.temperature = Y[4]; 159dc805cc4SLeila Ghaffari s.U = StateConservativeFromPrimitive(gas, s.Y, x); 160dc805cc4SLeila Ghaffari return s; 161dc805cc4SLeila Ghaffari } 162dc805cc4SLeila Ghaffari 163dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, 164dc805cc4SLeila Ghaffari State s, const CeedScalar dY[5], 165dc805cc4SLeila Ghaffari const CeedScalar x[3], const CeedScalar dx[3]) { 166dc805cc4SLeila Ghaffari State ds; 167dc805cc4SLeila Ghaffari ds.Y.pressure = dY[0]; 168dc805cc4SLeila Ghaffari ds.Y.velocity[0] = dY[1]; 169dc805cc4SLeila Ghaffari ds.Y.velocity[1] = dY[2]; 170dc805cc4SLeila Ghaffari ds.Y.velocity[2] = dY[3]; 171dc805cc4SLeila Ghaffari ds.Y.temperature = dY[4]; 172dc805cc4SLeila Ghaffari ds.U = StateConservativeFromPrimitive_fwd(gas, s, ds.Y, x, dx); 173dc805cc4SLeila Ghaffari return ds; 174dc805cc4SLeila Ghaffari } 175dc805cc4SLeila Ghaffari 176c6e8c570SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, 177c6e8c570SJames Wright StateConservative Flux[3]) { 178c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 179c6e8c570SJames Wright Flux[i].density = s.U.momentum[i]; 180c6e8c570SJames Wright for (CeedInt j=0; j<3; j++) 181c6e8c570SJames Wright Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j] 182c6e8c570SJames Wright + s.Y.pressure * (i == j); 183c6e8c570SJames Wright Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i]; 184c6e8c570SJames Wright } 185c6e8c570SJames Wright } 186c6e8c570SJames Wright 187c6e8c570SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, 188c6e8c570SJames Wright State s, State ds, StateConservative dFlux[3]) { 189c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 190c6e8c570SJames Wright dFlux[i].density = ds.U.momentum[i]; 191c6e8c570SJames Wright for (CeedInt j=0; j<3; j++) 192c6e8c570SJames Wright dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] + 193c6e8c570SJames Wright s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j); 194c6e8c570SJames Wright dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] + 195c6e8c570SJames Wright (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i]; 196c6e8c570SJames Wright } 197c6e8c570SJames Wright } 198c6e8c570SJames Wright 1992b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, 2002b89d87eSLeila Ghaffari State s, State ds[3], CeedScalar strong_conv[5]) { 2012b89d87eSLeila Ghaffari for (CeedInt i=0; i<5; i++) strong_conv[i] = 0; 2022b89d87eSLeila Ghaffari for (CeedInt i=0; i<3; i++) { 2032b89d87eSLeila Ghaffari StateConservative dF[3]; 2042b89d87eSLeila Ghaffari FluxInviscid_fwd(gas, s, ds[i], dF); 2052b89d87eSLeila Ghaffari CeedScalar dF_i[5]; 2062b89d87eSLeila Ghaffari UnpackState_U(dF[i], dF_i); 2072b89d87eSLeila Ghaffari for (CeedInt j=0; j<5; j++) 2082b89d87eSLeila Ghaffari strong_conv[j] += dF_i[j]; 2092b89d87eSLeila Ghaffari } 2102b89d87eSLeila Ghaffari } 2112b89d87eSLeila Ghaffari 212*20840d50SJames Wright CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3], 2132b89d87eSLeila Ghaffari CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) { 2142b89d87eSLeila Ghaffari for (CeedInt j=0; j<3; j++) { 2152b89d87eSLeila Ghaffari Flux[0][j] = F_inviscid[j].density; 2162b89d87eSLeila Ghaffari for (CeedInt k=0; k<3; k++) 2172b89d87eSLeila Ghaffari Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j]; 2182b89d87eSLeila Ghaffari Flux[4][j] = F_inviscid[j].E_total + Fe[j]; 2192b89d87eSLeila Ghaffari } 2202b89d87eSLeila Ghaffari } 2212b89d87eSLeila Ghaffari 2225bce47c7SJames Wright CEED_QFUNCTION_HELPER void FluxTotal_Boundary( 2235bce47c7SJames Wright const StateConservative F_inviscid[3], const CeedScalar stress[3][3], 2245bce47c7SJames Wright const CeedScalar Fe[3], const CeedScalar normal[3], CeedScalar Flux[5]) { 2255bce47c7SJames Wright 2265bce47c7SJames Wright for(CeedInt j=0; j<5; j++) Flux[j] = 0.; 2275bce47c7SJames Wright for (CeedInt j=0; j<3; j++) { 2285bce47c7SJames Wright Flux[0] += F_inviscid[j].density * normal[j]; 2295bce47c7SJames Wright for (CeedInt k=0; k<3; k++) { 2305bce47c7SJames Wright Flux[k+1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j]; 2315bce47c7SJames Wright } 2325bce47c7SJames Wright Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j]; 2335bce47c7SJames Wright } 2345bce47c7SJames Wright } 2355bce47c7SJames Wright 236c6e8c570SJames Wright // Kelvin-Mandel notation 237c6e8c570SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3], 238c6e8c570SJames Wright CeedScalar strain_rate[6]) { 239c6e8c570SJames Wright const CeedScalar weight = 1 / sqrt(2.); 240c6e8c570SJames Wright strain_rate[0] = grad_s[0].Y.velocity[0]; 241c6e8c570SJames Wright strain_rate[1] = grad_s[1].Y.velocity[1]; 242c6e8c570SJames Wright strain_rate[2] = grad_s[2].Y.velocity[2]; 243c6e8c570SJames Wright strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]); 244c6e8c570SJames Wright strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]); 245c6e8c570SJames Wright strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]); 246c6e8c570SJames Wright } 247c6e8c570SJames Wright 248c6e8c570SJames Wright CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, 249c6e8c570SJames Wright const CeedScalar strain_rate[6], CeedScalar stress[6]) { 250c6e8c570SJames Wright CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2]; 251c6e8c570SJames Wright for (CeedInt i=0; i<6; i++) { 252c6e8c570SJames Wright stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3)); 253c6e8c570SJames Wright } 254c6e8c570SJames Wright } 255c6e8c570SJames Wright 256c6e8c570SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, 257c6e8c570SJames Wright StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3], 258c6e8c570SJames Wright CeedScalar Fe[3]) { 259c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 260c6e8c570SJames Wright Fe[i] = - Y.velocity[0] * stress[0][i] 261c6e8c570SJames Wright - Y.velocity[1] * stress[1][i] 262c6e8c570SJames Wright - Y.velocity[2] * stress[2][i] 263c6e8c570SJames Wright - gas->k * grad_s[i].Y.temperature; 264c6e8c570SJames Wright } 265c6e8c570SJames Wright } 266c6e8c570SJames Wright 267c6e8c570SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, 268c6e8c570SJames Wright StatePrimitive Y, StatePrimitive dY, const State grad_ds[3], 269c6e8c570SJames Wright const CeedScalar stress[3][3], 270c6e8c570SJames Wright const CeedScalar dstress[3][3], 271c6e8c570SJames Wright CeedScalar dFe[3]) { 272c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 273c6e8c570SJames Wright dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i] 274c6e8c570SJames Wright - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i] 275c6e8c570SJames Wright - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] 276c6e8c570SJames Wright - gas->k * grad_ds[i].Y.temperature; 277c6e8c570SJames Wright } 278c6e8c570SJames Wright } 279c6e8c570SJames Wright 280c6e8c570SJames Wright #endif // newtonian_state_h 281