xref: /libCEED/examples/fluids/qfunctions/newtonian_state.h (revision f0a192229832583f29b3ec686dcf91b60810da85)
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>
17738af36cSAdelekeBankole #include "ceed/types.h"
18c6e8c570SJames Wright #include "newtonian_types.h"
1913fa47b2SJames Wright #include "utils.h"
20c6e8c570SJames Wright 
21c6e8c570SJames Wright typedef struct {
22c6e8c570SJames Wright   CeedScalar pressure;
23c6e8c570SJames Wright   CeedScalar velocity[3];
24c6e8c570SJames Wright   CeedScalar temperature;
25c6e8c570SJames Wright } StatePrimitive;
26c6e8c570SJames Wright 
27c6e8c570SJames Wright typedef struct {
28c6e8c570SJames Wright   CeedScalar density;
29c6e8c570SJames Wright   CeedScalar momentum[3];
30c6e8c570SJames Wright   CeedScalar E_total;
31c6e8c570SJames Wright } StateConservative;
32c6e8c570SJames Wright 
33c6e8c570SJames Wright typedef struct {
34c6e8c570SJames Wright   StateConservative U;
35c6e8c570SJames Wright   StatePrimitive Y;
36c6e8c570SJames Wright } State;
37c6e8c570SJames Wright 
382b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_U(StateConservative s, CeedScalar U[5]) {
392b89d87eSLeila Ghaffari   U[0] = s.density;
402b89d87eSLeila Ghaffari   for (int i=0; i<3; i++) U[i+1] = s.momentum[i];
412b89d87eSLeila Ghaffari   U[4] = s.E_total;
422b89d87eSLeila Ghaffari }
432b89d87eSLeila Ghaffari 
442b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) {
452b89d87eSLeila Ghaffari   Y[0] = s.pressure;
462b89d87eSLeila Ghaffari   for (int i=0; i<3; i++) Y[i+1] = s.velocity[i];
472b89d87eSLeila Ghaffari   Y[4] = s.temperature;
482b89d87eSLeila Ghaffari }
492b89d87eSLeila Ghaffari 
502518f336SLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio(
512518f336SLeila Ghaffari   NewtonianIdealGasContext gas) {
522518f336SLeila Ghaffari   return gas->cp / gas->cv;
532518f336SLeila Ghaffari }
542518f336SLeila Ghaffari 
552518f336SLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar GasConstant(
562518f336SLeila Ghaffari   NewtonianIdealGasContext gas) {
572518f336SLeila Ghaffari   return gas->cp - gas->cv;
582518f336SLeila Ghaffari }
592518f336SLeila Ghaffari 
602518f336SLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) {
612518f336SLeila Ghaffari   return gas->cp * gas->mu / gas->k;
622518f336SLeila Ghaffari }
632518f336SLeila Ghaffari 
642518f336SLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas,
652518f336SLeila Ghaffari     CeedScalar T) {
662518f336SLeila Ghaffari   return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T);
672518f336SLeila Ghaffari }
682518f336SLeila Ghaffari 
692518f336SLeila Ghaffari CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas,
702518f336SLeila Ghaffari                                       CeedScalar T, CeedScalar u) {
712518f336SLeila Ghaffari   return u / SoundSpeed(gas, T);
722518f336SLeila Ghaffari }
732518f336SLeila Ghaffari 
74*f0a19222SJames Wright CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy(
75*f0a19222SJames Wright   NewtonianIdealGasContext gas, const State s) {
76*f0a19222SJames Wright   // Ignoring potential energy
77*f0a19222SJames Wright   CeedScalar e_internal = gas->cv*s.Y.temperature;
78*f0a19222SJames Wright   CeedScalar e_kinetic  = 0.5*Dot3(s.Y.velocity, s.Y.velocity);
79*f0a19222SJames Wright   return e_internal + e_kinetic + s.Y.pressure/s.U.density;
80*f0a19222SJames Wright }
81*f0a19222SJames Wright 
82*f0a19222SJames Wright CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy_fwd(
83*f0a19222SJames Wright   NewtonianIdealGasContext gas, const State s, const State ds) {
84*f0a19222SJames Wright   // Ignoring potential energy
85*f0a19222SJames Wright   CeedScalar de_kinetic  = Dot3(ds.Y.velocity, s.Y.velocity);
86*f0a19222SJames Wright   CeedScalar de_internal = gas->cv * ds.Y.temperature;
87*f0a19222SJames Wright   return de_internal + de_kinetic + ds.Y.pressure/s.U.density
88*f0a19222SJames Wright          - s.Y.pressure/Square(s.U.density)*ds.U.density;
89*f0a19222SJames Wright }
90*f0a19222SJames Wright 
91c6e8c570SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(
92c6e8c570SJames Wright   NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) {
93c6e8c570SJames Wright   StatePrimitive Y;
94c6e8c570SJames Wright   for (CeedInt i=0; i<3; i++) Y.velocity[i] = U.momentum[i] / U.density;
95c6e8c570SJames Wright   CeedScalar e_kinetic   = .5 * Dot3(Y.velocity, Y.velocity);
96c6e8c570SJames Wright   CeedScalar e_potential = -Dot3(gas->g, x);
97c6e8c570SJames Wright   CeedScalar e_total     = U.E_total / U.density;
98c6e8c570SJames Wright   CeedScalar e_internal  = e_total - e_kinetic - e_potential;
99c6e8c570SJames Wright   Y.temperature          = e_internal / gas->cv;
1002518f336SLeila Ghaffari   Y.pressure = (HeatCapacityRatio(gas) - 1) * U.density * e_internal;
101c6e8c570SJames Wright   return Y;
102c6e8c570SJames Wright }
103c6e8c570SJames Wright 
104c6e8c570SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(
105c6e8c570SJames Wright   NewtonianIdealGasContext gas, State s, StateConservative dU,
106c6e8c570SJames Wright   const CeedScalar x[3], const CeedScalar dx[3]) {
107c6e8c570SJames Wright   StatePrimitive dY;
108c6e8c570SJames Wright   for (CeedInt i=0; i<3; i++) {
109c6e8c570SJames Wright     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
110c6e8c570SJames Wright   }
111c6e8c570SJames Wright   CeedScalar e_kinetic    = .5 * Dot3(s.Y.velocity, s.Y.velocity);
112c6e8c570SJames Wright   CeedScalar de_kinetic   = Dot3(dY.velocity, s.Y.velocity);
113c6e8c570SJames Wright   CeedScalar e_potential  = -Dot3(gas->g, x);
114c6e8c570SJames Wright   CeedScalar de_potential = -Dot3(gas->g, dx);
115c6e8c570SJames Wright   CeedScalar e_total      = s.U.E_total / s.U.density;
116c6e8c570SJames Wright   CeedScalar de_total     = (dU.E_total - e_total * dU.density) / s.U.density;
117c6e8c570SJames Wright   CeedScalar e_internal   = e_total - e_kinetic - e_potential;
118c6e8c570SJames Wright   CeedScalar de_internal  = de_total - de_kinetic - de_potential;
119c6e8c570SJames Wright   dY.temperature          = de_internal / gas->cv;
1202518f336SLeila Ghaffari   dY.pressure = (HeatCapacityRatio(gas) - 1)
121c6e8c570SJames Wright                 * (dU.density * e_internal + s.U.density * de_internal);
122c6e8c570SJames Wright   return dY;
123c6e8c570SJames Wright }
124c6e8c570SJames Wright 
125dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(
126dc805cc4SLeila Ghaffari   NewtonianIdealGasContext gas, StatePrimitive Y, const CeedScalar x[3]) {
127dc805cc4SLeila Ghaffari   StateConservative U;
1282518f336SLeila Ghaffari   U.density = Y.pressure / (GasConstant(gas) * Y.temperature);
129dc805cc4SLeila Ghaffari   for (int i=0; i<3; i++) U.momentum[i] = U.density*Y.velocity[i];
130dc805cc4SLeila Ghaffari   CeedScalar e_internal  = gas->cv * Y.temperature;
131dc805cc4SLeila Ghaffari   CeedScalar e_kinetic   = .5 * Dot3(Y.velocity, Y.velocity);
132dc805cc4SLeila Ghaffari   CeedScalar e_potential = -Dot3(gas->g, x);
133dc805cc4SLeila Ghaffari   CeedScalar e_total     = e_internal + e_kinetic + e_potential;
134dc805cc4SLeila Ghaffari   U.E_total = U.density*e_total;
135dc805cc4SLeila Ghaffari   return U;
136dc805cc4SLeila Ghaffari }
137dc805cc4SLeila Ghaffari 
138dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(
139dc805cc4SLeila Ghaffari   NewtonianIdealGasContext gas, State s, StatePrimitive dY,
140dc805cc4SLeila Ghaffari   const CeedScalar x[3], const CeedScalar dx[3]) {
141dc805cc4SLeila Ghaffari   StateConservative dU;
142dc805cc4SLeila Ghaffari   dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) /
1432518f336SLeila Ghaffari                (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
144dc805cc4SLeila Ghaffari   for (int i=0; i<3; i++) {
145dc805cc4SLeila Ghaffari     dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i];
146dc805cc4SLeila Ghaffari   }
147dc805cc4SLeila Ghaffari   CeedScalar e_kinetic    = .5 * Dot3(s.Y.velocity, s.Y.velocity);
148dc805cc4SLeila Ghaffari   CeedScalar de_kinetic   = Dot3(dY.velocity, s.Y.velocity);
149dc805cc4SLeila Ghaffari   CeedScalar e_potential  = -Dot3(gas->g, x);
150dc805cc4SLeila Ghaffari   CeedScalar de_potential = -Dot3(gas->g, dx);
151dc805cc4SLeila Ghaffari   CeedScalar e_internal   = gas->cv * s.Y.temperature;
152dc805cc4SLeila Ghaffari   CeedScalar de_internal  = gas->cv * dY.temperature;
153dc805cc4SLeila Ghaffari   CeedScalar e_total      = e_internal + e_kinetic + e_potential;
154dc805cc4SLeila Ghaffari   CeedScalar de_total     = de_internal + de_kinetic + de_potential;
155dc805cc4SLeila Ghaffari   dU.E_total = dU.density*e_total + s.U.density*de_total;
156dc805cc4SLeila Ghaffari   return dU;
157dc805cc4SLeila Ghaffari }
158dc805cc4SLeila Ghaffari 
15920840d50SJames Wright // Function pointer types for generic state array -> State struct functions
16020840d50SJames Wright typedef State (*StateFromQi_t)(NewtonianIdealGasContext gas,
16120840d50SJames Wright                                const CeedScalar qi[5], const CeedScalar x[3]);
16220840d50SJames Wright typedef State (*StateFromQi_fwd_t)(NewtonianIdealGasContext gas,
16320840d50SJames Wright                                    State s, const CeedScalar dqi[5],
16420840d50SJames Wright                                    const CeedScalar x[3], const CeedScalar dx[3]);
16520840d50SJames Wright 
166c6e8c570SJames Wright CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas,
167c6e8c570SJames Wright                                        const CeedScalar U[5], const CeedScalar x[3]) {
168c6e8c570SJames Wright   State s;
169c6e8c570SJames Wright   s.U.density     = U[0];
170c6e8c570SJames Wright   s.U.momentum[0] = U[1];
171c6e8c570SJames Wright   s.U.momentum[1] = U[2];
172c6e8c570SJames Wright   s.U.momentum[2] = U[3];
173c6e8c570SJames Wright   s.U.E_total     = U[4];
174c6e8c570SJames Wright   s.Y = StatePrimitiveFromConservative(gas, s.U, x);
175c6e8c570SJames Wright   return s;
176c6e8c570SJames Wright }
177c6e8c570SJames Wright 
178c6e8c570SJames Wright CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas,
179c6e8c570SJames Wright     State s, const CeedScalar dU[5],
180c6e8c570SJames Wright     const CeedScalar x[3], const CeedScalar dx[3]) {
181c6e8c570SJames Wright   State ds;
182c6e8c570SJames Wright   ds.U.density     = dU[0];
183c6e8c570SJames Wright   ds.U.momentum[0] = dU[1];
184c6e8c570SJames Wright   ds.U.momentum[1] = dU[2];
185c6e8c570SJames Wright   ds.U.momentum[2] = dU[3];
186c6e8c570SJames Wright   ds.U.E_total     = dU[4];
187c6e8c570SJames Wright   ds.Y = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx);
188c6e8c570SJames Wright   return ds;
189c6e8c570SJames Wright }
190c6e8c570SJames Wright 
191dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas,
192dc805cc4SLeila Ghaffari                                        const CeedScalar Y[5], const CeedScalar x[3]) {
193dc805cc4SLeila Ghaffari   State s;
194dc805cc4SLeila Ghaffari   s.Y.pressure    = Y[0];
195dc805cc4SLeila Ghaffari   s.Y.velocity[0] = Y[1];
196dc805cc4SLeila Ghaffari   s.Y.velocity[1] = Y[2];
197dc805cc4SLeila Ghaffari   s.Y.velocity[2] = Y[3];
198dc805cc4SLeila Ghaffari   s.Y.temperature = Y[4];
199dc805cc4SLeila Ghaffari   s.U = StateConservativeFromPrimitive(gas, s.Y, x);
200dc805cc4SLeila Ghaffari   return s;
201dc805cc4SLeila Ghaffari }
202dc805cc4SLeila Ghaffari 
203dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas,
204dc805cc4SLeila Ghaffari     State s, const CeedScalar dY[5],
205dc805cc4SLeila Ghaffari     const CeedScalar x[3], const CeedScalar dx[3]) {
206dc805cc4SLeila Ghaffari   State ds;
207dc805cc4SLeila Ghaffari   ds.Y.pressure    = dY[0];
208dc805cc4SLeila Ghaffari   ds.Y.velocity[0] = dY[1];
209dc805cc4SLeila Ghaffari   ds.Y.velocity[1] = dY[2];
210dc805cc4SLeila Ghaffari   ds.Y.velocity[2] = dY[3];
211dc805cc4SLeila Ghaffari   ds.Y.temperature = dY[4];
212dc805cc4SLeila Ghaffari   ds.U = StateConservativeFromPrimitive_fwd(gas, s, ds.Y, x, dx);
213dc805cc4SLeila Ghaffari   return ds;
214dc805cc4SLeila Ghaffari }
215dc805cc4SLeila Ghaffari 
216cea0a271SJames Wright // Function pointer types for State struct -> generic state array
217cea0a271SJames Wright typedef void (*StateToQi_t)(NewtonianIdealGasContext gas,
218cea0a271SJames Wright                             const State input, CeedScalar qi[5]);
219cea0a271SJames Wright 
220cea0a271SJames Wright CEED_QFUNCTION_HELPER void StateToU(NewtonianIdealGasContext gas,
221cea0a271SJames Wright                                     const State input, CeedScalar U[5]) {
222cea0a271SJames Wright   UnpackState_U(input.U, U);
223cea0a271SJames Wright }
224cea0a271SJames Wright 
225cea0a271SJames Wright CEED_QFUNCTION_HELPER void StateToY(NewtonianIdealGasContext gas,
226cea0a271SJames Wright                                     const State input, CeedScalar Y[5]) {
227cea0a271SJames Wright   UnpackState_Y(input.Y, Y);
228cea0a271SJames Wright }
229cea0a271SJames Wright 
230c6e8c570SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s,
231c6e8c570SJames Wright                                         StateConservative Flux[3]) {
232c6e8c570SJames Wright   for (CeedInt i=0; i<3; i++) {
233c6e8c570SJames Wright     Flux[i].density = s.U.momentum[i];
234c6e8c570SJames Wright     for (CeedInt j=0; j<3; j++)
235c6e8c570SJames Wright       Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j]
236c6e8c570SJames Wright                             + s.Y.pressure * (i == j);
237c6e8c570SJames Wright     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
238c6e8c570SJames Wright   }
239c6e8c570SJames Wright }
240c6e8c570SJames Wright 
241*f0a19222SJames Wright CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas,
242*f0a19222SJames Wright     State s, State ds, StateConservative dFlux[3]) {
243*f0a19222SJames Wright   for (CeedInt i=0; i<3; i++) {
244*f0a19222SJames Wright     dFlux[i].density = ds.U.momentum[i];
245*f0a19222SJames Wright     for (CeedInt j=0; j<3; j++)
246*f0a19222SJames Wright       dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] +
247*f0a19222SJames Wright                              s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j);
248*f0a19222SJames Wright     dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] +
249*f0a19222SJames Wright                        (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i];
250*f0a19222SJames Wright   }
251*f0a19222SJames Wright }
252*f0a19222SJames Wright 
253c98a0616SJames Wright CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal(
254c98a0616SJames Wright   NewtonianIdealGasContext gas, State s, const CeedScalar normal[3]) {
255738af36cSAdelekeBankole   StateConservative Flux[3], Flux_dot_n = {0};
256738af36cSAdelekeBankole   FluxInviscid(gas, s, Flux);
257738af36cSAdelekeBankole   for (CeedInt i=0; i<3; i++) {
258738af36cSAdelekeBankole     Flux_dot_n.density += Flux[i].density * normal[i];
259738af36cSAdelekeBankole     for (CeedInt j=0; j<3; j++)
260738af36cSAdelekeBankole       Flux_dot_n.momentum[j] += Flux[i].momentum[j] * normal[i];
261738af36cSAdelekeBankole     Flux_dot_n.E_total += Flux[i].E_total * normal[i];
262738af36cSAdelekeBankole   }
263738af36cSAdelekeBankole   return Flux_dot_n;
264738af36cSAdelekeBankole }
265738af36cSAdelekeBankole 
266*f0a19222SJames Wright CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal_fwd(
267*f0a19222SJames Wright   NewtonianIdealGasContext gas, State s, State ds, const CeedScalar normal[3]) {
268*f0a19222SJames Wright   StateConservative dFlux[3], Flux_dot_n = {0};
269*f0a19222SJames Wright   FluxInviscid_fwd(gas, s, ds, dFlux);
270c6e8c570SJames Wright   for (CeedInt i=0; i<3; i++) {
271*f0a19222SJames Wright     Flux_dot_n.density += dFlux[i].density * normal[i];
272c6e8c570SJames Wright     for (CeedInt j=0; j<3; j++)
273*f0a19222SJames Wright       Flux_dot_n.momentum[j] += dFlux[i].momentum[j] * normal[i];
274*f0a19222SJames Wright     Flux_dot_n.E_total += dFlux[i].E_total * normal[i];
275c6e8c570SJames Wright   }
276*f0a19222SJames Wright   return Flux_dot_n;
277c6e8c570SJames Wright }
278c6e8c570SJames Wright 
2792b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas,
2802b89d87eSLeila Ghaffari     State s, State ds[3], CeedScalar strong_conv[5]) {
2812b89d87eSLeila Ghaffari   for (CeedInt i=0; i<5; i++) strong_conv[i] = 0;
2822b89d87eSLeila Ghaffari   for (CeedInt i=0; i<3; i++) {
2832b89d87eSLeila Ghaffari     StateConservative dF[3];
2842b89d87eSLeila Ghaffari     FluxInviscid_fwd(gas, s, ds[i], dF);
2852b89d87eSLeila Ghaffari     CeedScalar dF_i[5];
2862b89d87eSLeila Ghaffari     UnpackState_U(dF[i], dF_i);
2872b89d87eSLeila Ghaffari     for (CeedInt j=0; j<5; j++)
2882b89d87eSLeila Ghaffari       strong_conv[j] += dF_i[j];
2892b89d87eSLeila Ghaffari   }
2902b89d87eSLeila Ghaffari }
2912b89d87eSLeila Ghaffari 
29220840d50SJames Wright CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3],
2932b89d87eSLeila Ghaffari                                      CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) {
2942b89d87eSLeila Ghaffari   for (CeedInt j=0; j<3; j++) {
2952b89d87eSLeila Ghaffari     Flux[0][j] = F_inviscid[j].density;
2962b89d87eSLeila Ghaffari     for (CeedInt k=0; k<3; k++)
2972b89d87eSLeila Ghaffari       Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
2982b89d87eSLeila Ghaffari     Flux[4][j] = F_inviscid[j].E_total + Fe[j];
2992b89d87eSLeila Ghaffari   }
3002b89d87eSLeila Ghaffari }
3012b89d87eSLeila Ghaffari 
3025bce47c7SJames Wright CEED_QFUNCTION_HELPER void FluxTotal_Boundary(
3035bce47c7SJames Wright   const StateConservative F_inviscid[3], const CeedScalar stress[3][3],
3045bce47c7SJames Wright   const CeedScalar Fe[3], const CeedScalar normal[3], CeedScalar Flux[5]) {
3055bce47c7SJames Wright 
3065bce47c7SJames Wright   for (CeedInt j=0; j<5; j++) Flux[j] = 0.;
3075bce47c7SJames Wright   for (CeedInt j=0; j<3; j++) {
3085bce47c7SJames Wright     Flux[0] += F_inviscid[j].density * normal[j];
3095bce47c7SJames Wright     for (CeedInt k=0; k<3; k++) {
3105bce47c7SJames Wright       Flux[k+1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j];
3115bce47c7SJames Wright     }
3125bce47c7SJames Wright     Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j];
3135bce47c7SJames Wright   }
3145bce47c7SJames Wright }
3155bce47c7SJames Wright 
316c6e8c570SJames Wright // Kelvin-Mandel notation
317c6e8c570SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3],
318c6e8c570SJames Wright                                         CeedScalar strain_rate[6]) {
319c6e8c570SJames Wright   const CeedScalar weight = 1 / sqrt(2.);
320c6e8c570SJames Wright   strain_rate[0] = grad_s[0].Y.velocity[0];
321c6e8c570SJames Wright   strain_rate[1] = grad_s[1].Y.velocity[1];
322c6e8c570SJames Wright   strain_rate[2] = grad_s[2].Y.velocity[2];
323c6e8c570SJames Wright   strain_rate[3] = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]);
324c6e8c570SJames Wright   strain_rate[4] = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]);
325c6e8c570SJames Wright   strain_rate[5] = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]);
326c6e8c570SJames Wright }
327c6e8c570SJames Wright 
328c6e8c570SJames Wright CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas,
329c6e8c570SJames Wright     const CeedScalar strain_rate[6], CeedScalar stress[6]) {
330c6e8c570SJames Wright   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
331c6e8c570SJames Wright   for (CeedInt i=0; i<6; i++) {
332c6e8c570SJames Wright     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
333c6e8c570SJames Wright   }
334c6e8c570SJames Wright }
335c6e8c570SJames Wright 
336c6e8c570SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas,
337c6e8c570SJames Wright     StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
338c6e8c570SJames Wright     CeedScalar Fe[3]) {
339c6e8c570SJames Wright   for (CeedInt i=0; i<3; i++) {
340c6e8c570SJames Wright     Fe[i] = - Y.velocity[0] * stress[0][i]
341c6e8c570SJames Wright             - Y.velocity[1] * stress[1][i]
342c6e8c570SJames Wright             - Y.velocity[2] * stress[2][i]
343c6e8c570SJames Wright             - gas->k * grad_s[i].Y.temperature;
344c6e8c570SJames Wright   }
345c6e8c570SJames Wright }
346c6e8c570SJames Wright 
347c6e8c570SJames Wright CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas,
348c6e8c570SJames Wright     StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
349c98a0616SJames Wright     const CeedScalar stress[3][3], const CeedScalar dstress[3][3],
350c6e8c570SJames Wright     CeedScalar dFe[3]) {
351c6e8c570SJames Wright   for (CeedInt i=0; i<3; i++) {
352c6e8c570SJames Wright     dFe[i] = - Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i]
353c6e8c570SJames Wright              - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i]
354c6e8c570SJames Wright              - Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i]
355c6e8c570SJames Wright              - gas->k * grad_ds[i].Y.temperature;
356c6e8c570SJames Wright   }
357c6e8c570SJames Wright }
358c6e8c570SJames Wright 
359c6e8c570SJames Wright #endif // newtonian_state_h
360