xref: /honee/qfunctions/newtonian_state.h (revision c62f7daefe61de3980c453d85bf9514ff7532a27)
1dc936754SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2475b2820SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3475b2820SJames Wright //
4475b2820SJames Wright // SPDX-License-Identifier: BSD-2-Clause
5475b2820SJames Wright //
6475b2820SJames Wright // This file is part of CEED:  http://github.com/ceed
7475b2820SJames Wright 
8475b2820SJames Wright /// @file
9475b2820SJames Wright /// Structs and helper functions regarding the state of a newtonian simulation
10c7ece6efSJeremy L Thompson #pragma once
11475b2820SJames Wright 
12475b2820SJames Wright #include <ceed.h>
13d0cce58aSJeremy L Thompson #include <math.h>
142b916ea7SJeremy L Thompson 
15475b2820SJames Wright #include "newtonian_types.h"
16704b8bbeSJames Wright #include "utils.h"
17475b2820SJames Wright 
18475b2820SJames Wright typedef struct {
19475b2820SJames Wright   CeedScalar density;
20475b2820SJames Wright   CeedScalar momentum[3];
21475b2820SJames Wright   CeedScalar E_total;
22475b2820SJames Wright } StateConservative;
23475b2820SJames Wright 
24475b2820SJames Wright typedef struct {
25475b2820SJames Wright   StateConservative U;
26475b2820SJames Wright   StatePrimitive    Y;
27*c62f7daeSJames Wright   StateEntropy      V;
28475b2820SJames Wright } State;
29475b2820SJames Wright 
30d1b9ef12SLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_U(StateConservative s, CeedScalar U[5]) {
31d1b9ef12SLeila Ghaffari   U[0] = s.density;
32d1b9ef12SLeila Ghaffari   for (int i = 0; i < 3; i++) U[i + 1] = s.momentum[i];
33d1b9ef12SLeila Ghaffari   U[4] = s.E_total;
34d1b9ef12SLeila Ghaffari }
35d1b9ef12SLeila Ghaffari 
36d1b9ef12SLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) {
37d1b9ef12SLeila Ghaffari   Y[0] = s.pressure;
38d1b9ef12SLeila Ghaffari   for (int i = 0; i < 3; i++) Y[i + 1] = s.velocity[i];
39d1b9ef12SLeila Ghaffari   Y[4] = s.temperature;
40d1b9ef12SLeila Ghaffari }
41d1b9ef12SLeila Ghaffari 
42*c62f7daeSJames Wright CEED_QFUNCTION_HELPER void UnpackState_V(StateEntropy s, CeedScalar V[5]) {
43*c62f7daeSJames Wright   V[0] = s.S_density;
44*c62f7daeSJames Wright   for (int i = 0; i < 3; i++) V[i + 1] = s.S_momentum[i];
45*c62f7daeSJames Wright   V[4] = s.S_energy;
46*c62f7daeSJames Wright }
47*c62f7daeSJames Wright 
482b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio(NewtonianIdealGasContext gas) { return gas->cp / gas->cv; }
49e0d1a4dfSLeila Ghaffari 
502b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar GasConstant(NewtonianIdealGasContext gas) { return gas->cp - gas->cv; }
51e0d1a4dfSLeila Ghaffari 
522b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) { return gas->cp * gas->mu / gas->k; }
53e0d1a4dfSLeila Ghaffari 
542b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas, CeedScalar T) { return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T); }
55e0d1a4dfSLeila Ghaffari 
562b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas, CeedScalar T, CeedScalar u) { return u / SoundSpeed(gas, T); }
57e0d1a4dfSLeila Ghaffari 
582b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy(NewtonianIdealGasContext gas, const State s) {
59c50f56e5SJames Wright   // Ignoring potential energy
60c50f56e5SJames Wright   CeedScalar e_internal = gas->cv * s.Y.temperature;
61c50f56e5SJames Wright   CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
62c50f56e5SJames Wright   return e_internal + e_kinetic + s.Y.pressure / s.U.density;
63c50f56e5SJames Wright }
64c50f56e5SJames Wright 
652b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy_fwd(NewtonianIdealGasContext gas, const State s, const State ds) {
66c50f56e5SJames Wright   // Ignoring potential energy
67c50f56e5SJames Wright   CeedScalar de_kinetic  = Dot3(ds.Y.velocity, s.Y.velocity);
68c50f56e5SJames Wright   CeedScalar de_internal = gas->cv * ds.Y.temperature;
692b916ea7SJeremy L Thompson   return de_internal + de_kinetic + ds.Y.pressure / s.U.density - s.Y.pressure / Square(s.U.density) * ds.U.density;
70c50f56e5SJames Wright }
71c50f56e5SJames Wright 
72edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(NewtonianIdealGasContext gas, StateConservative U) {
73475b2820SJames Wright   StatePrimitive Y;
74475b2820SJames Wright   for (CeedInt i = 0; i < 3; i++) Y.velocity[i] = U.momentum[i] / U.density;
75475b2820SJames Wright   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
76475b2820SJames Wright   CeedScalar e_total    = U.E_total / U.density;
77edcfef1bSKenneth E. Jansen   CeedScalar e_internal = e_total - e_kinetic;
78475b2820SJames Wright   Y.temperature         = e_internal / gas->cv;
79e0d1a4dfSLeila Ghaffari   Y.pressure            = (HeatCapacityRatio(gas) - 1) * U.density * e_internal;
80475b2820SJames Wright   return Y;
81475b2820SJames Wright }
82475b2820SJames Wright 
83edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) {
84475b2820SJames Wright   StatePrimitive dY;
85475b2820SJames Wright   for (CeedInt i = 0; i < 3; i++) {
86475b2820SJames Wright     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
87475b2820SJames Wright   }
88475b2820SJames Wright   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
89475b2820SJames Wright   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
90475b2820SJames Wright   CeedScalar e_total     = s.U.E_total / s.U.density;
91475b2820SJames Wright   CeedScalar de_total    = (dU.E_total - e_total * dU.density) / s.U.density;
92edcfef1bSKenneth E. Jansen   CeedScalar e_internal  = e_total - e_kinetic;
93edcfef1bSKenneth E. Jansen   CeedScalar de_internal = de_total - de_kinetic;
94475b2820SJames Wright   dY.temperature         = de_internal / gas->cv;
952b916ea7SJeremy L Thompson   dY.pressure            = (HeatCapacityRatio(gas) - 1) * (dU.density * e_internal + s.U.density * de_internal);
96475b2820SJames Wright   return dY;
97475b2820SJames Wright }
98475b2820SJames Wright 
99edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
100cbe60e31SLeila Ghaffari   StateConservative U;
101e0d1a4dfSLeila Ghaffari   U.density = Y.pressure / (GasConstant(gas) * Y.temperature);
102cbe60e31SLeila Ghaffari   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * Y.velocity[i];
103cbe60e31SLeila Ghaffari   CeedScalar e_internal = gas->cv * Y.temperature;
104cbe60e31SLeila Ghaffari   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
105edcfef1bSKenneth E. Jansen   CeedScalar e_total    = e_internal + e_kinetic;
106cbe60e31SLeila Ghaffari   U.E_total             = U.density * e_total;
107cbe60e31SLeila Ghaffari   return U;
108cbe60e31SLeila Ghaffari }
109cbe60e31SLeila Ghaffari 
110edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
111cbe60e31SLeila Ghaffari   StateConservative dU;
1122b916ea7SJeremy L Thompson   dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
113cbe60e31SLeila Ghaffari   for (int i = 0; i < 3; i++) {
114cbe60e31SLeila Ghaffari     dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i];
115cbe60e31SLeila Ghaffari   }
116cbe60e31SLeila Ghaffari   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
117cbe60e31SLeila Ghaffari   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
118cbe60e31SLeila Ghaffari   CeedScalar e_internal  = gas->cv * s.Y.temperature;
119cbe60e31SLeila Ghaffari   CeedScalar de_internal = gas->cv * dY.temperature;
120edcfef1bSKenneth E. Jansen   CeedScalar e_total     = e_internal + e_kinetic;
121edcfef1bSKenneth E. Jansen   CeedScalar de_total    = de_internal + de_kinetic;
122cbe60e31SLeila Ghaffari   dU.E_total             = dU.density * e_total + s.U.density * de_total;
123cbe60e31SLeila Ghaffari   return dU;
124cbe60e31SLeila Ghaffari }
125cbe60e31SLeila Ghaffari 
126*c62f7daeSJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromConservative(NewtonianIdealGasContext gas, StateConservative U) {
127*c62f7daeSJames Wright   StateEntropy     V;
128*c62f7daeSJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
129*c62f7daeSJames Wright   const CeedScalar e_kinetic  = .5 * Dot3(U.momentum, U.momentum) / U.density;
130*c62f7daeSJames Wright   const CeedScalar e_internal = U.E_total - e_kinetic;
131*c62f7daeSJames Wright   const CeedScalar p          = (gamma - 1) * e_internal;
132*c62f7daeSJames Wright   const CeedScalar entropy    = log(p) - gamma * log(U.density);
133*c62f7daeSJames Wright 
134*c62f7daeSJames Wright   V.S_density = (gamma - entropy) / (gamma - 1) - e_kinetic / p;
135*c62f7daeSJames Wright   for (int i = 0; i < 3; i++) V.S_momentum[i] = U.momentum[i] / p;
136*c62f7daeSJames Wright   V.S_energy = -U.density / p;
137*c62f7daeSJames Wright   return V;
138*c62f7daeSJames Wright }
139*c62f7daeSJames Wright 
140*c62f7daeSJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) {
141*c62f7daeSJames Wright   StateEntropy     dV;
142*c62f7daeSJames Wright   const CeedScalar gamma       = HeatCapacityRatio(gas);
143*c62f7daeSJames Wright   const CeedScalar e_kinetic   = .5 * Dot3(s.U.momentum, s.U.momentum) / s.U.density;
144*c62f7daeSJames Wright   const CeedScalar de_kinetic  = (Dot3(s.U.momentum, dU.momentum) - e_kinetic * dU.density) / s.U.density;
145*c62f7daeSJames Wright   const CeedScalar de_internal = dU.E_total - de_kinetic;
146*c62f7daeSJames Wright   const CeedScalar p           = s.Y.pressure;
147*c62f7daeSJames Wright   const CeedScalar dp          = (gamma - 1) * de_internal;
148*c62f7daeSJames Wright 
149*c62f7daeSJames Wright   CeedScalar dentropy = dp / p - gamma * dU.density / s.U.density;
150*c62f7daeSJames Wright 
151*c62f7daeSJames Wright   dV.S_density = -dentropy / (gamma - 1) - de_kinetic / p + dp * e_kinetic / Square(p);
152*c62f7daeSJames Wright   for (CeedInt i = 0; i < 3; i++) {
153*c62f7daeSJames Wright     dV.S_momentum[i] = (dU.momentum[i] - s.U.momentum[i] * dp / p) / p;
154*c62f7daeSJames Wright   }
155*c62f7daeSJames Wright   dV.S_energy = -(dU.density - s.U.density * dp / p) / p;
156*c62f7daeSJames Wright   return dV;
157*c62f7daeSJames Wright }
158*c62f7daeSJames Wright 
159*c62f7daeSJames Wright CEED_QFUNCTION_HELPER StateConservative StateConservativeFromEntropy(NewtonianIdealGasContext gas, StateEntropy V) {
160*c62f7daeSJames Wright   StateConservative U;
161*c62f7daeSJames Wright   CeedScalar        velocity[3];
162*c62f7daeSJames Wright   for (int i = 0; i < 3; i++) velocity[i] = -V.S_momentum[i] / V.S_energy;
163*c62f7daeSJames Wright   const CeedScalar gamma     = HeatCapacityRatio(gas);
164*c62f7daeSJames Wright   const CeedScalar e_kinetic = 0.5 * Dot3(velocity, velocity);
165*c62f7daeSJames Wright   const CeedScalar entropy   = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
166*c62f7daeSJames Wright   const CeedScalar log_rho   = -(entropy + log(-V.S_energy)) / (gamma - 1);
167*c62f7daeSJames Wright   U.density                  = exp(log_rho);
168*c62f7daeSJames Wright   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * velocity[i];
169*c62f7daeSJames Wright 
170*c62f7daeSJames Wright   const CeedScalar e_internal = -gas->cv / (GasConstant(gas) * V.S_energy);
171*c62f7daeSJames Wright   U.E_total                   = U.density * (e_internal + e_kinetic);
172*c62f7daeSJames Wright   return U;
173*c62f7daeSJames Wright }
174*c62f7daeSJames Wright 
175*c62f7daeSJames Wright CEED_QFUNCTION_HELPER StateConservative StateConservativeFromEntropy_fwd(NewtonianIdealGasContext gas, State s, StateEntropy dV) {
176*c62f7daeSJames Wright   StateConservative dU;
177*c62f7daeSJames Wright   CeedScalar        dvelocity[3];
178*c62f7daeSJames Wright   for (int i = 0; i < 3; i++) dvelocity[i] = (-dV.S_momentum[i] - s.Y.velocity[i] * dV.S_energy) / s.V.S_energy;
179*c62f7daeSJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
180*c62f7daeSJames Wright   const CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
181*c62f7daeSJames Wright   const CeedScalar de_kinetic = Dot3(dvelocity, s.Y.velocity);
182*c62f7daeSJames Wright   const CeedScalar entropy    = gamma - (gamma - 1) * (s.V.S_density - e_kinetic * s.V.S_energy);
183*c62f7daeSJames Wright   const CeedScalar dentropy   = -(gamma - 1) * (dV.S_density - (de_kinetic * s.V.S_energy + e_kinetic * dV.S_energy));
184*c62f7daeSJames Wright   const CeedScalar log_rho    = -(entropy + log(-s.V.S_energy)) / (gamma - 1);
185*c62f7daeSJames Wright   const CeedScalar rho        = exp(log_rho);
186*c62f7daeSJames Wright   dU.density                  = -rho / (gamma - 1) * (dentropy + dV.S_energy / s.V.S_energy);
187*c62f7daeSJames Wright   for (int i = 0; i < 3; i++) dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dvelocity[i];
188*c62f7daeSJames Wright 
189*c62f7daeSJames Wright   const CeedScalar e_internal  = -gas->cv / (GasConstant(gas) * s.V.S_energy);
190*c62f7daeSJames Wright   const CeedScalar de_internal = gas->cv * dV.S_energy / (GasConstant(gas) * s.V.S_energy * s.V.S_energy);
191*c62f7daeSJames Wright   const CeedScalar e_total     = e_internal + e_kinetic;
192*c62f7daeSJames Wright   dU.E_total                   = dU.density * e_total + s.U.density * (de_internal + de_kinetic);
193*c62f7daeSJames Wright   return dU;
194*c62f7daeSJames Wright }
195*c62f7daeSJames Wright 
196*c62f7daeSJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
197*c62f7daeSJames Wright   StateEntropy     V;
198*c62f7daeSJames Wright   const CeedScalar gamma     = HeatCapacityRatio(gas);
199*c62f7daeSJames Wright   const CeedScalar rho       = Y.pressure / (GasConstant(gas) * Y.temperature);
200*c62f7daeSJames Wright   const CeedScalar entropy   = log(Y.pressure) - gamma * log(rho);
201*c62f7daeSJames Wright   const CeedScalar rho_div_p = rho / Y.pressure;
202*c62f7daeSJames Wright   const CeedScalar e_kinetic = 0.5 * Dot3(Y.velocity, Y.velocity);
203*c62f7daeSJames Wright 
204*c62f7daeSJames Wright   V.S_density = (gamma - entropy) / (gamma - 1) - rho_div_p * e_kinetic;
205*c62f7daeSJames Wright   for (int i = 0; i < 3; i++) V.S_momentum[i] = rho_div_p * Y.velocity[i];
206*c62f7daeSJames Wright   V.S_energy = -rho_div_p;
207*c62f7daeSJames Wright   return V;
208*c62f7daeSJames Wright }
209*c62f7daeSJames Wright 
210*c62f7daeSJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
211*c62f7daeSJames Wright   StateEntropy     dV;
212*c62f7daeSJames Wright   const CeedScalar gamma = HeatCapacityRatio(gas);
213*c62f7daeSJames Wright   CeedScalar       drho  = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
214*c62f7daeSJames Wright 
215*c62f7daeSJames Wright   const CeedScalar e_kinetic  = .5 * Dot3(s.Y.velocity, s.Y.velocity);
216*c62f7daeSJames Wright   const CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
217*c62f7daeSJames Wright   const CeedScalar rho_div_p  = s.U.density / s.Y.pressure;
218*c62f7daeSJames Wright   const CeedScalar drho_div_p = (drho * s.Y.pressure - s.U.density * dY.pressure) / Square(s.Y.pressure);
219*c62f7daeSJames Wright 
220*c62f7daeSJames Wright   CeedScalar dentropy = dY.pressure / s.Y.pressure - gamma * drho / s.U.density;
221*c62f7daeSJames Wright 
222*c62f7daeSJames Wright   dV.S_density = -dentropy / (gamma - 1) - de_kinetic * rho_div_p - e_kinetic * drho_div_p;
223*c62f7daeSJames Wright   for (CeedInt i = 0; i < 3; i++) dV.S_momentum[i] = rho_div_p * dY.velocity[i] + drho_div_p * s.Y.velocity[i];
224*c62f7daeSJames Wright   dV.S_energy = -drho_div_p;
225*c62f7daeSJames Wright   return dV;
226*c62f7daeSJames Wright }
227*c62f7daeSJames Wright 
228*c62f7daeSJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromEntropy(NewtonianIdealGasContext gas, StateEntropy V) {
229*c62f7daeSJames Wright   StatePrimitive Y;
230*c62f7daeSJames Wright   for (int i = 0; i < 3; i++) Y.velocity[i] = -V.S_momentum[i] / V.S_energy;
231*c62f7daeSJames Wright   Y.temperature              = -1 / (GasConstant(gas) * V.S_energy);
232*c62f7daeSJames Wright   const CeedScalar gamma     = HeatCapacityRatio(gas);
233*c62f7daeSJames Wright   const CeedScalar e_kinetic = 0.5 * Dot3(Y.velocity, Y.velocity);
234*c62f7daeSJames Wright   const CeedScalar entropy   = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
235*c62f7daeSJames Wright   const CeedScalar log_P     = -(entropy + gamma * log(-V.S_energy)) / (gamma - 1);
236*c62f7daeSJames Wright   Y.pressure                 = exp(log_P);
237*c62f7daeSJames Wright   return Y;
238*c62f7daeSJames Wright }
239*c62f7daeSJames Wright 
240*c62f7daeSJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromEntropy_fwd(NewtonianIdealGasContext gas, State s, StateEntropy dV) {
241*c62f7daeSJames Wright   StatePrimitive dY;
242*c62f7daeSJames Wright   for (int i = 0; i < 3; i++) dY.velocity[i] = -(dV.S_momentum[i] - s.V.S_momentum[i] * dV.S_energy / s.V.S_energy) / s.V.S_energy;
243*c62f7daeSJames Wright   dY.temperature              = dV.S_energy / (GasConstant(gas) * s.V.S_energy * s.V.S_energy);
244*c62f7daeSJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
245*c62f7daeSJames Wright   const CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
246*c62f7daeSJames Wright   const CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
247*c62f7daeSJames Wright   const CeedScalar dentropy   = (1 - gamma) * (dV.S_density - e_kinetic * dV.S_energy - de_kinetic * s.V.S_energy);
248*c62f7daeSJames Wright   dY.pressure                 = s.Y.pressure * (-dentropy - gamma * dV.S_energy / s.V.S_energy) / (gamma - 1);
249*c62f7daeSJames Wright   return dY;
250*c62f7daeSJames Wright }
251*c62f7daeSJames Wright 
252edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
253edcfef1bSKenneth E. Jansen   StateConservative U = StateConservativeFromPrimitive(gas, Y);
254*c62f7daeSJames Wright   StateEntropy      V = StateEntropyFromPrimitive(gas, Y);
255b8fb7609SAdeleke O. Bankole   State             s;
256b8fb7609SAdeleke O. Bankole   s.U = U;
257b8fb7609SAdeleke O. Bankole   s.Y = Y;
258*c62f7daeSJames Wright   s.V = V;
259b8fb7609SAdeleke O. Bankole   return s;
260b8fb7609SAdeleke O. Bankole }
261b8fb7609SAdeleke O. Bankole 
262edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
263edcfef1bSKenneth E. Jansen   StateConservative dU = StateConservativeFromPrimitive_fwd(gas, s, dY);
264*c62f7daeSJames Wright   StateEntropy      dV = StateEntropyFromPrimitive_fwd(gas, s, dY);
265c8c30d87SJed Brown   State             ds;
266c8c30d87SJed Brown   ds.U = dU;
267c8c30d87SJed Brown   ds.Y = dY;
268*c62f7daeSJames Wright   ds.V = dV;
269c8c30d87SJed Brown   return ds;
270c8c30d87SJed Brown }
271c8c30d87SJed Brown 
272c2c93676SJed Brown // linear combination of n states
273c2c93676SJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeMult(CeedInt n, const CeedScalar a[], const StateConservative X[]) {
274c2c93676SJed Brown   StateConservative R = {0};
275c2c93676SJed Brown   for (CeedInt i = 0; i < n; i++) {
276c2c93676SJed Brown     R.density += a[i] * X[i].density;
277c2c93676SJed Brown     for (int j = 0; j < 3; j++) R.momentum[j] += a[i] * X[i].momentum[j];
278c2c93676SJed Brown     R.E_total += a[i] * X[i].E_total;
279c2c93676SJed Brown   }
280dbb62a19SJed Brown   return R;
281dbb62a19SJed Brown }
282dbb62a19SJed Brown 
283dbb62a19SJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeAXPBYPCZ(CeedScalar a, StateConservative X, CeedScalar b, StateConservative Y, CeedScalar c,
284dbb62a19SJed Brown                                                                   StateConservative Z) {
285dbb62a19SJed Brown   StateConservative R;
286dbb62a19SJed Brown   R.density = a * X.density + b * Y.density + c * Z.density;
287dbb62a19SJed Brown   for (int i = 0; i < 3; i++) R.momentum[i] = a * X.momentum[i] + b * Y.momentum[i] + c * Z.momentum[i];
288dbb62a19SJed Brown   R.E_total = a * X.E_total + b * Y.E_total + c * Z.E_total;
289dbb62a19SJed Brown   return R;
290dbb62a19SJed Brown }
291dbb62a19SJed Brown 
292b8fb7609SAdeleke O. Bankole CEED_QFUNCTION_HELPER void StateToU(NewtonianIdealGasContext gas, const State input, CeedScalar U[5]) { UnpackState_U(input.U, U); }
293b8fb7609SAdeleke O. Bankole 
294b8fb7609SAdeleke O. Bankole CEED_QFUNCTION_HELPER void StateToY(NewtonianIdealGasContext gas, const State input, CeedScalar Y[5]) { UnpackState_Y(input.Y, Y); }
295a8bca8acSJames Wright 
296*c62f7daeSJames Wright CEED_QFUNCTION_HELPER void StateToV(NewtonianIdealGasContext gas, const State input, CeedScalar V[5]) { UnpackState_V(input.V, V); }
297*c62f7daeSJames Wright 
2988fff8293SJames Wright CEED_QFUNCTION_HELPER void StateToQ(NewtonianIdealGasContext gas, const State input, CeedScalar Q[5], StateVariable state_var) {
2998fff8293SJames Wright   switch (state_var) {
3008fff8293SJames Wright     case STATEVAR_CONSERVATIVE:
3018fff8293SJames Wright       StateToU(gas, input, Q);
3028fff8293SJames Wright       break;
3038fff8293SJames Wright     case STATEVAR_PRIMITIVE:
3048fff8293SJames Wright       StateToY(gas, input, Q);
3058fff8293SJames Wright       break;
306*c62f7daeSJames Wright     case STATEVAR_ENTROPY:
307*c62f7daeSJames Wright       StateToV(gas, input, Q);
308*c62f7daeSJames Wright       break;
3098fff8293SJames Wright   }
3108fff8293SJames Wright }
311d4559bbeSJames Wright 
312edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, const CeedScalar U[5]) {
313475b2820SJames Wright   State s;
314475b2820SJames Wright   s.U.density     = U[0];
315475b2820SJames Wright   s.U.momentum[0] = U[1];
316475b2820SJames Wright   s.U.momentum[1] = U[2];
317475b2820SJames Wright   s.U.momentum[2] = U[3];
318475b2820SJames Wright   s.U.E_total     = U[4];
319edcfef1bSKenneth E. Jansen   s.Y             = StatePrimitiveFromConservative(gas, s.U);
320*c62f7daeSJames Wright   s.V             = StateEntropyFromConservative(gas, s.U);
321475b2820SJames Wright   return s;
322475b2820SJames Wright }
323475b2820SJames Wright 
324edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dU[5]) {
325475b2820SJames Wright   State ds;
326475b2820SJames Wright   ds.U.density     = dU[0];
327475b2820SJames Wright   ds.U.momentum[0] = dU[1];
328475b2820SJames Wright   ds.U.momentum[1] = dU[2];
329475b2820SJames Wright   ds.U.momentum[2] = dU[3];
330475b2820SJames Wright   ds.U.E_total     = dU[4];
331edcfef1bSKenneth E. Jansen   ds.Y             = StatePrimitiveFromConservative_fwd(gas, s, ds.U);
332*c62f7daeSJames Wright   ds.V             = StateEntropyFromConservative_fwd(gas, s, ds.U);
333475b2820SJames Wright   return ds;
334475b2820SJames Wright }
335475b2820SJames Wright 
336edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, const CeedScalar Y[5]) {
337cbe60e31SLeila Ghaffari   State s;
338cbe60e31SLeila Ghaffari   s.Y.pressure    = Y[0];
339cbe60e31SLeila Ghaffari   s.Y.velocity[0] = Y[1];
340cbe60e31SLeila Ghaffari   s.Y.velocity[1] = Y[2];
341cbe60e31SLeila Ghaffari   s.Y.velocity[2] = Y[3];
342cbe60e31SLeila Ghaffari   s.Y.temperature = Y[4];
343edcfef1bSKenneth E. Jansen   s.U             = StateConservativeFromPrimitive(gas, s.Y);
344*c62f7daeSJames Wright   s.V             = StateEntropyFromPrimitive(gas, s.Y);
345cbe60e31SLeila Ghaffari   return s;
346cbe60e31SLeila Ghaffari }
347cbe60e31SLeila Ghaffari 
348edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dY[5]) {
349cbe60e31SLeila Ghaffari   State ds;
350cbe60e31SLeila Ghaffari   ds.Y.pressure    = dY[0];
351cbe60e31SLeila Ghaffari   ds.Y.velocity[0] = dY[1];
352cbe60e31SLeila Ghaffari   ds.Y.velocity[1] = dY[2];
353cbe60e31SLeila Ghaffari   ds.Y.velocity[2] = dY[3];
354cbe60e31SLeila Ghaffari   ds.Y.temperature = dY[4];
355edcfef1bSKenneth E. Jansen   ds.U             = StateConservativeFromPrimitive_fwd(gas, s, ds.Y);
356*c62f7daeSJames Wright   ds.V             = StateEntropyFromPrimitive_fwd(gas, s, ds.Y);
357*c62f7daeSJames Wright   return ds;
358*c62f7daeSJames Wright }
359*c62f7daeSJames Wright 
360*c62f7daeSJames Wright CEED_QFUNCTION_HELPER State StateFromV(NewtonianIdealGasContext gas, const CeedScalar V[5]) {
361*c62f7daeSJames Wright   State s;
362*c62f7daeSJames Wright   s.V.S_density     = V[0];
363*c62f7daeSJames Wright   s.V.S_momentum[0] = V[1];
364*c62f7daeSJames Wright   s.V.S_momentum[1] = V[2];
365*c62f7daeSJames Wright   s.V.S_momentum[2] = V[3];
366*c62f7daeSJames Wright   s.V.S_energy      = V[4];
367*c62f7daeSJames Wright   s.U               = StateConservativeFromEntropy(gas, s.V);
368*c62f7daeSJames Wright   s.Y               = StatePrimitiveFromEntropy(gas, s.V);
369*c62f7daeSJames Wright   return s;
370*c62f7daeSJames Wright }
371*c62f7daeSJames Wright 
372*c62f7daeSJames Wright CEED_QFUNCTION_HELPER State StateFromV_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dV[5]) {
373*c62f7daeSJames Wright   State ds;
374*c62f7daeSJames Wright   ds.V.S_density     = dV[0];
375*c62f7daeSJames Wright   ds.V.S_momentum[0] = dV[1];
376*c62f7daeSJames Wright   ds.V.S_momentum[1] = dV[2];
377*c62f7daeSJames Wright   ds.V.S_momentum[2] = dV[3];
378*c62f7daeSJames Wright   ds.V.S_energy      = dV[4];
379*c62f7daeSJames Wright   ds.U               = StateConservativeFromEntropy_fwd(gas, s, ds.V);
380*c62f7daeSJames Wright   ds.Y               = StatePrimitiveFromEntropy_fwd(gas, s, ds.V);
381cbe60e31SLeila Ghaffari   return ds;
382cbe60e31SLeila Ghaffari }
383cbe60e31SLeila Ghaffari 
384edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromQ(NewtonianIdealGasContext gas, const CeedScalar Q[5], StateVariable state_var) {
3858fff8293SJames Wright   State s;
3868fff8293SJames Wright   switch (state_var) {
3878fff8293SJames Wright     case STATEVAR_CONSERVATIVE:
388edcfef1bSKenneth E. Jansen       s = StateFromU(gas, Q);
3898fff8293SJames Wright       break;
3908fff8293SJames Wright     case STATEVAR_PRIMITIVE:
391edcfef1bSKenneth E. Jansen       s = StateFromY(gas, Q);
3928fff8293SJames Wright       break;
393*c62f7daeSJames Wright     case STATEVAR_ENTROPY:
394*c62f7daeSJames Wright       s = StateFromV(gas, Q);
395*c62f7daeSJames Wright       break;
3968fff8293SJames Wright   }
3978fff8293SJames Wright   return s;
3988fff8293SJames Wright }
3998fff8293SJames Wright 
4002e7597b6SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromQ_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dQ[5], StateVariable state_var) {
4018fff8293SJames Wright   State ds;
4028fff8293SJames Wright   switch (state_var) {
4038fff8293SJames Wright     case STATEVAR_CONSERVATIVE:
404edcfef1bSKenneth E. Jansen       ds = StateFromU_fwd(gas, s, dQ);
4058fff8293SJames Wright       break;
4068fff8293SJames Wright     case STATEVAR_PRIMITIVE:
407edcfef1bSKenneth E. Jansen       ds = StateFromY_fwd(gas, s, dQ);
4088fff8293SJames Wright       break;
409*c62f7daeSJames Wright     case STATEVAR_ENTROPY:
410*c62f7daeSJames Wright       ds = StateFromV_fwd(gas, s, dQ);
411*c62f7daeSJames Wright       break;
4128fff8293SJames Wright   }
4138fff8293SJames Wright   return ds;
4148fff8293SJames Wright }
4158fff8293SJames Wright 
4162b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, StateConservative Flux[3]) {
417475b2820SJames Wright   for (CeedInt i = 0; i < 3; i++) {
418475b2820SJames Wright     Flux[i].density = s.U.momentum[i];
4192b916ea7SJeremy 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);
420475b2820SJames Wright     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
421475b2820SJames Wright   }
422475b2820SJames Wright }
423475b2820SJames Wright 
4242b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, State s, State ds, StateConservative dFlux[3]) {
425c50f56e5SJames Wright   for (CeedInt i = 0; i < 3; i++) {
426c50f56e5SJames Wright     dFlux[i].density = ds.U.momentum[i];
4272b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
4282b916ea7SJeremy 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);
4292b916ea7SJeremy L Thompson     }
4302b916ea7SJeremy 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];
431c50f56e5SJames Wright   }
432c50f56e5SJames Wright }
433c50f56e5SJames Wright 
4342b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal(NewtonianIdealGasContext gas, State s, const CeedScalar normal[3]) {
4357b530f2aSAdelekeBankole   StateConservative Flux[3], Flux_dot_n = {0};
4367b530f2aSAdelekeBankole   FluxInviscid(gas, s, Flux);
4377b530f2aSAdelekeBankole   for (CeedInt i = 0; i < 3; i++) {
4387b530f2aSAdelekeBankole     Flux_dot_n.density += Flux[i].density * normal[i];
4392b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += Flux[i].momentum[j] * normal[i];
4407b530f2aSAdelekeBankole     Flux_dot_n.E_total += Flux[i].E_total * normal[i];
4417b530f2aSAdelekeBankole   }
4427b530f2aSAdelekeBankole   return Flux_dot_n;
4437b530f2aSAdelekeBankole }
4447b530f2aSAdelekeBankole 
4452b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal_fwd(NewtonianIdealGasContext gas, State s, State ds, const CeedScalar normal[3]) {
446c50f56e5SJames Wright   StateConservative dFlux[3], Flux_dot_n = {0};
447c50f56e5SJames Wright   FluxInviscid_fwd(gas, s, ds, dFlux);
448475b2820SJames Wright   for (CeedInt i = 0; i < 3; i++) {
449c50f56e5SJames Wright     Flux_dot_n.density += dFlux[i].density * normal[i];
4502b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += dFlux[i].momentum[j] * normal[i];
451c50f56e5SJames Wright     Flux_dot_n.E_total += dFlux[i].E_total * normal[i];
452475b2820SJames Wright   }
453c50f56e5SJames Wright   return Flux_dot_n;
454475b2820SJames Wright }
455475b2820SJames Wright 
4562b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, State s, State ds[3], CeedScalar strong_conv[5]) {
457d1b9ef12SLeila Ghaffari   for (CeedInt i = 0; i < 5; i++) strong_conv[i] = 0;
458d1b9ef12SLeila Ghaffari   for (CeedInt i = 0; i < 3; i++) {
459d1b9ef12SLeila Ghaffari     StateConservative dF[3];
460d1b9ef12SLeila Ghaffari     FluxInviscid_fwd(gas, s, ds[i], dF);
461d1b9ef12SLeila Ghaffari     CeedScalar dF_i[5];
462d1b9ef12SLeila Ghaffari     UnpackState_U(dF[i], dF_i);
4632b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) strong_conv[j] += dF_i[j];
464d1b9ef12SLeila Ghaffari   }
465d1b9ef12SLeila Ghaffari }
466d1b9ef12SLeila Ghaffari 
4672b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3], CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) {
468d1b9ef12SLeila Ghaffari   for (CeedInt j = 0; j < 3; j++) {
469d1b9ef12SLeila Ghaffari     Flux[0][j] = F_inviscid[j].density;
4702b916ea7SJeremy L Thompson     for (CeedInt k = 0; k < 3; k++) Flux[k + 1][j] = F_inviscid[j].momentum[k] - stress[k][j];
471d1b9ef12SLeila Ghaffari     Flux[4][j] = F_inviscid[j].E_total + Fe[j];
472d1b9ef12SLeila Ghaffari   }
473d1b9ef12SLeila Ghaffari }
474d1b9ef12SLeila Ghaffari 
4752b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal_Boundary(const StateConservative F_inviscid[3], const CeedScalar stress[3][3], const CeedScalar Fe[3],
4762b916ea7SJeremy L Thompson                                               const CeedScalar normal[3], CeedScalar Flux[5]) {
477c5740391SJames Wright   for (CeedInt j = 0; j < 5; j++) Flux[j] = 0.;
478c5740391SJames Wright   for (CeedInt j = 0; j < 3; j++) {
479c5740391SJames Wright     Flux[0] += F_inviscid[j].density * normal[j];
480c5740391SJames Wright     for (CeedInt k = 0; k < 3; k++) {
481c5740391SJames Wright       Flux[k + 1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j];
482c5740391SJames Wright     }
483c5740391SJames Wright     Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j];
484c5740391SJames Wright   }
485c5740391SJames Wright }
486c5740391SJames Wright 
487c8c30d87SJed Brown CEED_QFUNCTION_HELPER void FluxTotal_RiemannBoundary(const StateConservative F_inviscid_normal, const CeedScalar stress[3][3], const CeedScalar Fe[3],
488c8c30d87SJed Brown                                                      const CeedScalar normal[3], CeedScalar Flux[5]) {
489c8c30d87SJed Brown   Flux[0] = F_inviscid_normal.density;
490c8c30d87SJed Brown   for (CeedInt k = 0; k < 3; k++) Flux[k + 1] = F_inviscid_normal.momentum[k];
491c8c30d87SJed Brown   Flux[4] = F_inviscid_normal.E_total;
492c8c30d87SJed Brown   for (CeedInt j = 0; j < 3; j++) {
493c8c30d87SJed Brown     for (CeedInt k = 0; k < 3; k++) {
494c8c30d87SJed Brown       Flux[k + 1] -= stress[k][j] * normal[j];
495c8c30d87SJed Brown     }
496c8c30d87SJed Brown     Flux[4] += Fe[j] * normal[j];
497c8c30d87SJed Brown   }
498c8c30d87SJed Brown }
499c8c30d87SJed Brown 
50040a33f2dSJames Wright CEED_QFUNCTION_HELPER void VelocityGradient(const State grad_s[3], CeedScalar grad_velocity[3][3]) {
50140a33f2dSJames Wright   grad_velocity[0][0] = grad_s[0].Y.velocity[0];
50240a33f2dSJames Wright   grad_velocity[0][1] = grad_s[1].Y.velocity[0];
50340a33f2dSJames Wright   grad_velocity[0][2] = grad_s[2].Y.velocity[0];
50440a33f2dSJames Wright   grad_velocity[1][0] = grad_s[0].Y.velocity[1];
50540a33f2dSJames Wright   grad_velocity[1][1] = grad_s[1].Y.velocity[1];
50640a33f2dSJames Wright   grad_velocity[1][2] = grad_s[2].Y.velocity[1];
50740a33f2dSJames Wright   grad_velocity[2][0] = grad_s[0].Y.velocity[2];
50840a33f2dSJames Wright   grad_velocity[2][1] = grad_s[1].Y.velocity[2];
50940a33f2dSJames Wright   grad_velocity[2][2] = grad_s[2].Y.velocity[2];
51040a33f2dSJames Wright }
51140a33f2dSJames Wright 
51240a33f2dSJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const CeedScalar grad_velocity[3][3], CeedScalar strain_rate[6]) {
51340a33f2dSJames Wright   const CeedScalar weight = 1 / sqrt(2.);  // Really sqrt(2.) / 2
51440a33f2dSJames Wright   strain_rate[0]          = grad_velocity[0][0];
51540a33f2dSJames Wright   strain_rate[1]          = grad_velocity[1][1];
51640a33f2dSJames Wright   strain_rate[2]          = grad_velocity[2][2];
51740a33f2dSJames Wright   strain_rate[3]          = weight * (grad_velocity[1][2] + grad_velocity[2][1]);
51840a33f2dSJames Wright   strain_rate[4]          = weight * (grad_velocity[0][2] + grad_velocity[2][0]);
51940a33f2dSJames Wright   strain_rate[5]          = weight * (grad_velocity[0][1] + grad_velocity[1][0]);
52040a33f2dSJames Wright }
52140a33f2dSJames Wright 
522475b2820SJames Wright // Kelvin-Mandel notation
52340a33f2dSJames Wright CEED_QFUNCTION_HELPER void KMStrainRate_State(const State grad_s[3], CeedScalar strain_rate[6]) {
52440a33f2dSJames Wright   CeedScalar grad_velocity[3][3];
52540a33f2dSJames Wright   VelocityGradient(grad_s, grad_velocity);
52640a33f2dSJames Wright   KMStrainRate(grad_velocity, strain_rate);
52740a33f2dSJames Wright }
52840a33f2dSJames Wright 
529f6ac214eSJames Wright //@brief Given velocity gradient du_i/dx_j, return 0.5*(du_i/dx_j - du_j/dx_i)
53040a33f2dSJames Wright CEED_QFUNCTION_HELPER void RotationRate(const CeedScalar grad_velocity[3][3], CeedScalar rotation_rate[3][3]) {
53140a33f2dSJames Wright   rotation_rate[0][0] = 0;
53240a33f2dSJames Wright   rotation_rate[1][1] = 0;
53340a33f2dSJames Wright   rotation_rate[2][2] = 0;
53440a33f2dSJames Wright   rotation_rate[1][2] = 0.5 * (grad_velocity[1][2] - grad_velocity[2][1]);
53540a33f2dSJames Wright   rotation_rate[0][2] = 0.5 * (grad_velocity[0][2] - grad_velocity[2][0]);
53640a33f2dSJames Wright   rotation_rate[0][1] = 0.5 * (grad_velocity[0][1] - grad_velocity[1][0]);
53740a33f2dSJames Wright   rotation_rate[2][1] = -rotation_rate[1][2];
53840a33f2dSJames Wright   rotation_rate[2][0] = -rotation_rate[0][2];
53940a33f2dSJames Wright   rotation_rate[1][0] = -rotation_rate[0][1];
540475b2820SJames Wright }
541475b2820SJames Wright 
5422b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, const CeedScalar strain_rate[6], CeedScalar stress[6]) {
543475b2820SJames Wright   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
544475b2820SJames Wright   for (CeedInt i = 0; i < 6; i++) {
545475b2820SJames Wright     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
546475b2820SJames Wright   }
547475b2820SJames Wright }
548475b2820SJames Wright 
5492b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
550475b2820SJames Wright                                              CeedScalar Fe[3]) {
551475b2820SJames Wright   for (CeedInt i = 0; i < 3; i++) {
5522b916ea7SJeremy 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;
553475b2820SJames Wright   }
554475b2820SJames Wright }
555475b2820SJames Wright 
5562b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
5572b916ea7SJeremy L Thompson                                                  const CeedScalar stress[3][3], const CeedScalar dstress[3][3], CeedScalar dFe[3]) {
558475b2820SJames Wright   for (CeedInt i = 0; i < 3; i++) {
5592b916ea7SJeremy 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] -
5602b916ea7SJeremy L Thompson              Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] - gas->k * grad_ds[i].Y.temperature;
561475b2820SJames Wright   }
562475b2820SJames Wright }
563475b2820SJames Wright 
56440a33f2dSJames Wright CEED_QFUNCTION_HELPER void Vorticity(const State grad_s[3], CeedScalar vorticity[3]) {
56540a33f2dSJames Wright   CeedScalar grad_velocity[3][3];
56640a33f2dSJames Wright   VelocityGradient(grad_s, grad_velocity);
56740a33f2dSJames Wright   Curl3(grad_velocity, vorticity);
56840a33f2dSJames Wright }
56940a33f2dSJames Wright 
5702e7597b6SKenneth E. Jansen CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s, StateVariable state_var,
5712e7597b6SKenneth E. Jansen                                                               const CeedScalar *grad_q, const CeedScalar dXdx[3][3], State grad_s[3]) {
57287bd45e7SJames Wright   for (CeedInt k = 0; k < 3; k++) {
573edcfef1bSKenneth E. Jansen     CeedScalar dqi[5];
57487bd45e7SJames Wright     for (CeedInt j = 0; j < 5; j++) {
57587bd45e7SJames Wright       dqi[j] =
57687bd45e7SJames Wright           grad_q[(Q * 5) * 0 + Q * j + i] * dXdx[0][k] + grad_q[(Q * 5) * 1 + Q * j + i] * dXdx[1][k] + grad_q[(Q * 5) * 2 + Q * j + i] * dXdx[2][k];
57787bd45e7SJames Wright     }
578edcfef1bSKenneth E. Jansen     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
57987bd45e7SJames Wright   }
58087bd45e7SJames Wright }
58187bd45e7SJames Wright 
58287bd45e7SJames Wright CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_Boundary(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s,
5832e7597b6SKenneth E. Jansen                                                                        StateVariable state_var, const CeedScalar *grad_q, const CeedScalar dXdx[2][3],
5842e7597b6SKenneth E. Jansen                                                                        State grad_s[3]) {
58587bd45e7SJames Wright   for (CeedInt k = 0; k < 3; k++) {
586edcfef1bSKenneth E. Jansen     CeedScalar dqi[5];
58787bd45e7SJames Wright     for (CeedInt j = 0; j < 5; j++) {
58887bd45e7SJames Wright       dqi[j] = grad_q[(Q * 5) * 0 + Q * j + i] * dXdx[0][k] + grad_q[(Q * 5) * 1 + Q * j + i] * dXdx[1][k];
58987bd45e7SJames Wright     }
590edcfef1bSKenneth E. Jansen     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
59187bd45e7SJames Wright   }
59287bd45e7SJames Wright }
593