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 <math.h> 16c6e8c570SJames Wright #include <ceed.h> 17c6e8c570SJames Wright #include "newtonian_types.h" 18*13fa47b2SJames 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 37c6e8c570SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative( 38c6e8c570SJames Wright NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) { 39c6e8c570SJames Wright StatePrimitive Y; 40c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density; 41c6e8c570SJames Wright CeedScalar e_kinetic = .5 * Dot3(Y.velocity, Y.velocity); 42c6e8c570SJames Wright CeedScalar e_potential = -Dot3(gas->g, x); 43c6e8c570SJames Wright CeedScalar e_total = U.E_total / U.density; 44c6e8c570SJames Wright CeedScalar e_internal = e_total - e_kinetic - e_potential; 45c6e8c570SJames Wright Y.temperature = e_internal / gas->cv; 46c6e8c570SJames Wright Y.pressure = (gas->cp / gas->cv - 1) * U.density * e_internal; 47c6e8c570SJames Wright return Y; 48c6e8c570SJames Wright } 49c6e8c570SJames Wright 50c6e8c570SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd( 51c6e8c570SJames Wright NewtonianIdealGasContext gas, State s, StateConservative dU, 52c6e8c570SJames Wright const CeedScalar x[3], const CeedScalar dx[3]) { 53c6e8c570SJames Wright StatePrimitive dY; 54c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 55c6e8c570SJames Wright dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density; 56c6e8c570SJames Wright } 57c6e8c570SJames Wright CeedScalar e_kinetic = .5 * Dot3(s.Y.velocity, s.Y.velocity); 58c6e8c570SJames Wright CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity); 59c6e8c570SJames Wright CeedScalar e_potential = -Dot3(gas->g, x); 60c6e8c570SJames Wright CeedScalar de_potential = -Dot3(gas->g, dx); 61c6e8c570SJames Wright CeedScalar e_total = s.U.E_total / s.U.density; 62c6e8c570SJames Wright CeedScalar de_total = (dU.E_total - e_total * dU.density) / s.U.density; 63c6e8c570SJames Wright CeedScalar e_internal = e_total - e_kinetic - e_potential; 64c6e8c570SJames Wright CeedScalar de_internal = de_total - de_kinetic - de_potential; 65c6e8c570SJames Wright dY.temperature = de_internal / gas->cv; 66c6e8c570SJames Wright dY.pressure = (gas->cp / gas->cv - 1) 67c6e8c570SJames Wright * (dU.density * e_internal + s.U.density * de_internal); 68c6e8c570SJames Wright return dY; 69c6e8c570SJames Wright } 70c6e8c570SJames Wright 71c6e8c570SJames Wright CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, 72c6e8c570SJames Wright const CeedScalar U[5], const CeedScalar x[3]) { 73c6e8c570SJames Wright State s; 74c6e8c570SJames Wright s.U.density = U[0]; 75c6e8c570SJames Wright s.U.momentum[0] = U[1]; 76c6e8c570SJames Wright s.U.momentum[1] = U[2]; 77c6e8c570SJames Wright s.U.momentum[2] = U[3]; 78c6e8c570SJames Wright s.U.E_total = U[4]; 79c6e8c570SJames Wright s.Y = StatePrimitiveFromConservative(gas, s.U, x); 80c6e8c570SJames Wright return s; 81c6e8c570SJames Wright } 82c6e8c570SJames Wright 83c6e8c570SJames Wright CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, 84c6e8c570SJames Wright State s, const CeedScalar dU[5], 85c6e8c570SJames Wright const CeedScalar x[3], const CeedScalar dx[3]) { 86c6e8c570SJames Wright State ds; 87c6e8c570SJames Wright ds.U.density = dU[0]; 88c6e8c570SJames Wright ds.U.momentum[0] = dU[1]; 89c6e8c570SJames Wright ds.U.momentum[1] = dU[2]; 90c6e8c570SJames Wright ds.U.momentum[2] = dU[3]; 91c6e8c570SJames Wright ds.U.E_total = dU[4]; 92c6e8c570SJames Wright ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx); 93c6e8c570SJames Wright return ds; 94c6e8c570SJames Wright } 95c6e8c570SJames Wright 96c6e8c570SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, 97c6e8c570SJames Wright StateConservative Flux[3]) { 98c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 99c6e8c570SJames Wright Flux[i].density = s.U.momentum[i]; 100c6e8c570SJames Wright for (CeedInt j=0; j<3; j++) 101c6e8c570SJames Wright Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j] 102c6e8c570SJames Wright + s.Y.pressure * (i == j); 103c6e8c570SJames Wright Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i]; 104c6e8c570SJames Wright } 105c6e8c570SJames Wright } 106c6e8c570SJames Wright 107c6e8c570SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, 108c6e8c570SJames Wright State s, State ds, StateConservative dFlux[3]) { 109c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 110c6e8c570SJames Wright dFlux[i].density = ds.U.momentum[i]; 111c6e8c570SJames Wright for (CeedInt j=0; j<3; j++) 112c6e8c570SJames Wright dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] + 113c6e8c570SJames Wright s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j); 114c6e8c570SJames Wright dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] + 115c6e8c570SJames Wright (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i]; 116c6e8c570SJames Wright } 117c6e8c570SJames Wright } 118c6e8c570SJames Wright 119c6e8c570SJames Wright // Kelvin-Mandel notation 120c6e8c570SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3], 121c6e8c570SJames Wright CeedScalar strain_rate[6]) { 122c6e8c570SJames Wright const CeedScalar weight = 1 / sqrt(2.); 123c6e8c570SJames Wright strain_rate[0] = grad_s[0].Y.velocity[0]; 124c6e8c570SJames Wright strain_rate[1] = grad_s[1].Y.velocity[1]; 125c6e8c570SJames Wright strain_rate[2] = grad_s[2].Y.velocity[2]; 126c6e8c570SJames Wright strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]); 127c6e8c570SJames Wright strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]); 128c6e8c570SJames Wright strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]); 129c6e8c570SJames Wright } 130c6e8c570SJames Wright 131c6e8c570SJames Wright CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, 132c6e8c570SJames Wright const CeedScalar strain_rate[6], CeedScalar stress[6]) { 133c6e8c570SJames Wright CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2]; 134c6e8c570SJames Wright for (CeedInt i=0; i<6; i++) { 135c6e8c570SJames Wright stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3)); 136c6e8c570SJames Wright } 137c6e8c570SJames Wright } 138c6e8c570SJames Wright 139c6e8c570SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, 140c6e8c570SJames Wright StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3], 141c6e8c570SJames Wright CeedScalar Fe[3]) { 142c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 143c6e8c570SJames Wright Fe[i] = - Y.velocity[0] * stress[0][i] 144c6e8c570SJames Wright - Y.velocity[1] * stress[1][i] 145c6e8c570SJames Wright - Y.velocity[2] * stress[2][i] 146c6e8c570SJames Wright - gas->k * grad_s[i].Y.temperature; 147c6e8c570SJames Wright } 148c6e8c570SJames Wright } 149c6e8c570SJames Wright 150c6e8c570SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, 151c6e8c570SJames Wright StatePrimitive Y, StatePrimitive dY, const State grad_ds[3], 152c6e8c570SJames Wright const CeedScalar stress[3][3], 153c6e8c570SJames Wright const CeedScalar dstress[3][3], 154c6e8c570SJames Wright CeedScalar dFe[3]) { 155c6e8c570SJames Wright for (CeedInt i=0; i<3; i++) { 156c6e8c570SJames Wright dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i] 157c6e8c570SJames Wright - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i] 158c6e8c570SJames Wright - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] 159c6e8c570SJames Wright - gas->k * grad_ds[i].Y.temperature; 160c6e8c570SJames Wright } 161c6e8c570SJames Wright } 162c6e8c570SJames Wright 163c6e8c570SJames Wright #endif // newtonian_state_h 164