xref: /libCEED/examples/fluids/qfunctions/newtonian_state.h (revision c95797efee8c9ebf9ca7d7c6df55b02fe3e75dc2)
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 #ifndef newtonian_state_h
12c6e8c570SJames Wright #define newtonian_state_h
13c6e8c570SJames Wright 
14c6e8c570SJames Wright #include <ceed.h>
15c9c2c079SJeremy L Thompson #include <math.h>
162b730f8bSJeremy L Thompson 
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 
492b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio(NewtonianIdealGasContext gas) { return gas->cp / gas->cv; }
502518f336SLeila Ghaffari 
512b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar GasConstant(NewtonianIdealGasContext gas) { return gas->cp - gas->cv; }
522518f336SLeila Ghaffari 
532b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) { return gas->cp * gas->mu / gas->k; }
542518f336SLeila Ghaffari 
552b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas, CeedScalar T) { return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T); }
562518f336SLeila Ghaffari 
572b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas, CeedScalar T, CeedScalar u) { return u / SoundSpeed(gas, T); }
582518f336SLeila Ghaffari 
592b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy(NewtonianIdealGasContext gas, const State s) {
60f0a19222SJames Wright   // Ignoring potential energy
61f0a19222SJames Wright   CeedScalar e_internal = gas->cv * s.Y.temperature;
62f0a19222SJames Wright   CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
63f0a19222SJames Wright   return e_internal + e_kinetic + s.Y.pressure / s.U.density;
64f0a19222SJames Wright }
65f0a19222SJames Wright 
662b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy_fwd(NewtonianIdealGasContext gas, const State s, const State ds) {
67f0a19222SJames Wright   // Ignoring potential energy
68f0a19222SJames Wright   CeedScalar de_kinetic  = Dot3(ds.Y.velocity, s.Y.velocity);
69f0a19222SJames Wright   CeedScalar de_internal = gas->cv * ds.Y.temperature;
702b730f8bSJeremy L Thompson   return de_internal + de_kinetic + ds.Y.pressure / s.U.density - s.Y.pressure / Square(s.U.density) * ds.U.density;
71f0a19222SJames Wright }
72f0a19222SJames Wright 
732b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(NewtonianIdealGasContext gas, StateConservative U, const CeedScalar x[3]) {
74c6e8c570SJames Wright   StatePrimitive Y;
75c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) Y.velocity[i] = U.momentum[i] / U.density;
76c6e8c570SJames Wright   CeedScalar e_kinetic   = .5 * Dot3(Y.velocity, Y.velocity);
77c6e8c570SJames Wright   CeedScalar e_potential = -Dot3(gas->g, x);
78c6e8c570SJames Wright   CeedScalar e_total     = U.E_total / U.density;
79c6e8c570SJames Wright   CeedScalar e_internal  = e_total - e_kinetic - e_potential;
80c6e8c570SJames Wright   Y.temperature          = e_internal / gas->cv;
812518f336SLeila Ghaffari   Y.pressure             = (HeatCapacityRatio(gas) - 1) * U.density * e_internal;
82c6e8c570SJames Wright   return Y;
83c6e8c570SJames Wright }
84c6e8c570SJames Wright 
852b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU,
86c6e8c570SJames Wright                                                                         const CeedScalar x[3], const CeedScalar dx[3]) {
87c6e8c570SJames Wright   StatePrimitive dY;
88c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
89c6e8c570SJames Wright     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
90c6e8c570SJames Wright   }
91c6e8c570SJames Wright   CeedScalar e_kinetic    = .5 * Dot3(s.Y.velocity, s.Y.velocity);
92c6e8c570SJames Wright   CeedScalar de_kinetic   = Dot3(dY.velocity, s.Y.velocity);
93c6e8c570SJames Wright   CeedScalar e_potential  = -Dot3(gas->g, x);
94c6e8c570SJames Wright   CeedScalar de_potential = -Dot3(gas->g, dx);
95c6e8c570SJames Wright   CeedScalar e_total      = s.U.E_total / s.U.density;
96c6e8c570SJames Wright   CeedScalar de_total     = (dU.E_total - e_total * dU.density) / s.U.density;
97c6e8c570SJames Wright   CeedScalar e_internal   = e_total - e_kinetic - e_potential;
98c6e8c570SJames Wright   CeedScalar de_internal  = de_total - de_kinetic - de_potential;
99c6e8c570SJames Wright   dY.temperature          = de_internal / gas->cv;
1002b730f8bSJeremy L Thompson   dY.pressure             = (HeatCapacityRatio(gas) - 1) * (dU.density * e_internal + s.U.density * de_internal);
101c6e8c570SJames Wright   return dY;
102c6e8c570SJames Wright }
103c6e8c570SJames Wright 
1042b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y, const CeedScalar x[3]) {
105dc805cc4SLeila Ghaffari   StateConservative U;
1062518f336SLeila Ghaffari   U.density = Y.pressure / (GasConstant(gas) * Y.temperature);
107dc805cc4SLeila Ghaffari   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * Y.velocity[i];
108dc805cc4SLeila Ghaffari   CeedScalar e_internal  = gas->cv * Y.temperature;
109dc805cc4SLeila Ghaffari   CeedScalar e_kinetic   = .5 * Dot3(Y.velocity, Y.velocity);
110dc805cc4SLeila Ghaffari   CeedScalar e_potential = -Dot3(gas->g, x);
111dc805cc4SLeila Ghaffari   CeedScalar e_total     = e_internal + e_kinetic + e_potential;
112dc805cc4SLeila Ghaffari   U.E_total              = U.density * e_total;
113dc805cc4SLeila Ghaffari   return U;
114dc805cc4SLeila Ghaffari }
115dc805cc4SLeila Ghaffari 
1162b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY,
117dc805cc4SLeila Ghaffari                                                                            const CeedScalar x[3], const CeedScalar dx[3]) {
118dc805cc4SLeila Ghaffari   StateConservative dU;
1192b730f8bSJeremy L Thompson   dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
120dc805cc4SLeila Ghaffari   for (int i = 0; i < 3; i++) {
121dc805cc4SLeila Ghaffari     dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i];
122dc805cc4SLeila Ghaffari   }
123dc805cc4SLeila Ghaffari   CeedScalar e_kinetic    = .5 * Dot3(s.Y.velocity, s.Y.velocity);
124dc805cc4SLeila Ghaffari   CeedScalar de_kinetic   = Dot3(dY.velocity, s.Y.velocity);
125dc805cc4SLeila Ghaffari   CeedScalar e_potential  = -Dot3(gas->g, x);
126dc805cc4SLeila Ghaffari   CeedScalar de_potential = -Dot3(gas->g, dx);
127dc805cc4SLeila Ghaffari   CeedScalar e_internal   = gas->cv * s.Y.temperature;
128dc805cc4SLeila Ghaffari   CeedScalar de_internal  = gas->cv * dY.temperature;
129dc805cc4SLeila Ghaffari   CeedScalar e_total      = e_internal + e_kinetic + e_potential;
130dc805cc4SLeila Ghaffari   CeedScalar de_total     = de_internal + de_kinetic + de_potential;
131dc805cc4SLeila Ghaffari   dU.E_total              = dU.density * e_total + s.U.density * de_total;
132dc805cc4SLeila Ghaffari   return dU;
133dc805cc4SLeila Ghaffari }
134dc805cc4SLeila Ghaffari 
135*c95797efSJed Brown // linear combination of n states
136*c95797efSJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeMult(CeedInt n, const CeedScalar a[], const StateConservative X[]) {
137*c95797efSJed Brown   StateConservative R = {0};
138*c95797efSJed Brown   for (CeedInt i = 0; i < n; i++) {
139*c95797efSJed Brown     R.density += a[i] * X[i].density;
140*c95797efSJed Brown     for (int j = 0; j < 3; j++) R.momentum[j] += a[i] * X[i].momentum[j];
141*c95797efSJed Brown     R.E_total += a[i] * X[i].E_total;
142*c95797efSJed Brown   }
14384ae27ebSJed Brown   return R;
14484ae27ebSJed Brown }
14584ae27ebSJed Brown 
14684ae27ebSJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeAXPBYPCZ(CeedScalar a, StateConservative X, CeedScalar b, StateConservative Y, CeedScalar c,
14784ae27ebSJed Brown                                                                   StateConservative Z) {
14884ae27ebSJed Brown   StateConservative R;
14984ae27ebSJed Brown   R.density = a * X.density + b * Y.density + c * Z.density;
15084ae27ebSJed Brown   for (int i = 0; i < 3; i++) R.momentum[i] = a * X.momentum[i] + b * Y.momentum[i] + c * Z.momentum[i];
15184ae27ebSJed Brown   R.E_total = a * X.E_total + b * Y.E_total + c * Z.E_total;
15284ae27ebSJed Brown   return R;
15384ae27ebSJed Brown }
15484ae27ebSJed Brown 
15520840d50SJames Wright // Function pointer types for generic state array -> State struct functions
1562b730f8bSJeremy L Thompson typedef State (*StateFromQi_t)(NewtonianIdealGasContext gas, const CeedScalar qi[5], const CeedScalar x[3]);
1572b730f8bSJeremy L Thompson typedef State (*StateFromQi_fwd_t)(NewtonianIdealGasContext gas, State s, const CeedScalar dqi[5], const CeedScalar x[3], const CeedScalar dx[3]);
15820840d50SJames Wright 
1592b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, const CeedScalar U[5], const CeedScalar x[3]) {
160c6e8c570SJames Wright   State s;
161c6e8c570SJames Wright   s.U.density     = U[0];
162c6e8c570SJames Wright   s.U.momentum[0] = U[1];
163c6e8c570SJames Wright   s.U.momentum[1] = U[2];
164c6e8c570SJames Wright   s.U.momentum[2] = U[3];
165c6e8c570SJames Wright   s.U.E_total     = U[4];
166c6e8c570SJames Wright   s.Y             = StatePrimitiveFromConservative(gas, s.U, x);
167c6e8c570SJames Wright   return s;
168c6e8c570SJames Wright }
169c6e8c570SJames Wright 
1702b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dU[5], const CeedScalar x[3],
1712b730f8bSJeremy L Thompson                                            const CeedScalar dx[3]) {
172c6e8c570SJames Wright   State ds;
173c6e8c570SJames Wright   ds.U.density     = dU[0];
174c6e8c570SJames Wright   ds.U.momentum[0] = dU[1];
175c6e8c570SJames Wright   ds.U.momentum[1] = dU[2];
176c6e8c570SJames Wright   ds.U.momentum[2] = dU[3];
177c6e8c570SJames Wright   ds.U.E_total     = dU[4];
178c6e8c570SJames Wright   ds.Y             = StatePrimitiveFromConservative_fwd(gas, s, ds.U, x, dx);
179c6e8c570SJames Wright   return ds;
180c6e8c570SJames Wright }
181c6e8c570SJames Wright 
1822b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, const CeedScalar Y[5], const CeedScalar x[3]) {
183dc805cc4SLeila Ghaffari   State s;
184dc805cc4SLeila Ghaffari   s.Y.pressure    = Y[0];
185dc805cc4SLeila Ghaffari   s.Y.velocity[0] = Y[1];
186dc805cc4SLeila Ghaffari   s.Y.velocity[1] = Y[2];
187dc805cc4SLeila Ghaffari   s.Y.velocity[2] = Y[3];
188dc805cc4SLeila Ghaffari   s.Y.temperature = Y[4];
189dc805cc4SLeila Ghaffari   s.U             = StateConservativeFromPrimitive(gas, s.Y, x);
190dc805cc4SLeila Ghaffari   return s;
191dc805cc4SLeila Ghaffari }
192dc805cc4SLeila Ghaffari 
1932b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dY[5], const CeedScalar x[3],
1942b730f8bSJeremy L Thompson                                            const CeedScalar dx[3]) {
195dc805cc4SLeila Ghaffari   State ds;
196dc805cc4SLeila Ghaffari   ds.Y.pressure    = dY[0];
197dc805cc4SLeila Ghaffari   ds.Y.velocity[0] = dY[1];
198dc805cc4SLeila Ghaffari   ds.Y.velocity[1] = dY[2];
199dc805cc4SLeila Ghaffari   ds.Y.velocity[2] = dY[3];
200dc805cc4SLeila Ghaffari   ds.Y.temperature = dY[4];
201dc805cc4SLeila Ghaffari   ds.U             = StateConservativeFromPrimitive_fwd(gas, s, ds.Y, x, dx);
202dc805cc4SLeila Ghaffari   return ds;
203dc805cc4SLeila Ghaffari }
204dc805cc4SLeila Ghaffari 
205cea0a271SJames Wright // Function pointer types for State struct -> generic state array
2062b730f8bSJeremy L Thompson typedef void (*StateToQi_t)(NewtonianIdealGasContext gas, const State input, CeedScalar qi[5]);
207cea0a271SJames Wright 
2082b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void StateToU(NewtonianIdealGasContext gas, const State input, CeedScalar U[5]) { UnpackState_U(input.U, U); }
209cea0a271SJames Wright 
2102b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void StateToY(NewtonianIdealGasContext gas, const State input, CeedScalar Y[5]) { UnpackState_Y(input.Y, Y); }
211cea0a271SJames Wright 
2122b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, StateConservative Flux[3]) {
213c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
214c6e8c570SJames Wright     Flux[i].density = s.U.momentum[i];
2152b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j] + s.Y.pressure * (i == j);
216c6e8c570SJames Wright     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
217c6e8c570SJames Wright   }
218c6e8c570SJames Wright }
219c6e8c570SJames Wright 
2202b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, State s, State ds, StateConservative dFlux[3]) {
221f0a19222SJames Wright   for (CeedInt i = 0; i < 3; i++) {
222f0a19222SJames Wright     dFlux[i].density = ds.U.momentum[i];
2232b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
2242b730f8bSJeremy L Thompson       dFlux[i].momentum[j] = ds.U.momentum[i] * s.Y.velocity[j] + s.U.momentum[i] * ds.Y.velocity[j] + ds.Y.pressure * (i == j);
2252b730f8bSJeremy L Thompson     }
2262b730f8bSJeremy L Thompson     dFlux[i].E_total = (ds.U.E_total + ds.Y.pressure) * s.Y.velocity[i] + (s.U.E_total + s.Y.pressure) * ds.Y.velocity[i];
227f0a19222SJames Wright   }
228f0a19222SJames Wright }
229f0a19222SJames Wright 
2302b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal(NewtonianIdealGasContext gas, State s, const CeedScalar normal[3]) {
231738af36cSAdelekeBankole   StateConservative Flux[3], Flux_dot_n = {0};
232738af36cSAdelekeBankole   FluxInviscid(gas, s, Flux);
233738af36cSAdelekeBankole   for (CeedInt i = 0; i < 3; i++) {
234738af36cSAdelekeBankole     Flux_dot_n.density += Flux[i].density * normal[i];
2352b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += Flux[i].momentum[j] * normal[i];
236738af36cSAdelekeBankole     Flux_dot_n.E_total += Flux[i].E_total * normal[i];
237738af36cSAdelekeBankole   }
238738af36cSAdelekeBankole   return Flux_dot_n;
239738af36cSAdelekeBankole }
240738af36cSAdelekeBankole 
2412b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal_fwd(NewtonianIdealGasContext gas, State s, State ds, const CeedScalar normal[3]) {
242f0a19222SJames Wright   StateConservative dFlux[3], Flux_dot_n = {0};
243f0a19222SJames Wright   FluxInviscid_fwd(gas, s, ds, dFlux);
244c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
245f0a19222SJames Wright     Flux_dot_n.density += dFlux[i].density * normal[i];
2462b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += dFlux[i].momentum[j] * normal[i];
247f0a19222SJames Wright     Flux_dot_n.E_total += dFlux[i].E_total * normal[i];
248c6e8c570SJames Wright   }
249f0a19222SJames Wright   return Flux_dot_n;
250c6e8c570SJames Wright }
251c6e8c570SJames Wright 
2522b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, State s, State ds[3], CeedScalar strong_conv[5]) {
2532b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 5; i++) strong_conv[i] = 0;
2542b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 3; i++) {
2552b89d87eSLeila Ghaffari     StateConservative dF[3];
2562b89d87eSLeila Ghaffari     FluxInviscid_fwd(gas, s, ds[i], dF);
2572b89d87eSLeila Ghaffari     CeedScalar dF_i[5];
2582b89d87eSLeila Ghaffari     UnpackState_U(dF[i], dF_i);
2592b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) strong_conv[j] += dF_i[j];
2602b89d87eSLeila Ghaffari   }
2612b89d87eSLeila Ghaffari }
2622b89d87eSLeila Ghaffari 
2632b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3], CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) {
2642b89d87eSLeila Ghaffari   for (CeedInt j = 0; j < 3; j++) {
2652b89d87eSLeila Ghaffari     Flux[0][j] = F_inviscid[j].density;
2662b730f8bSJeremy L Thompson     for (CeedInt k = 0; k < 3; k++) Flux[k + 1][j] = F_inviscid[j].momentum[k] - stress[k][j];
2672b89d87eSLeila Ghaffari     Flux[4][j] = F_inviscid[j].E_total + Fe[j];
2682b89d87eSLeila Ghaffari   }
2692b89d87eSLeila Ghaffari }
2702b89d87eSLeila Ghaffari 
2712b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal_Boundary(const StateConservative F_inviscid[3], const CeedScalar stress[3][3], const CeedScalar Fe[3],
2722b730f8bSJeremy L Thompson                                               const CeedScalar normal[3], CeedScalar Flux[5]) {
2735bce47c7SJames Wright   for (CeedInt j = 0; j < 5; j++) Flux[j] = 0.;
2745bce47c7SJames Wright   for (CeedInt j = 0; j < 3; j++) {
2755bce47c7SJames Wright     Flux[0] += F_inviscid[j].density * normal[j];
2765bce47c7SJames Wright     for (CeedInt k = 0; k < 3; k++) {
2775bce47c7SJames Wright       Flux[k + 1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j];
2785bce47c7SJames Wright     }
2795bce47c7SJames Wright     Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j];
2805bce47c7SJames Wright   }
2815bce47c7SJames Wright }
2825bce47c7SJames Wright 
283c6e8c570SJames Wright // Kelvin-Mandel notation
2842b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void KMStrainRate(const State grad_s[3], CeedScalar strain_rate[6]) {
285c6e8c570SJames Wright   const CeedScalar weight = 1 / sqrt(2.);
286c6e8c570SJames Wright   strain_rate[0]          = grad_s[0].Y.velocity[0];
287c6e8c570SJames Wright   strain_rate[1]          = grad_s[1].Y.velocity[1];
288c6e8c570SJames Wright   strain_rate[2]          = grad_s[2].Y.velocity[2];
289c6e8c570SJames Wright   strain_rate[3]          = weight * (grad_s[2].Y.velocity[1] + grad_s[1].Y.velocity[2]);
290c6e8c570SJames Wright   strain_rate[4]          = weight * (grad_s[2].Y.velocity[0] + grad_s[0].Y.velocity[2]);
291c6e8c570SJames Wright   strain_rate[5]          = weight * (grad_s[1].Y.velocity[0] + grad_s[0].Y.velocity[1]);
292c6e8c570SJames Wright }
293c6e8c570SJames Wright 
2942b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, const CeedScalar strain_rate[6], CeedScalar stress[6]) {
295c6e8c570SJames Wright   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
296c6e8c570SJames Wright   for (CeedInt i = 0; i < 6; i++) {
297c6e8c570SJames Wright     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
298c6e8c570SJames Wright   }
299c6e8c570SJames Wright }
300c6e8c570SJames Wright 
3012b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
302c6e8c570SJames Wright                                              CeedScalar Fe[3]) {
303c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
3042b730f8bSJeremy L Thompson     Fe[i] = -Y.velocity[0] * stress[0][i] - Y.velocity[1] * stress[1][i] - Y.velocity[2] * stress[2][i] - gas->k * grad_s[i].Y.temperature;
305c6e8c570SJames Wright   }
306c6e8c570SJames Wright }
307c6e8c570SJames Wright 
3082b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
3092b730f8bSJeremy L Thompson                                                  const CeedScalar stress[3][3], const CeedScalar dstress[3][3], CeedScalar dFe[3]) {
310c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
3112b730f8bSJeremy L Thompson     dFe[i] = -Y.velocity[0] * dstress[0][i] - dY.velocity[0] * stress[0][i] - Y.velocity[1] * dstress[1][i] - dY.velocity[1] * stress[1][i] -
3122b730f8bSJeremy L Thompson              Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] - gas->k * grad_ds[i].Y.temperature;
313c6e8c570SJames Wright   }
314c6e8c570SJames Wright }
315c6e8c570SJames Wright 
316c6e8c570SJames Wright #endif  // newtonian_state_h
317