xref: /libCEED/examples/fluids/qfunctions/newtonian_state.h (revision 02b29df7b0ab76fdb65016753e3b39a54ba3f090)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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
10509d4af6SJeremy L Thompson #pragma once
11c6e8c570SJames Wright 
12c6e8c570SJames Wright #include <ceed.h>
13c9c2c079SJeremy L Thompson #include <math.h>
142b730f8bSJeremy L Thompson 
15c6e8c570SJames Wright #include "newtonian_types.h"
1613fa47b2SJames Wright #include "utils.h"
17c6e8c570SJames Wright 
18c6e8c570SJames Wright typedef struct {
19c6e8c570SJames Wright   CeedScalar density;
20c6e8c570SJames Wright   CeedScalar momentum[3];
21c6e8c570SJames Wright   CeedScalar E_total;
22c6e8c570SJames Wright } StateConservative;
23c6e8c570SJames Wright 
24c6e8c570SJames Wright typedef struct {
25c6e8c570SJames Wright   StateConservative U;
26c6e8c570SJames Wright   StatePrimitive    Y;
27*02b29df7SJames Wright   StateEntropy      V;
28c6e8c570SJames Wright } State;
29c6e8c570SJames Wright 
302b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_U(StateConservative s, CeedScalar U[5]) {
312b89d87eSLeila Ghaffari   U[0] = s.density;
322b89d87eSLeila Ghaffari   for (int i = 0; i < 3; i++) U[i + 1] = s.momentum[i];
332b89d87eSLeila Ghaffari   U[4] = s.E_total;
342b89d87eSLeila Ghaffari }
352b89d87eSLeila Ghaffari 
362b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) {
372b89d87eSLeila Ghaffari   Y[0] = s.pressure;
382b89d87eSLeila Ghaffari   for (int i = 0; i < 3; i++) Y[i + 1] = s.velocity[i];
392b89d87eSLeila Ghaffari   Y[4] = s.temperature;
402b89d87eSLeila Ghaffari }
412b89d87eSLeila Ghaffari 
42*02b29df7SJames Wright CEED_QFUNCTION_HELPER void UnpackState_V(StateEntropy s, CeedScalar V[5]) {
43*02b29df7SJames Wright   V[0] = s.S_density;
44*02b29df7SJames Wright   for (int i = 0; i < 3; i++) V[i + 1] = s.S_momentum[i];
45*02b29df7SJames Wright   V[4] = s.S_energy;
46*02b29df7SJames Wright }
47*02b29df7SJames Wright 
482b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio(NewtonianIdealGasContext gas) { return gas->cp / gas->cv; }
492518f336SLeila Ghaffari 
502b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar GasConstant(NewtonianIdealGasContext gas) { return gas->cp - gas->cv; }
512518f336SLeila Ghaffari 
522b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) { return gas->cp * gas->mu / gas->k; }
532518f336SLeila Ghaffari 
542b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas, CeedScalar T) { return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T); }
552518f336SLeila Ghaffari 
562b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas, CeedScalar T, CeedScalar u) { return u / SoundSpeed(gas, T); }
572518f336SLeila Ghaffari 
582b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy(NewtonianIdealGasContext gas, const State s) {
59f0a19222SJames Wright   // Ignoring potential energy
60f0a19222SJames Wright   CeedScalar e_internal = gas->cv * s.Y.temperature;
61f0a19222SJames Wright   CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
62f0a19222SJames Wright   return e_internal + e_kinetic + s.Y.pressure / s.U.density;
63f0a19222SJames Wright }
64f0a19222SJames Wright 
652b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy_fwd(NewtonianIdealGasContext gas, const State s, const State ds) {
66f0a19222SJames Wright   // Ignoring potential energy
67f0a19222SJames Wright   CeedScalar de_kinetic  = Dot3(ds.Y.velocity, s.Y.velocity);
68f0a19222SJames Wright   CeedScalar de_internal = gas->cv * ds.Y.temperature;
692b730f8bSJeremy L Thompson   return de_internal + de_kinetic + ds.Y.pressure / s.U.density - s.Y.pressure / Square(s.U.density) * ds.U.density;
70f0a19222SJames Wright }
71f0a19222SJames Wright 
723bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(NewtonianIdealGasContext gas, StateConservative U) {
73c6e8c570SJames Wright   StatePrimitive Y;
74c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) Y.velocity[i] = U.momentum[i] / U.density;
75c6e8c570SJames Wright   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
76c6e8c570SJames Wright   CeedScalar e_total    = U.E_total / U.density;
773bd61617SKenneth E. Jansen   CeedScalar e_internal = e_total - e_kinetic;
78c6e8c570SJames Wright   Y.temperature         = e_internal / gas->cv;
792518f336SLeila Ghaffari   Y.pressure            = (HeatCapacityRatio(gas) - 1) * U.density * e_internal;
80c6e8c570SJames Wright   return Y;
81c6e8c570SJames Wright }
82c6e8c570SJames Wright 
833bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) {
84c6e8c570SJames Wright   StatePrimitive dY;
85c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
86c6e8c570SJames Wright     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
87c6e8c570SJames Wright   }
88c6e8c570SJames Wright   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
89c6e8c570SJames Wright   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
90c6e8c570SJames Wright   CeedScalar e_total     = s.U.E_total / s.U.density;
91c6e8c570SJames Wright   CeedScalar de_total    = (dU.E_total - e_total * dU.density) / s.U.density;
923bd61617SKenneth E. Jansen   CeedScalar e_internal  = e_total - e_kinetic;
933bd61617SKenneth E. Jansen   CeedScalar de_internal = de_total - de_kinetic;
94c6e8c570SJames Wright   dY.temperature         = de_internal / gas->cv;
952b730f8bSJeremy L Thompson   dY.pressure            = (HeatCapacityRatio(gas) - 1) * (dU.density * e_internal + s.U.density * de_internal);
96c6e8c570SJames Wright   return dY;
97c6e8c570SJames Wright }
98c6e8c570SJames Wright 
993bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
100dc805cc4SLeila Ghaffari   StateConservative U;
1012518f336SLeila Ghaffari   U.density = Y.pressure / (GasConstant(gas) * Y.temperature);
102dc805cc4SLeila Ghaffari   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * Y.velocity[i];
103dc805cc4SLeila Ghaffari   CeedScalar e_internal = gas->cv * Y.temperature;
104dc805cc4SLeila Ghaffari   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
1053bd61617SKenneth E. Jansen   CeedScalar e_total    = e_internal + e_kinetic;
106dc805cc4SLeila Ghaffari   U.E_total             = U.density * e_total;
107dc805cc4SLeila Ghaffari   return U;
108dc805cc4SLeila Ghaffari }
109dc805cc4SLeila Ghaffari 
1103bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
111dc805cc4SLeila Ghaffari   StateConservative dU;
1122b730f8bSJeremy L Thompson   dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
113dc805cc4SLeila Ghaffari   for (int i = 0; i < 3; i++) {
114dc805cc4SLeila Ghaffari     dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i];
115dc805cc4SLeila Ghaffari   }
116dc805cc4SLeila Ghaffari   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
117dc805cc4SLeila Ghaffari   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
118dc805cc4SLeila Ghaffari   CeedScalar e_internal  = gas->cv * s.Y.temperature;
119dc805cc4SLeila Ghaffari   CeedScalar de_internal = gas->cv * dY.temperature;
1203bd61617SKenneth E. Jansen   CeedScalar e_total     = e_internal + e_kinetic;
1213bd61617SKenneth E. Jansen   CeedScalar de_total    = de_internal + de_kinetic;
122dc805cc4SLeila Ghaffari   dU.E_total             = dU.density * e_total + s.U.density * de_total;
123dc805cc4SLeila Ghaffari   return dU;
124dc805cc4SLeila Ghaffari }
125dc805cc4SLeila Ghaffari 
126*02b29df7SJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromConservative(NewtonianIdealGasContext gas, StateConservative U) {
127*02b29df7SJames Wright   StateEntropy     V;
128*02b29df7SJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
129*02b29df7SJames Wright   const CeedScalar e_kinetic  = .5 * Dot3(U.momentum, U.momentum) / U.density;
130*02b29df7SJames Wright   const CeedScalar e_internal = U.E_total - e_kinetic;
131*02b29df7SJames Wright   const CeedScalar p          = (gamma - 1) * e_internal;
132*02b29df7SJames Wright   const CeedScalar entropy    = log(p) - gamma * log(U.density);
133*02b29df7SJames Wright 
134*02b29df7SJames Wright   V.S_density = (gamma - entropy) / (gamma - 1) - e_kinetic / p;
135*02b29df7SJames Wright   for (int i = 0; i < 3; i++) V.S_momentum[i] = U.momentum[i] / p;
136*02b29df7SJames Wright   V.S_energy = -U.density / p;
137*02b29df7SJames Wright   return V;
138*02b29df7SJames Wright }
139*02b29df7SJames Wright 
140*02b29df7SJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) {
141*02b29df7SJames Wright   StateEntropy     dV;
142*02b29df7SJames Wright   const CeedScalar gamma       = HeatCapacityRatio(gas);
143*02b29df7SJames Wright   const CeedScalar e_kinetic   = .5 * Dot3(s.U.momentum, s.U.momentum) / s.U.density;
144*02b29df7SJames Wright   const CeedScalar de_kinetic  = (Dot3(s.U.momentum, dU.momentum) - e_kinetic * dU.density) / s.U.density;
145*02b29df7SJames Wright   const CeedScalar de_internal = dU.E_total - de_kinetic;
146*02b29df7SJames Wright   const CeedScalar p           = s.Y.pressure;
147*02b29df7SJames Wright   const CeedScalar dp          = (gamma - 1) * de_internal;
148*02b29df7SJames Wright 
149*02b29df7SJames Wright   CeedScalar dentropy = dp / p - gamma * dU.density / s.U.density;
150*02b29df7SJames Wright 
151*02b29df7SJames Wright   dV.S_density = -dentropy / (gamma - 1) - de_kinetic / p + dp * e_kinetic / Square(p);
152*02b29df7SJames Wright   for (CeedInt i = 0; i < 3; i++) {
153*02b29df7SJames Wright     dV.S_momentum[i] = (dU.momentum[i] - s.U.momentum[i] * dp / p) / p;
154*02b29df7SJames Wright   }
155*02b29df7SJames Wright   dV.S_energy = -(dU.density - s.U.density * dp / p) / p;
156*02b29df7SJames Wright   return dV;
157*02b29df7SJames Wright }
158*02b29df7SJames Wright 
159*02b29df7SJames Wright CEED_QFUNCTION_HELPER StateConservative StateConservativeFromEntropy(NewtonianIdealGasContext gas, StateEntropy V) {
160*02b29df7SJames Wright   StateConservative U;
161*02b29df7SJames Wright   CeedScalar        velocity[3];
162*02b29df7SJames Wright   for (int i = 0; i < 3; i++) velocity[i] = -V.S_momentum[i] / V.S_energy;
163*02b29df7SJames Wright   const CeedScalar gamma     = HeatCapacityRatio(gas);
164*02b29df7SJames Wright   const CeedScalar e_kinetic = 0.5 * Dot3(velocity, velocity);
165*02b29df7SJames Wright   const CeedScalar entropy   = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
166*02b29df7SJames Wright   const CeedScalar log_rho   = -(entropy + log(-V.S_energy)) / (gamma - 1);
167*02b29df7SJames Wright   U.density                  = exp(log_rho);
168*02b29df7SJames Wright   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * velocity[i];
169*02b29df7SJames Wright 
170*02b29df7SJames Wright   const CeedScalar e_internal = -gas->cv / (GasConstant(gas) * V.S_energy);
171*02b29df7SJames Wright   U.E_total                   = U.density * (e_internal + e_kinetic);
172*02b29df7SJames Wright   return U;
173*02b29df7SJames Wright }
174*02b29df7SJames Wright 
175*02b29df7SJames Wright CEED_QFUNCTION_HELPER StateConservative StateConservativeFromEntropy_fwd(NewtonianIdealGasContext gas, State s, StateEntropy dV) {
176*02b29df7SJames Wright   StateConservative dU;
177*02b29df7SJames Wright   CeedScalar        dvelocity[3];
178*02b29df7SJames 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*02b29df7SJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
180*02b29df7SJames Wright   const CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
181*02b29df7SJames Wright   const CeedScalar de_kinetic = Dot3(dvelocity, s.Y.velocity);
182*02b29df7SJames Wright   const CeedScalar entropy    = gamma - (gamma - 1) * (s.V.S_density - e_kinetic * s.V.S_energy);
183*02b29df7SJames Wright   const CeedScalar dentropy   = -(gamma - 1) * (dV.S_density - (de_kinetic * s.V.S_energy + e_kinetic * dV.S_energy));
184*02b29df7SJames Wright   const CeedScalar log_rho    = -(entropy + log(-s.V.S_energy)) / (gamma - 1);
185*02b29df7SJames Wright   const CeedScalar rho        = exp(log_rho);
186*02b29df7SJames Wright   dU.density                  = -rho / (gamma - 1) * (dentropy + dV.S_energy / s.V.S_energy);
187*02b29df7SJames Wright   for (int i = 0; i < 3; i++) dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dvelocity[i];
188*02b29df7SJames Wright 
189*02b29df7SJames Wright   const CeedScalar e_internal  = -gas->cv / (GasConstant(gas) * s.V.S_energy);
190*02b29df7SJames Wright   const CeedScalar de_internal = gas->cv * dV.S_energy / (GasConstant(gas) * s.V.S_energy * s.V.S_energy);
191*02b29df7SJames Wright   const CeedScalar e_total     = e_internal + e_kinetic;
192*02b29df7SJames Wright   dU.E_total                   = dU.density * e_total + s.U.density * (de_internal + de_kinetic);
193*02b29df7SJames Wright   return dU;
194*02b29df7SJames Wright }
195*02b29df7SJames Wright 
196*02b29df7SJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
197*02b29df7SJames Wright   StateEntropy     V;
198*02b29df7SJames Wright   const CeedScalar gamma     = HeatCapacityRatio(gas);
199*02b29df7SJames Wright   const CeedScalar rho       = Y.pressure / (GasConstant(gas) * Y.temperature);
200*02b29df7SJames Wright   const CeedScalar entropy   = log(Y.pressure) - gamma * log(rho);
201*02b29df7SJames Wright   const CeedScalar rho_div_p = rho / Y.pressure;
202*02b29df7SJames Wright   const CeedScalar e_kinetic = 0.5 * Dot3(Y.velocity, Y.velocity);
203*02b29df7SJames Wright 
204*02b29df7SJames Wright   V.S_density = (gamma - entropy) / (gamma - 1) - rho_div_p * e_kinetic;
205*02b29df7SJames Wright   for (int i = 0; i < 3; i++) V.S_momentum[i] = rho_div_p * Y.velocity[i];
206*02b29df7SJames Wright   V.S_energy = -rho_div_p;
207*02b29df7SJames Wright   return V;
208*02b29df7SJames Wright }
209*02b29df7SJames Wright 
210*02b29df7SJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
211*02b29df7SJames Wright   StateEntropy     dV;
212*02b29df7SJames Wright   const CeedScalar gamma = HeatCapacityRatio(gas);
213*02b29df7SJames Wright   CeedScalar       drho  = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
214*02b29df7SJames Wright 
215*02b29df7SJames Wright   const CeedScalar e_kinetic  = .5 * Dot3(s.Y.velocity, s.Y.velocity);
216*02b29df7SJames Wright   const CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
217*02b29df7SJames Wright   const CeedScalar rho_div_p  = s.U.density / s.Y.pressure;
218*02b29df7SJames Wright   const CeedScalar drho_div_p = (drho * s.Y.pressure - s.U.density * dY.pressure) / Square(s.Y.pressure);
219*02b29df7SJames Wright 
220*02b29df7SJames Wright   CeedScalar dentropy = dY.pressure / s.Y.pressure - gamma * drho / s.U.density;
221*02b29df7SJames Wright 
222*02b29df7SJames Wright   dV.S_density = -dentropy / (gamma - 1) - de_kinetic * rho_div_p - e_kinetic * drho_div_p;
223*02b29df7SJames 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*02b29df7SJames Wright   dV.S_energy = -drho_div_p;
225*02b29df7SJames Wright   return dV;
226*02b29df7SJames Wright }
227*02b29df7SJames Wright 
228*02b29df7SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromEntropy(NewtonianIdealGasContext gas, StateEntropy V) {
229*02b29df7SJames Wright   StatePrimitive Y;
230*02b29df7SJames Wright   for (int i = 0; i < 3; i++) Y.velocity[i] = -V.S_momentum[i] / V.S_energy;
231*02b29df7SJames Wright   Y.temperature              = -1 / (GasConstant(gas) * V.S_energy);
232*02b29df7SJames Wright   const CeedScalar gamma     = HeatCapacityRatio(gas);
233*02b29df7SJames Wright   const CeedScalar e_kinetic = 0.5 * Dot3(Y.velocity, Y.velocity);
234*02b29df7SJames Wright   const CeedScalar entropy   = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
235*02b29df7SJames Wright   const CeedScalar log_P     = -(entropy + gamma * log(-V.S_energy)) / (gamma - 1);
236*02b29df7SJames Wright   Y.pressure                 = exp(log_P);
237*02b29df7SJames Wright   return Y;
238*02b29df7SJames Wright }
239*02b29df7SJames Wright 
240*02b29df7SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromEntropy_fwd(NewtonianIdealGasContext gas, State s, StateEntropy dV) {
241*02b29df7SJames Wright   StatePrimitive dY;
242*02b29df7SJames 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*02b29df7SJames Wright   dY.temperature              = dV.S_energy / (GasConstant(gas) * s.V.S_energy * s.V.S_energy);
244*02b29df7SJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
245*02b29df7SJames Wright   const CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
246*02b29df7SJames Wright   const CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
247*02b29df7SJames Wright   const CeedScalar dentropy   = (1 - gamma) * (dV.S_density - e_kinetic * dV.S_energy - de_kinetic * s.V.S_energy);
248*02b29df7SJames Wright   dY.pressure                 = s.Y.pressure * (-dentropy - gamma * dV.S_energy / s.V.S_energy) / (gamma - 1);
249*02b29df7SJames Wright   return dY;
250*02b29df7SJames Wright }
251*02b29df7SJames Wright 
2523bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
2533bd61617SKenneth E. Jansen   StateConservative U = StateConservativeFromPrimitive(gas, Y);
254*02b29df7SJames Wright   StateEntropy      V = StateEntropyFromPrimitive(gas, Y);
255d310b3d3SAdeleke O. Bankole   State             s;
256d310b3d3SAdeleke O. Bankole   s.U = U;
257d310b3d3SAdeleke O. Bankole   s.Y = Y;
258*02b29df7SJames Wright   s.V = V;
259d310b3d3SAdeleke O. Bankole   return s;
260d310b3d3SAdeleke O. Bankole }
261d310b3d3SAdeleke O. Bankole 
2623bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
2633bd61617SKenneth E. Jansen   StateConservative dU = StateConservativeFromPrimitive_fwd(gas, s, dY);
264*02b29df7SJames Wright   StateEntropy      dV = StateEntropyFromPrimitive_fwd(gas, s, dY);
2658a94a473SJed Brown   State             ds;
2668a94a473SJed Brown   ds.U = dU;
2678a94a473SJed Brown   ds.Y = dY;
268*02b29df7SJames Wright   ds.V = dV;
2698a94a473SJed Brown   return ds;
2708a94a473SJed Brown }
2718a94a473SJed Brown 
272c95797efSJed Brown // linear combination of n states
273c95797efSJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeMult(CeedInt n, const CeedScalar a[], const StateConservative X[]) {
274c95797efSJed Brown   StateConservative R = {0};
275c95797efSJed Brown   for (CeedInt i = 0; i < n; i++) {
276c95797efSJed Brown     R.density += a[i] * X[i].density;
277c95797efSJed Brown     for (int j = 0; j < 3; j++) R.momentum[j] += a[i] * X[i].momentum[j];
278c95797efSJed Brown     R.E_total += a[i] * X[i].E_total;
279c95797efSJed Brown   }
28084ae27ebSJed Brown   return R;
28184ae27ebSJed Brown }
28284ae27ebSJed Brown 
28384ae27ebSJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeAXPBYPCZ(CeedScalar a, StateConservative X, CeedScalar b, StateConservative Y, CeedScalar c,
28484ae27ebSJed Brown                                                                   StateConservative Z) {
28584ae27ebSJed Brown   StateConservative R;
28684ae27ebSJed Brown   R.density = a * X.density + b * Y.density + c * Z.density;
28784ae27ebSJed Brown   for (int i = 0; i < 3; i++) R.momentum[i] = a * X.momentum[i] + b * Y.momentum[i] + c * Z.momentum[i];
28884ae27ebSJed Brown   R.E_total = a * X.E_total + b * Y.E_total + c * Z.E_total;
28984ae27ebSJed Brown   return R;
29084ae27ebSJed Brown }
29184ae27ebSJed Brown 
292d310b3d3SAdeleke O. Bankole CEED_QFUNCTION_HELPER void StateToU(NewtonianIdealGasContext gas, const State input, CeedScalar U[5]) { UnpackState_U(input.U, U); }
293d310b3d3SAdeleke O. Bankole 
294d310b3d3SAdeleke O. Bankole CEED_QFUNCTION_HELPER void StateToY(NewtonianIdealGasContext gas, const State input, CeedScalar Y[5]) { UnpackState_Y(input.Y, Y); }
295f179a332SJames Wright 
296*02b29df7SJames Wright CEED_QFUNCTION_HELPER void StateToV(NewtonianIdealGasContext gas, const State input, CeedScalar V[5]) { UnpackState_V(input.V, V); }
297*02b29df7SJames Wright 
298be91e165SJames Wright CEED_QFUNCTION_HELPER void StateToQ(NewtonianIdealGasContext gas, const State input, CeedScalar Q[5], StateVariable state_var) {
299be91e165SJames Wright   switch (state_var) {
300be91e165SJames Wright     case STATEVAR_CONSERVATIVE:
301be91e165SJames Wright       StateToU(gas, input, Q);
302be91e165SJames Wright       break;
303be91e165SJames Wright     case STATEVAR_PRIMITIVE:
304be91e165SJames Wright       StateToY(gas, input, Q);
305be91e165SJames Wright       break;
306*02b29df7SJames Wright     case STATEVAR_ENTROPY:
307*02b29df7SJames Wright       StateToV(gas, input, Q);
308*02b29df7SJames Wright       break;
309be91e165SJames Wright   }
310be91e165SJames Wright }
31120840d50SJames Wright 
3123bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, const CeedScalar U[5]) {
313c6e8c570SJames Wright   State s;
314c6e8c570SJames Wright   s.U.density     = U[0];
315c6e8c570SJames Wright   s.U.momentum[0] = U[1];
316c6e8c570SJames Wright   s.U.momentum[1] = U[2];
317c6e8c570SJames Wright   s.U.momentum[2] = U[3];
318c6e8c570SJames Wright   s.U.E_total     = U[4];
3193bd61617SKenneth E. Jansen   s.Y             = StatePrimitiveFromConservative(gas, s.U);
320*02b29df7SJames Wright   s.V             = StateEntropyFromConservative(gas, s.U);
321c6e8c570SJames Wright   return s;
322c6e8c570SJames Wright }
323c6e8c570SJames Wright 
3243bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dU[5]) {
325c6e8c570SJames Wright   State ds;
326c6e8c570SJames Wright   ds.U.density     = dU[0];
327c6e8c570SJames Wright   ds.U.momentum[0] = dU[1];
328c6e8c570SJames Wright   ds.U.momentum[1] = dU[2];
329c6e8c570SJames Wright   ds.U.momentum[2] = dU[3];
330c6e8c570SJames Wright   ds.U.E_total     = dU[4];
3313bd61617SKenneth E. Jansen   ds.Y             = StatePrimitiveFromConservative_fwd(gas, s, ds.U);
332*02b29df7SJames Wright   ds.V             = StateEntropyFromConservative_fwd(gas, s, ds.U);
333c6e8c570SJames Wright   return ds;
334c6e8c570SJames Wright }
335c6e8c570SJames Wright 
3363bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, const CeedScalar Y[5]) {
337dc805cc4SLeila Ghaffari   State s;
338dc805cc4SLeila Ghaffari   s.Y.pressure    = Y[0];
339dc805cc4SLeila Ghaffari   s.Y.velocity[0] = Y[1];
340dc805cc4SLeila Ghaffari   s.Y.velocity[1] = Y[2];
341dc805cc4SLeila Ghaffari   s.Y.velocity[2] = Y[3];
342dc805cc4SLeila Ghaffari   s.Y.temperature = Y[4];
3433bd61617SKenneth E. Jansen   s.U             = StateConservativeFromPrimitive(gas, s.Y);
344*02b29df7SJames Wright   s.V             = StateEntropyFromPrimitive(gas, s.Y);
345dc805cc4SLeila Ghaffari   return s;
346dc805cc4SLeila Ghaffari }
347dc805cc4SLeila Ghaffari 
3483bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dY[5]) {
349dc805cc4SLeila Ghaffari   State ds;
350dc805cc4SLeila Ghaffari   ds.Y.pressure    = dY[0];
351dc805cc4SLeila Ghaffari   ds.Y.velocity[0] = dY[1];
352dc805cc4SLeila Ghaffari   ds.Y.velocity[1] = dY[2];
353dc805cc4SLeila Ghaffari   ds.Y.velocity[2] = dY[3];
354dc805cc4SLeila Ghaffari   ds.Y.temperature = dY[4];
3553bd61617SKenneth E. Jansen   ds.U             = StateConservativeFromPrimitive_fwd(gas, s, ds.Y);
356*02b29df7SJames Wright   ds.V             = StateEntropyFromPrimitive_fwd(gas, s, ds.Y);
357*02b29df7SJames Wright   return ds;
358*02b29df7SJames Wright }
359*02b29df7SJames Wright 
360*02b29df7SJames Wright CEED_QFUNCTION_HELPER State StateFromV(NewtonianIdealGasContext gas, const CeedScalar V[5]) {
361*02b29df7SJames Wright   State s;
362*02b29df7SJames Wright   s.V.S_density     = V[0];
363*02b29df7SJames Wright   s.V.S_momentum[0] = V[1];
364*02b29df7SJames Wright   s.V.S_momentum[1] = V[2];
365*02b29df7SJames Wright   s.V.S_momentum[2] = V[3];
366*02b29df7SJames Wright   s.V.S_energy      = V[4];
367*02b29df7SJames Wright   s.U               = StateConservativeFromEntropy(gas, s.V);
368*02b29df7SJames Wright   s.Y               = StatePrimitiveFromEntropy(gas, s.V);
369*02b29df7SJames Wright   return s;
370*02b29df7SJames Wright }
371*02b29df7SJames Wright 
372*02b29df7SJames Wright CEED_QFUNCTION_HELPER State StateFromV_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dV[5]) {
373*02b29df7SJames Wright   State ds;
374*02b29df7SJames Wright   ds.V.S_density     = dV[0];
375*02b29df7SJames Wright   ds.V.S_momentum[0] = dV[1];
376*02b29df7SJames Wright   ds.V.S_momentum[1] = dV[2];
377*02b29df7SJames Wright   ds.V.S_momentum[2] = dV[3];
378*02b29df7SJames Wright   ds.V.S_energy      = dV[4];
379*02b29df7SJames Wright   ds.U               = StateConservativeFromEntropy_fwd(gas, s, ds.V);
380*02b29df7SJames Wright   ds.Y               = StatePrimitiveFromEntropy_fwd(gas, s, ds.V);
381dc805cc4SLeila Ghaffari   return ds;
382dc805cc4SLeila Ghaffari }
383dc805cc4SLeila Ghaffari 
3843bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromQ(NewtonianIdealGasContext gas, const CeedScalar Q[5], StateVariable state_var) {
385be91e165SJames Wright   State s;
386be91e165SJames Wright   switch (state_var) {
387be91e165SJames Wright     case STATEVAR_CONSERVATIVE:
3883bd61617SKenneth E. Jansen       s = StateFromU(gas, Q);
389be91e165SJames Wright       break;
390be91e165SJames Wright     case STATEVAR_PRIMITIVE:
3913bd61617SKenneth E. Jansen       s = StateFromY(gas, Q);
392be91e165SJames Wright       break;
393*02b29df7SJames Wright     case STATEVAR_ENTROPY:
394*02b29df7SJames Wright       s = StateFromV(gas, Q);
395*02b29df7SJames Wright       break;
396be91e165SJames Wright   }
397be91e165SJames Wright   return s;
398be91e165SJames Wright }
399be91e165SJames Wright 
40028c2e84aSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromQ_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dQ[5], StateVariable state_var) {
401be91e165SJames Wright   State ds;
402be91e165SJames Wright   switch (state_var) {
403be91e165SJames Wright     case STATEVAR_CONSERVATIVE:
4043bd61617SKenneth E. Jansen       ds = StateFromU_fwd(gas, s, dQ);
405be91e165SJames Wright       break;
406be91e165SJames Wright     case STATEVAR_PRIMITIVE:
4073bd61617SKenneth E. Jansen       ds = StateFromY_fwd(gas, s, dQ);
408be91e165SJames Wright       break;
409*02b29df7SJames Wright     case STATEVAR_ENTROPY:
410*02b29df7SJames Wright       ds = StateFromV_fwd(gas, s, dQ);
411*02b29df7SJames Wright       break;
412be91e165SJames Wright   }
413be91e165SJames Wright   return ds;
414be91e165SJames Wright }
415be91e165SJames Wright 
4162b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, StateConservative Flux[3]) {
417c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
418c6e8c570SJames Wright     Flux[i].density = s.U.momentum[i];
4192b730f8bSJeremy 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);
420c6e8c570SJames Wright     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
421c6e8c570SJames Wright   }
422c6e8c570SJames Wright }
423c6e8c570SJames Wright 
4242b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, State s, State ds, StateConservative dFlux[3]) {
425f0a19222SJames Wright   for (CeedInt i = 0; i < 3; i++) {
426f0a19222SJames Wright     dFlux[i].density = ds.U.momentum[i];
4272b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
4282b730f8bSJeremy 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);
4292b730f8bSJeremy L Thompson     }
4302b730f8bSJeremy 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];
431f0a19222SJames Wright   }
432f0a19222SJames Wright }
433f0a19222SJames Wright 
4342b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal(NewtonianIdealGasContext gas, State s, const CeedScalar normal[3]) {
435738af36cSAdelekeBankole   StateConservative Flux[3], Flux_dot_n = {0};
436738af36cSAdelekeBankole   FluxInviscid(gas, s, Flux);
437738af36cSAdelekeBankole   for (CeedInt i = 0; i < 3; i++) {
438738af36cSAdelekeBankole     Flux_dot_n.density += Flux[i].density * normal[i];
4392b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += Flux[i].momentum[j] * normal[i];
440738af36cSAdelekeBankole     Flux_dot_n.E_total += Flux[i].E_total * normal[i];
441738af36cSAdelekeBankole   }
442738af36cSAdelekeBankole   return Flux_dot_n;
443738af36cSAdelekeBankole }
444738af36cSAdelekeBankole 
4452b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal_fwd(NewtonianIdealGasContext gas, State s, State ds, const CeedScalar normal[3]) {
446f0a19222SJames Wright   StateConservative dFlux[3], Flux_dot_n = {0};
447f0a19222SJames Wright   FluxInviscid_fwd(gas, s, ds, dFlux);
448c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
449f0a19222SJames Wright     Flux_dot_n.density += dFlux[i].density * normal[i];
4502b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += dFlux[i].momentum[j] * normal[i];
451f0a19222SJames Wright     Flux_dot_n.E_total += dFlux[i].E_total * normal[i];
452c6e8c570SJames Wright   }
453f0a19222SJames Wright   return Flux_dot_n;
454c6e8c570SJames Wright }
455c6e8c570SJames Wright 
4562b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, State s, State ds[3], CeedScalar strong_conv[5]) {
4572b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 5; i++) strong_conv[i] = 0;
4582b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 3; i++) {
4592b89d87eSLeila Ghaffari     StateConservative dF[3];
4602b89d87eSLeila Ghaffari     FluxInviscid_fwd(gas, s, ds[i], dF);
4612b89d87eSLeila Ghaffari     CeedScalar dF_i[5];
4622b89d87eSLeila Ghaffari     UnpackState_U(dF[i], dF_i);
4632b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) strong_conv[j] += dF_i[j];
4642b89d87eSLeila Ghaffari   }
4652b89d87eSLeila Ghaffari }
4662b89d87eSLeila Ghaffari 
4672b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3], CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) {
4682b89d87eSLeila Ghaffari   for (CeedInt j = 0; j < 3; j++) {
4692b89d87eSLeila Ghaffari     Flux[0][j] = F_inviscid[j].density;
4702b730f8bSJeremy L Thompson     for (CeedInt k = 0; k < 3; k++) Flux[k + 1][j] = F_inviscid[j].momentum[k] - stress[k][j];
4712b89d87eSLeila Ghaffari     Flux[4][j] = F_inviscid[j].E_total + Fe[j];
4722b89d87eSLeila Ghaffari   }
4732b89d87eSLeila Ghaffari }
4742b89d87eSLeila Ghaffari 
4752b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal_Boundary(const StateConservative F_inviscid[3], const CeedScalar stress[3][3], const CeedScalar Fe[3],
4762b730f8bSJeremy L Thompson                                               const CeedScalar normal[3], CeedScalar Flux[5]) {
4775bce47c7SJames Wright   for (CeedInt j = 0; j < 5; j++) Flux[j] = 0.;
4785bce47c7SJames Wright   for (CeedInt j = 0; j < 3; j++) {
4795bce47c7SJames Wright     Flux[0] += F_inviscid[j].density * normal[j];
4805bce47c7SJames Wright     for (CeedInt k = 0; k < 3; k++) {
4815bce47c7SJames Wright       Flux[k + 1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j];
4825bce47c7SJames Wright     }
4835bce47c7SJames Wright     Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j];
4845bce47c7SJames Wright   }
4855bce47c7SJames Wright }
4865bce47c7SJames Wright 
4878a94a473SJed Brown CEED_QFUNCTION_HELPER void FluxTotal_RiemannBoundary(const StateConservative F_inviscid_normal, const CeedScalar stress[3][3], const CeedScalar Fe[3],
4888a94a473SJed Brown                                                      const CeedScalar normal[3], CeedScalar Flux[5]) {
4898a94a473SJed Brown   Flux[0] = F_inviscid_normal.density;
4908a94a473SJed Brown   for (CeedInt k = 0; k < 3; k++) Flux[k + 1] = F_inviscid_normal.momentum[k];
4918a94a473SJed Brown   Flux[4] = F_inviscid_normal.E_total;
4928a94a473SJed Brown   for (CeedInt j = 0; j < 3; j++) {
4938a94a473SJed Brown     for (CeedInt k = 0; k < 3; k++) {
4948a94a473SJed Brown       Flux[k + 1] -= stress[k][j] * normal[j];
4958a94a473SJed Brown     }
4968a94a473SJed Brown     Flux[4] += Fe[j] * normal[j];
4978a94a473SJed Brown   }
4988a94a473SJed Brown }
4998a94a473SJed Brown 
500d08fcc28SJames Wright CEED_QFUNCTION_HELPER void VelocityGradient(const State grad_s[3], CeedScalar grad_velocity[3][3]) {
501d08fcc28SJames Wright   grad_velocity[0][0] = grad_s[0].Y.velocity[0];
502d08fcc28SJames Wright   grad_velocity[0][1] = grad_s[1].Y.velocity[0];
503d08fcc28SJames Wright   grad_velocity[0][2] = grad_s[2].Y.velocity[0];
504d08fcc28SJames Wright   grad_velocity[1][0] = grad_s[0].Y.velocity[1];
505d08fcc28SJames Wright   grad_velocity[1][1] = grad_s[1].Y.velocity[1];
506d08fcc28SJames Wright   grad_velocity[1][2] = grad_s[2].Y.velocity[1];
507d08fcc28SJames Wright   grad_velocity[2][0] = grad_s[0].Y.velocity[2];
508d08fcc28SJames Wright   grad_velocity[2][1] = grad_s[1].Y.velocity[2];
509d08fcc28SJames Wright   grad_velocity[2][2] = grad_s[2].Y.velocity[2];
510d08fcc28SJames Wright }
511d08fcc28SJames Wright 
512d08fcc28SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const CeedScalar grad_velocity[3][3], CeedScalar strain_rate[6]) {
513d08fcc28SJames Wright   const CeedScalar weight = 1 / sqrt(2.);  // Really sqrt(2.) / 2
514d08fcc28SJames Wright   strain_rate[0]          = grad_velocity[0][0];
515d08fcc28SJames Wright   strain_rate[1]          = grad_velocity[1][1];
516d08fcc28SJames Wright   strain_rate[2]          = grad_velocity[2][2];
517d08fcc28SJames Wright   strain_rate[3]          = weight * (grad_velocity[1][2] + grad_velocity[2][1]);
518d08fcc28SJames Wright   strain_rate[4]          = weight * (grad_velocity[0][2] + grad_velocity[2][0]);
519d08fcc28SJames Wright   strain_rate[5]          = weight * (grad_velocity[0][1] + grad_velocity[1][0]);
520d08fcc28SJames Wright }
521d08fcc28SJames Wright 
522c6e8c570SJames Wright // Kelvin-Mandel notation
523d08fcc28SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate_State(const State grad_s[3], CeedScalar strain_rate[6]) {
524d08fcc28SJames Wright   CeedScalar grad_velocity[3][3];
525d08fcc28SJames Wright   VelocityGradient(grad_s, grad_velocity);
526d08fcc28SJames Wright   KMStrainRate(grad_velocity, strain_rate);
527d08fcc28SJames Wright }
528d08fcc28SJames Wright 
5297618d7b7SJames Wright //@brief Given velocity gradient du_i/dx_j, return 0.5*(du_i/dx_j - du_j/dx_i)
530d08fcc28SJames Wright CEED_QFUNCTION_HELPER void RotationRate(const CeedScalar grad_velocity[3][3], CeedScalar rotation_rate[3][3]) {
531d08fcc28SJames Wright   rotation_rate[0][0] = 0;
532d08fcc28SJames Wright   rotation_rate[1][1] = 0;
533d08fcc28SJames Wright   rotation_rate[2][2] = 0;
534d08fcc28SJames Wright   rotation_rate[1][2] = 0.5 * (grad_velocity[1][2] - grad_velocity[2][1]);
535d08fcc28SJames Wright   rotation_rate[0][2] = 0.5 * (grad_velocity[0][2] - grad_velocity[2][0]);
536d08fcc28SJames Wright   rotation_rate[0][1] = 0.5 * (grad_velocity[0][1] - grad_velocity[1][0]);
537d08fcc28SJames Wright   rotation_rate[2][1] = -rotation_rate[1][2];
538d08fcc28SJames Wright   rotation_rate[2][0] = -rotation_rate[0][2];
539d08fcc28SJames Wright   rotation_rate[1][0] = -rotation_rate[0][1];
540c6e8c570SJames Wright }
541c6e8c570SJames Wright 
5422b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, const CeedScalar strain_rate[6], CeedScalar stress[6]) {
543c6e8c570SJames Wright   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
544c6e8c570SJames Wright   for (CeedInt i = 0; i < 6; i++) {
545c6e8c570SJames Wright     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
546c6e8c570SJames Wright   }
547c6e8c570SJames Wright }
548c6e8c570SJames Wright 
5492b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
550c6e8c570SJames Wright                                              CeedScalar Fe[3]) {
551c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
5522b730f8bSJeremy 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;
553c6e8c570SJames Wright   }
554c6e8c570SJames Wright }
555c6e8c570SJames Wright 
5562b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
5572b730f8bSJeremy L Thompson                                                  const CeedScalar stress[3][3], const CeedScalar dstress[3][3], CeedScalar dFe[3]) {
558c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
5592b730f8bSJeremy 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] -
5602b730f8bSJeremy L Thompson              Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] - gas->k * grad_ds[i].Y.temperature;
561c6e8c570SJames Wright   }
562c6e8c570SJames Wright }
563c6e8c570SJames Wright 
564d08fcc28SJames Wright CEED_QFUNCTION_HELPER void Vorticity(const State grad_s[3], CeedScalar vorticity[3]) {
565d08fcc28SJames Wright   CeedScalar grad_velocity[3][3];
566d08fcc28SJames Wright   VelocityGradient(grad_s, grad_velocity);
567d08fcc28SJames Wright   Curl3(grad_velocity, vorticity);
568d08fcc28SJames Wright }
569d08fcc28SJames Wright 
57028c2e84aSKenneth E. Jansen CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s, StateVariable state_var,
57128c2e84aSKenneth E. Jansen                                                               const CeedScalar *grad_q, const CeedScalar dXdx[3][3], State grad_s[3]) {
5729b6a821dSJames Wright   for (CeedInt k = 0; k < 3; k++) {
5733bd61617SKenneth E. Jansen     CeedScalar dqi[5];
5749b6a821dSJames Wright     for (CeedInt j = 0; j < 5; j++) {
5759b6a821dSJames Wright       dqi[j] =
5769b6a821dSJames 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];
5779b6a821dSJames Wright     }
5783bd61617SKenneth E. Jansen     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
5799b6a821dSJames Wright   }
5809b6a821dSJames Wright }
5819b6a821dSJames Wright 
5829b6a821dSJames Wright CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_Boundary(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s,
58328c2e84aSKenneth E. Jansen                                                                        StateVariable state_var, const CeedScalar *grad_q, const CeedScalar dXdx[2][3],
58428c2e84aSKenneth E. Jansen                                                                        State grad_s[3]) {
5859b6a821dSJames Wright   for (CeedInt k = 0; k < 3; k++) {
5863bd61617SKenneth E. Jansen     CeedScalar dqi[5];
5879b6a821dSJames Wright     for (CeedInt j = 0; j < 5; j++) {
5889b6a821dSJames 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];
5899b6a821dSJames Wright     }
5903bd61617SKenneth E. Jansen     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
5919b6a821dSJames Wright   }
5929b6a821dSJames Wright }
593