1*c6e8c570SJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2*c6e8c570SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3*c6e8c570SJames Wright // 4*c6e8c570SJames Wright // SPDX-License-Identifier: BSD-2-Clause 5*c6e8c570SJames Wright // 6*c6e8c570SJames Wright // This file is part of CEED: http://github.com/ceed 7*c6e8c570SJames Wright 8*c6e8c570SJames Wright /// @file 9*c6e8c570SJames Wright /// Structs and helper functions regarding the state of a newtonian simulation 10*c6e8c570SJames Wright 11*c6e8c570SJames Wright 12*c6e8c570SJames Wright #ifndef newtonian_state_h 13*c6e8c570SJames Wright #define newtonian_state_h 14*c6e8c570SJames Wright 15*c6e8c570SJames Wright #include <math.h> 16*c6e8c570SJames Wright #include <ceed.h> 17*c6e8c570SJames Wright #include "newtonian_types.h" 18*c6e8c570SJames Wright 19*c6e8c570SJames Wright typedef struct { 20*c6e8c570SJames Wright CeedScalar pressure; 21*c6e8c570SJames Wright CeedScalar velocity[3]; 22*c6e8c570SJames Wright CeedScalar temperature; 23*c6e8c570SJames Wright } StatePrimitive; 24*c6e8c570SJames Wright 25*c6e8c570SJames Wright typedef struct { 26*c6e8c570SJames Wright CeedScalar density; 27*c6e8c570SJames Wright CeedScalar momentum[3]; 28*c6e8c570SJames Wright CeedScalar E_total; 29*c6e8c570SJames Wright } StateConservative; 30*c6e8c570SJames Wright 31*c6e8c570SJames Wright typedef struct { 32*c6e8c570SJames Wright StateConservative U; 33*c6e8c570SJames Wright StatePrimitive Y; 34*c6e8c570SJames Wright } State; 35*c6e8c570SJames Wright 36*c6e8c570SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative( 37*c6e8c570SJames Wright NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) { 38*c6e8c570SJames Wright StatePrimitive Y; 39*c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density; 40*c6e8c570SJames Wright CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 41*c6e8c570SJames Wright CeedScalar e_potential = -Dot3(gas->g, x); 42*c6e8c570SJames Wright CeedScalar e_total = U.E_total / U.density; 43*c6e8c570SJames Wright CeedScalar e_internal = e_total - e_kinetic - e_potential; 44*c6e8c570SJames Wright Y.temperature = e_internal / gas->cv; 45*c6e8c570SJames Wright Y.pressure = (gas->cp / gas->cv - 1) * U.density * e_internal; 46*c6e8c570SJames Wright return Y; 47*c6e8c570SJames Wright } 48*c6e8c570SJames Wright 49*c6e8c570SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd( 50*c6e8c570SJames Wright NewtonianIdealGasContext gas, State s, StateConservative dU, 51*c6e8c570SJames Wright const CeedScalar x[3], const CeedScalar dx[3]) { 52*c6e8c570SJames Wright StatePrimitive dY; 53*c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 54*c6e8c570SJames Wright dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density; 55*c6e8c570SJames Wright } 56*c6e8c570SJames Wright CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 57*c6e8c570SJames Wright CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 58*c6e8c570SJames Wright CeedScalar e_potential = -Dot3(gas->g, x); 59*c6e8c570SJames Wright CeedScalar de_potential = -Dot3(gas->g, dx); 60*c6e8c570SJames Wright CeedScalar e_total = s.U.E_total / s.U.density; 61*c6e8c570SJames Wright CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density; 62*c6e8c570SJames Wright CeedScalar e_internal = e_total - e_kinetic - e_potential; 63*c6e8c570SJames Wright CeedScalar de_internal = de_total - de_kinetic - de_potential; 64*c6e8c570SJames Wright dY.temperature = de_internal / gas->cv; 65*c6e8c570SJames Wright dY.pressure = (gas->cp / gas->cv - 1) 66*c6e8c570SJames Wright * (dU.density * e_internal + s.U.density * de_internal); 67*c6e8c570SJames Wright return dY; 68*c6e8c570SJames Wright } 69*c6e8c570SJames Wright 70*c6e8c570SJames Wright CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, 71*c6e8c570SJames Wright const CeedScalar U[5], const CeedScalar x[3]) { 72*c6e8c570SJames Wright State s; 73*c6e8c570SJames Wright s.U.density = U[0]; 74*c6e8c570SJames Wright s.U.momentum[0] = U[1]; 75*c6e8c570SJames Wright s.U.momentum[1] = U[2]; 76*c6e8c570SJames Wright s.U.momentum[2] = U[3]; 77*c6e8c570SJames Wright s.U.E_total = U[4]; 78*c6e8c570SJames Wright s.Y = StatePrimitiveFromConservative(gas, s.U, x); 79*c6e8c570SJames Wright return s; 80*c6e8c570SJames Wright } 81*c6e8c570SJames Wright 82*c6e8c570SJames Wright CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, 83*c6e8c570SJames Wright State s, const CeedScalar dU[5], 84*c6e8c570SJames Wright const CeedScalar x[3], const CeedScalar dx[3]) { 85*c6e8c570SJames Wright State ds; 86*c6e8c570SJames Wright ds.U.density = dU[0]; 87*c6e8c570SJames Wright ds.U.momentum[0] = dU[1]; 88*c6e8c570SJames Wright ds.U.momentum[1] = dU[2]; 89*c6e8c570SJames Wright ds.U.momentum[2] = dU[3]; 90*c6e8c570SJames Wright ds.U.E_total = dU[4]; 91*c6e8c570SJames Wright ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx); 92*c6e8c570SJames Wright return ds; 93*c6e8c570SJames Wright } 94*c6e8c570SJames Wright 95*c6e8c570SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, 96*c6e8c570SJames Wright StateConservative Flux[3]) { 97*c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 98*c6e8c570SJames Wright Flux[i].density = s.U.momentum[i]; 99*c6e8c570SJames Wright for (CeedInt j=0; j<3; j++) 100*c6e8c570SJames Wright Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j] 101*c6e8c570SJames Wright + s.Y.pressure * (i == j); 102*c6e8c570SJames Wright Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i]; 103*c6e8c570SJames Wright } 104*c6e8c570SJames Wright } 105*c6e8c570SJames Wright 106*c6e8c570SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, 107*c6e8c570SJames Wright State s, State ds, StateConservative dFlux[3]) { 108*c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 109*c6e8c570SJames Wright dFlux[i].density = ds.U.momentum[i]; 110*c6e8c570SJames Wright for (CeedInt j=0; j<3; j++) 111*c6e8c570SJames Wright dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] + 112*c6e8c570SJames Wright s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j); 113*c6e8c570SJames Wright dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] + 114*c6e8c570SJames Wright (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i]; 115*c6e8c570SJames Wright } 116*c6e8c570SJames Wright } 117*c6e8c570SJames Wright 118*c6e8c570SJames Wright // Kelvin-Mandel notation 119*c6e8c570SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3], 120*c6e8c570SJames Wright CeedScalar strain_rate[6]) { 121*c6e8c570SJames Wright const CeedScalar weight = 1 / sqrt(2.); 122*c6e8c570SJames Wright strain_rate[0] = grad_s[0].Y.velocity[0]; 123*c6e8c570SJames Wright strain_rate[1] = grad_s[1].Y.velocity[1]; 124*c6e8c570SJames Wright strain_rate[2] = grad_s[2].Y.velocity[2]; 125*c6e8c570SJames Wright strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]); 126*c6e8c570SJames Wright strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]); 127*c6e8c570SJames Wright strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]); 128*c6e8c570SJames Wright } 129*c6e8c570SJames Wright 130*c6e8c570SJames Wright CEED_QFUNCTION_HELPER void KMUnpack(const CeedScalar v[6], CeedScalar A[3][3]) { 131*c6e8c570SJames Wright const CeedScalar weight = 1 / sqrt(2.); 132*c6e8c570SJames Wright A[0][0] = v[0]; 133*c6e8c570SJames Wright A[1][1] = v[1]; 134*c6e8c570SJames Wright A[2][2] = v[2]; 135*c6e8c570SJames Wright A[2][1] = A[1][2] = weight * v[3]; 136*c6e8c570SJames Wright A[2][0] = A[0][2] = weight * v[4]; 137*c6e8c570SJames Wright A[1][0] = A[0][1] = weight * v[5]; 138*c6e8c570SJames Wright } 139*c6e8c570SJames Wright 140*c6e8c570SJames Wright CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, 141*c6e8c570SJames Wright const CeedScalar strain_rate[6], CeedScalar stress[6]) { 142*c6e8c570SJames Wright CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2]; 143*c6e8c570SJames Wright for (CeedInt i=0; i<6; i++) { 144*c6e8c570SJames Wright stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3)); 145*c6e8c570SJames Wright } 146*c6e8c570SJames Wright } 147*c6e8c570SJames Wright 148*c6e8c570SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, 149*c6e8c570SJames Wright StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3], 150*c6e8c570SJames Wright CeedScalar Fe[3]) { 151*c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 152*c6e8c570SJames Wright Fe[i] = - Y.velocity[0] * stress[0][i] 153*c6e8c570SJames Wright - Y.velocity[1] * stress[1][i] 154*c6e8c570SJames Wright - Y.velocity[2] * stress[2][i] 155*c6e8c570SJames Wright - gas->k * grad_s[i].Y.temperature; 156*c6e8c570SJames Wright } 157*c6e8c570SJames Wright } 158*c6e8c570SJames Wright 159*c6e8c570SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, 160*c6e8c570SJames Wright StatePrimitive Y, StatePrimitive dY, const State grad_ds[3], 161*c6e8c570SJames Wright const CeedScalar stress[3][3], 162*c6e8c570SJames Wright const CeedScalar dstress[3][3], 163*c6e8c570SJames Wright CeedScalar dFe[3]) { 164*c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 165*c6e8c570SJames Wright dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i] 166*c6e8c570SJames Wright - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i] 167*c6e8c570SJames Wright - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] 168*c6e8c570SJames Wright - gas->k * grad_ds[i].Y.temperature; 169*c6e8c570SJames Wright } 170*c6e8c570SJames Wright } 171*c6e8c570SJames Wright 172*c6e8c570SJames Wright #endif // newtonian_state_h 173