xref: /libCEED/examples/fluids/qfunctions/newtonian_state.h (revision 6fa2c4dafed9e033f9d25ea23122f61b16180a35)
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;
27c6e8c570SJames Wright } State;
28c6e8c570SJames Wright 
292b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_U(StateConservative s, CeedScalar U[5]) {
302b89d87eSLeila Ghaffari   U[0] = s.density;
312b89d87eSLeila Ghaffari   for (int i = 0; i < 3; i++) U[i + 1] = s.momentum[i];
322b89d87eSLeila Ghaffari   U[4] = s.E_total;
332b89d87eSLeila Ghaffari }
342b89d87eSLeila Ghaffari 
352b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) {
362b89d87eSLeila Ghaffari   Y[0] = s.pressure;
372b89d87eSLeila Ghaffari   for (int i = 0; i < 3; i++) Y[i + 1] = s.velocity[i];
382b89d87eSLeila Ghaffari   Y[4] = s.temperature;
392b89d87eSLeila Ghaffari }
402b89d87eSLeila Ghaffari 
4102b29df7SJames Wright CEED_QFUNCTION_HELPER void UnpackState_V(StateEntropy s, CeedScalar V[5]) {
4202b29df7SJames Wright   V[0] = s.S_density;
4302b29df7SJames Wright   for (int i = 0; i < 3; i++) V[i + 1] = s.S_momentum[i];
4402b29df7SJames Wright   V[4] = s.S_energy;
4502b29df7SJames Wright }
4602b29df7SJames Wright 
472b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio(NewtonianIdealGasContext gas) { return gas->cp / gas->cv; }
482518f336SLeila Ghaffari 
492b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar GasConstant(NewtonianIdealGasContext gas) { return gas->cp - gas->cv; }
502518f336SLeila Ghaffari 
512b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) { return gas->cp * gas->mu / gas->k; }
522518f336SLeila Ghaffari 
532b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas, CeedScalar T) { return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T); }
542518f336SLeila Ghaffari 
552b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas, CeedScalar T, CeedScalar u) { return u / SoundSpeed(gas, T); }
562518f336SLeila Ghaffari 
572b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy(NewtonianIdealGasContext gas, const State s) {
58f0a19222SJames Wright   CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
59*6fa2c4daSJames Wright   CeedScalar e_internal = gas->cv * s.Y.temperature;
60f0a19222SJames Wright   return e_internal + e_kinetic + s.Y.pressure / s.U.density;
61f0a19222SJames Wright }
62f0a19222SJames Wright 
632b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy_fwd(NewtonianIdealGasContext gas, const State s, const State ds) {
64f0a19222SJames Wright   CeedScalar de_kinetic  = Dot3(ds.Y.velocity, s.Y.velocity);
65f0a19222SJames Wright   CeedScalar de_internal = gas->cv * ds.Y.temperature;
662b730f8bSJeremy L Thompson   return de_internal + de_kinetic + ds.Y.pressure / s.U.density - s.Y.pressure / Square(s.U.density) * ds.U.density;
67f0a19222SJames Wright }
68f0a19222SJames Wright 
693bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(NewtonianIdealGasContext gas, StateConservative U) {
70c6e8c570SJames Wright   StatePrimitive Y;
71c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) Y.velocity[i] = U.momentum[i] / U.density;
72c6e8c570SJames Wright   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
73c6e8c570SJames Wright   CeedScalar e_total    = U.E_total / U.density;
743bd61617SKenneth E. Jansen   CeedScalar e_internal = e_total - e_kinetic;
75c6e8c570SJames Wright   Y.temperature         = e_internal / gas->cv;
762518f336SLeila Ghaffari   Y.pressure            = (HeatCapacityRatio(gas) - 1) * U.density * e_internal;
77c6e8c570SJames Wright   return Y;
78c6e8c570SJames Wright }
79c6e8c570SJames Wright 
803bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) {
81c6e8c570SJames Wright   StatePrimitive dY;
82c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
83c6e8c570SJames Wright     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
84c6e8c570SJames Wright   }
85c6e8c570SJames Wright   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
86c6e8c570SJames Wright   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
87c6e8c570SJames Wright   CeedScalar e_total     = s.U.E_total / s.U.density;
88c6e8c570SJames Wright   CeedScalar de_total    = (dU.E_total - e_total * dU.density) / s.U.density;
893bd61617SKenneth E. Jansen   CeedScalar e_internal  = e_total - e_kinetic;
903bd61617SKenneth E. Jansen   CeedScalar de_internal = de_total - de_kinetic;
91c6e8c570SJames Wright   dY.temperature         = de_internal / gas->cv;
922b730f8bSJeremy L Thompson   dY.pressure            = (HeatCapacityRatio(gas) - 1) * (dU.density * e_internal + s.U.density * de_internal);
93c6e8c570SJames Wright   return dY;
94c6e8c570SJames Wright }
95c6e8c570SJames Wright 
96eba67db8SJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
97eba67db8SJames Wright   StateEntropy     V;
98eba67db8SJames Wright   const CeedScalar gamma     = HeatCapacityRatio(gas);
99eba67db8SJames Wright   const CeedScalar rho       = Y.pressure / (GasConstant(gas) * Y.temperature);
100eba67db8SJames Wright   const CeedScalar entropy   = log(Y.pressure) - gamma * log(rho);
101eba67db8SJames Wright   const CeedScalar rho_div_p = rho / Y.pressure;
102eba67db8SJames Wright   const CeedScalar e_kinetic = 0.5 * Dot3(Y.velocity, Y.velocity);
103eba67db8SJames Wright 
104eba67db8SJames Wright   V.S_density = (gamma - entropy) / (gamma - 1) - rho_div_p * e_kinetic;
105eba67db8SJames Wright   for (int i = 0; i < 3; i++) V.S_momentum[i] = rho_div_p * Y.velocity[i];
106eba67db8SJames Wright   V.S_energy = -rho_div_p;
107eba67db8SJames Wright   return V;
108eba67db8SJames Wright }
109eba67db8SJames Wright 
110eba67db8SJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
111eba67db8SJames Wright   StateEntropy     dV;
112eba67db8SJames Wright   const CeedScalar gamma = HeatCapacityRatio(gas);
113eba67db8SJames Wright   CeedScalar       drho  = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
114eba67db8SJames Wright 
115eba67db8SJames Wright   const CeedScalar e_kinetic  = .5 * Dot3(s.Y.velocity, s.Y.velocity);
116eba67db8SJames Wright   const CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
117eba67db8SJames Wright   const CeedScalar rho_div_p  = s.U.density / s.Y.pressure;
118eba67db8SJames Wright   const CeedScalar drho_div_p = (drho * s.Y.pressure - s.U.density * dY.pressure) / Square(s.Y.pressure);
119eba67db8SJames Wright 
120eba67db8SJames Wright   CeedScalar dentropy = dY.pressure / s.Y.pressure - gamma * drho / s.U.density;
121eba67db8SJames Wright 
122eba67db8SJames Wright   dV.S_density = -dentropy / (gamma - 1) - de_kinetic * rho_div_p - e_kinetic * drho_div_p;
123eba67db8SJames 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];
124eba67db8SJames Wright   dV.S_energy = -drho_div_p;
125eba67db8SJames Wright   return dV;
126eba67db8SJames Wright }
127eba67db8SJames Wright 
128eba67db8SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromEntropy(NewtonianIdealGasContext gas, StateEntropy V) {
129eba67db8SJames Wright   StatePrimitive Y;
130eba67db8SJames Wright   for (int i = 0; i < 3; i++) Y.velocity[i] = -V.S_momentum[i] / V.S_energy;
131eba67db8SJames Wright   Y.temperature              = -1 / (GasConstant(gas) * V.S_energy);
132eba67db8SJames Wright   const CeedScalar gamma     = HeatCapacityRatio(gas);
133eba67db8SJames Wright   const CeedScalar e_kinetic = 0.5 * Dot3(Y.velocity, Y.velocity);
134eba67db8SJames Wright   const CeedScalar entropy   = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
135eba67db8SJames Wright   const CeedScalar log_P     = -(entropy + gamma * log(-V.S_energy)) / (gamma - 1);
136eba67db8SJames Wright   Y.pressure                 = exp(log_P);
137eba67db8SJames Wright   return Y;
138eba67db8SJames Wright }
139eba67db8SJames Wright 
140eba67db8SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromEntropy_fwd(NewtonianIdealGasContext gas, State s, StateEntropy dV) {
141eba67db8SJames Wright   StatePrimitive dY;
142eba67db8SJames Wright   StateEntropy   V = StateEntropyFromPrimitive(gas, s.Y);
143eba67db8SJames Wright   for (int i = 0; i < 3; i++) dY.velocity[i] = -(dV.S_momentum[i] - V.S_momentum[i] * dV.S_energy / V.S_energy) / V.S_energy;
144eba67db8SJames Wright   dY.temperature              = dV.S_energy / (GasConstant(gas) * V.S_energy * V.S_energy);
145eba67db8SJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
146eba67db8SJames Wright   const CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
147eba67db8SJames Wright   const CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
148eba67db8SJames Wright   const CeedScalar dentropy   = (1 - gamma) * (dV.S_density - e_kinetic * dV.S_energy - de_kinetic * V.S_energy);
149eba67db8SJames Wright   dY.pressure                 = s.Y.pressure * (-dentropy - gamma * dV.S_energy / V.S_energy) / (gamma - 1);
150eba67db8SJames Wright   return dY;
151eba67db8SJames Wright }
152eba67db8SJames Wright 
1533bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
154dc805cc4SLeila Ghaffari   StateConservative U;
1552518f336SLeila Ghaffari   U.density = Y.pressure / (GasConstant(gas) * Y.temperature);
156dc805cc4SLeila Ghaffari   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * Y.velocity[i];
157dc805cc4SLeila Ghaffari   CeedScalar e_internal = gas->cv * Y.temperature;
158dc805cc4SLeila Ghaffari   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
1593bd61617SKenneth E. Jansen   CeedScalar e_total    = e_internal + e_kinetic;
160dc805cc4SLeila Ghaffari   U.E_total             = U.density * e_total;
161dc805cc4SLeila Ghaffari   return U;
162dc805cc4SLeila Ghaffari }
163dc805cc4SLeila Ghaffari 
1643bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
165dc805cc4SLeila Ghaffari   StateConservative dU;
1662b730f8bSJeremy L Thompson   dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
167dc805cc4SLeila Ghaffari   for (int i = 0; i < 3; i++) {
168dc805cc4SLeila Ghaffari     dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i];
169dc805cc4SLeila Ghaffari   }
170dc805cc4SLeila Ghaffari   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
171dc805cc4SLeila Ghaffari   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
172dc805cc4SLeila Ghaffari   CeedScalar e_internal  = gas->cv * s.Y.temperature;
173dc805cc4SLeila Ghaffari   CeedScalar de_internal = gas->cv * dY.temperature;
1743bd61617SKenneth E. Jansen   CeedScalar e_total     = e_internal + e_kinetic;
1753bd61617SKenneth E. Jansen   CeedScalar de_total    = de_internal + de_kinetic;
176dc805cc4SLeila Ghaffari   dU.E_total             = dU.density * e_total + s.U.density * de_total;
177dc805cc4SLeila Ghaffari   return dU;
178dc805cc4SLeila Ghaffari }
179dc805cc4SLeila Ghaffari 
18002b29df7SJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromConservative(NewtonianIdealGasContext gas, StateConservative U) {
18102b29df7SJames Wright   StateEntropy     V;
18202b29df7SJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
18302b29df7SJames Wright   const CeedScalar e_kinetic  = .5 * Dot3(U.momentum, U.momentum) / U.density;
18402b29df7SJames Wright   const CeedScalar e_internal = U.E_total - e_kinetic;
18502b29df7SJames Wright   const CeedScalar p          = (gamma - 1) * e_internal;
18602b29df7SJames Wright   const CeedScalar entropy    = log(p) - gamma * log(U.density);
18702b29df7SJames Wright 
18802b29df7SJames Wright   V.S_density = (gamma - entropy) / (gamma - 1) - e_kinetic / p;
18902b29df7SJames Wright   for (int i = 0; i < 3; i++) V.S_momentum[i] = U.momentum[i] / p;
19002b29df7SJames Wright   V.S_energy = -U.density / p;
19102b29df7SJames Wright   return V;
19202b29df7SJames Wright }
19302b29df7SJames Wright 
19402b29df7SJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) {
19502b29df7SJames Wright   StateEntropy     dV;
19602b29df7SJames Wright   const CeedScalar gamma       = HeatCapacityRatio(gas);
19702b29df7SJames Wright   const CeedScalar e_kinetic   = .5 * Dot3(s.U.momentum, s.U.momentum) / s.U.density;
19802b29df7SJames Wright   const CeedScalar de_kinetic  = (Dot3(s.U.momentum, dU.momentum) - e_kinetic * dU.density) / s.U.density;
19902b29df7SJames Wright   const CeedScalar de_internal = dU.E_total - de_kinetic;
20002b29df7SJames Wright   const CeedScalar p           = s.Y.pressure;
20102b29df7SJames Wright   const CeedScalar dp          = (gamma - 1) * de_internal;
20202b29df7SJames Wright 
20302b29df7SJames Wright   CeedScalar dentropy = dp / p - gamma * dU.density / s.U.density;
20402b29df7SJames Wright 
20502b29df7SJames Wright   dV.S_density = -dentropy / (gamma - 1) - de_kinetic / p + dp * e_kinetic / Square(p);
20602b29df7SJames Wright   for (CeedInt i = 0; i < 3; i++) {
20702b29df7SJames Wright     dV.S_momentum[i] = (dU.momentum[i] - s.U.momentum[i] * dp / p) / p;
20802b29df7SJames Wright   }
20902b29df7SJames Wright   dV.S_energy = -(dU.density - s.U.density * dp / p) / p;
21002b29df7SJames Wright   return dV;
21102b29df7SJames Wright }
21202b29df7SJames Wright 
21302b29df7SJames Wright CEED_QFUNCTION_HELPER StateConservative StateConservativeFromEntropy(NewtonianIdealGasContext gas, StateEntropy V) {
21402b29df7SJames Wright   StateConservative U;
21502b29df7SJames Wright   CeedScalar        velocity[3];
21602b29df7SJames Wright   for (int i = 0; i < 3; i++) velocity[i] = -V.S_momentum[i] / V.S_energy;
21702b29df7SJames Wright   const CeedScalar gamma     = HeatCapacityRatio(gas);
21802b29df7SJames Wright   const CeedScalar e_kinetic = 0.5 * Dot3(velocity, velocity);
21902b29df7SJames Wright   const CeedScalar entropy   = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
22002b29df7SJames Wright   const CeedScalar log_rho   = -(entropy + log(-V.S_energy)) / (gamma - 1);
22102b29df7SJames Wright   U.density                  = exp(log_rho);
22202b29df7SJames Wright   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * velocity[i];
22302b29df7SJames Wright 
22402b29df7SJames Wright   const CeedScalar e_internal = -gas->cv / (GasConstant(gas) * V.S_energy);
22502b29df7SJames Wright   U.E_total                   = U.density * (e_internal + e_kinetic);
22602b29df7SJames Wright   return U;
22702b29df7SJames Wright }
22802b29df7SJames Wright 
22902b29df7SJames Wright CEED_QFUNCTION_HELPER StateConservative StateConservativeFromEntropy_fwd(NewtonianIdealGasContext gas, State s, StateEntropy dV) {
23002b29df7SJames Wright   StateConservative dU;
23102b29df7SJames Wright   CeedScalar        dvelocity[3];
232eba67db8SJames Wright   StateEntropy      V = StateEntropyFromPrimitive(gas, s.Y);
233eba67db8SJames Wright   for (int i = 0; i < 3; i++) dvelocity[i] = (-dV.S_momentum[i] - s.Y.velocity[i] * dV.S_energy) / V.S_energy;
23402b29df7SJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
23502b29df7SJames Wright   const CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
23602b29df7SJames Wright   const CeedScalar de_kinetic = Dot3(dvelocity, s.Y.velocity);
237eba67db8SJames Wright   const CeedScalar entropy    = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
238eba67db8SJames Wright   const CeedScalar dentropy   = -(gamma - 1) * (dV.S_density - (de_kinetic * V.S_energy + e_kinetic * dV.S_energy));
239eba67db8SJames Wright   const CeedScalar log_rho    = -(entropy + log(-V.S_energy)) / (gamma - 1);
24002b29df7SJames Wright   const CeedScalar rho        = exp(log_rho);
241eba67db8SJames Wright   dU.density                  = -rho / (gamma - 1) * (dentropy + dV.S_energy / V.S_energy);
24202b29df7SJames Wright   for (int i = 0; i < 3; i++) dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dvelocity[i];
24302b29df7SJames Wright 
244eba67db8SJames Wright   const CeedScalar e_internal  = -gas->cv / (GasConstant(gas) * V.S_energy);
245eba67db8SJames Wright   const CeedScalar de_internal = gas->cv * dV.S_energy / (GasConstant(gas) * V.S_energy * V.S_energy);
24602b29df7SJames Wright   const CeedScalar e_total     = e_internal + e_kinetic;
24702b29df7SJames Wright   dU.E_total                   = dU.density * e_total + s.U.density * (de_internal + de_kinetic);
24802b29df7SJames Wright   return dU;
24902b29df7SJames Wright }
25002b29df7SJames Wright 
2513bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
2523bd61617SKenneth E. Jansen   StateConservative U = StateConservativeFromPrimitive(gas, Y);
253d310b3d3SAdeleke O. Bankole   State             s;
254d310b3d3SAdeleke O. Bankole   s.U = U;
255d310b3d3SAdeleke O. Bankole   s.Y = Y;
256d310b3d3SAdeleke O. Bankole   return s;
257d310b3d3SAdeleke O. Bankole }
258d310b3d3SAdeleke O. Bankole 
2593bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
2603bd61617SKenneth E. Jansen   StateConservative dU = StateConservativeFromPrimitive_fwd(gas, s, dY);
2618a94a473SJed Brown   State             ds;
2628a94a473SJed Brown   ds.U = dU;
2638a94a473SJed Brown   ds.Y = dY;
2648a94a473SJed Brown   return ds;
2658a94a473SJed Brown }
2668a94a473SJed Brown 
267c95797efSJed Brown // linear combination of n states
268c95797efSJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeMult(CeedInt n, const CeedScalar a[], const StateConservative X[]) {
269c95797efSJed Brown   StateConservative R = {0};
270c95797efSJed Brown   for (CeedInt i = 0; i < n; i++) {
271c95797efSJed Brown     R.density += a[i] * X[i].density;
272c95797efSJed Brown     for (int j = 0; j < 3; j++) R.momentum[j] += a[i] * X[i].momentum[j];
273c95797efSJed Brown     R.E_total += a[i] * X[i].E_total;
274c95797efSJed Brown   }
27584ae27ebSJed Brown   return R;
27684ae27ebSJed Brown }
27784ae27ebSJed Brown 
27884ae27ebSJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeAXPBYPCZ(CeedScalar a, StateConservative X, CeedScalar b, StateConservative Y, CeedScalar c,
27984ae27ebSJed Brown                                                                   StateConservative Z) {
28084ae27ebSJed Brown   StateConservative R;
28184ae27ebSJed Brown   R.density = a * X.density + b * Y.density + c * Z.density;
28284ae27ebSJed Brown   for (int i = 0; i < 3; i++) R.momentum[i] = a * X.momentum[i] + b * Y.momentum[i] + c * Z.momentum[i];
28384ae27ebSJed Brown   R.E_total = a * X.E_total + b * Y.E_total + c * Z.E_total;
28484ae27ebSJed Brown   return R;
28584ae27ebSJed Brown }
28684ae27ebSJed Brown 
287d310b3d3SAdeleke O. Bankole CEED_QFUNCTION_HELPER void StateToU(NewtonianIdealGasContext gas, const State input, CeedScalar U[5]) { UnpackState_U(input.U, U); }
288d310b3d3SAdeleke O. Bankole 
289d310b3d3SAdeleke O. Bankole CEED_QFUNCTION_HELPER void StateToY(NewtonianIdealGasContext gas, const State input, CeedScalar Y[5]) { UnpackState_Y(input.Y, Y); }
290f179a332SJames Wright 
291eba67db8SJames Wright CEED_QFUNCTION_HELPER void StateToV(NewtonianIdealGasContext gas, const State input, CeedScalar V[5]) {
292eba67db8SJames Wright   StateEntropy state_V = StateEntropyFromPrimitive(gas, input.Y);
293eba67db8SJames Wright   UnpackState_V(state_V, V);
294eba67db8SJames Wright }
29502b29df7SJames Wright 
296be91e165SJames Wright CEED_QFUNCTION_HELPER void StateToQ(NewtonianIdealGasContext gas, const State input, CeedScalar Q[5], StateVariable state_var) {
297be91e165SJames Wright   switch (state_var) {
298be91e165SJames Wright     case STATEVAR_CONSERVATIVE:
299be91e165SJames Wright       StateToU(gas, input, Q);
300be91e165SJames Wright       break;
301be91e165SJames Wright     case STATEVAR_PRIMITIVE:
302be91e165SJames Wright       StateToY(gas, input, Q);
303be91e165SJames Wright       break;
30402b29df7SJames Wright     case STATEVAR_ENTROPY:
30502b29df7SJames Wright       StateToV(gas, input, Q);
30602b29df7SJames Wright       break;
307be91e165SJames Wright   }
308be91e165SJames Wright }
30920840d50SJames Wright 
310eba67db8SJames Wright CEED_QFUNCTION_HELPER void StateToQ_fwd(NewtonianIdealGasContext gas, const State input, const State dinput, CeedScalar dQ[5],
311eba67db8SJames Wright                                         StateVariable state_var) {
312eba67db8SJames Wright   switch (state_var) {
313eba67db8SJames Wright     case STATEVAR_CONSERVATIVE:
314eba67db8SJames Wright     case STATEVAR_PRIMITIVE:
315eba67db8SJames Wright       StateToQ(gas, dinput, dQ, state_var);
316eba67db8SJames Wright       break;
317eba67db8SJames Wright     case STATEVAR_ENTROPY: {
318eba67db8SJames Wright       StateEntropy dstate_v;
319eba67db8SJames Wright 
320eba67db8SJames Wright       dstate_v = StateEntropyFromPrimitive_fwd(gas, input, dinput.Y);
321eba67db8SJames Wright       UnpackState_V(dstate_v, dQ);
322eba67db8SJames Wright     } break;
323eba67db8SJames Wright   }
324eba67db8SJames Wright }
325eba67db8SJames Wright 
3263bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, const CeedScalar U[5]) {
327c6e8c570SJames Wright   State s;
328c6e8c570SJames Wright   s.U.density     = U[0];
329c6e8c570SJames Wright   s.U.momentum[0] = U[1];
330c6e8c570SJames Wright   s.U.momentum[1] = U[2];
331c6e8c570SJames Wright   s.U.momentum[2] = U[3];
332c6e8c570SJames Wright   s.U.E_total     = U[4];
3333bd61617SKenneth E. Jansen   s.Y             = StatePrimitiveFromConservative(gas, s.U);
334c6e8c570SJames Wright   return s;
335c6e8c570SJames Wright }
336c6e8c570SJames Wright 
3373bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dU[5]) {
338c6e8c570SJames Wright   State ds;
339c6e8c570SJames Wright   ds.U.density     = dU[0];
340c6e8c570SJames Wright   ds.U.momentum[0] = dU[1];
341c6e8c570SJames Wright   ds.U.momentum[1] = dU[2];
342c6e8c570SJames Wright   ds.U.momentum[2] = dU[3];
343c6e8c570SJames Wright   ds.U.E_total     = dU[4];
3443bd61617SKenneth E. Jansen   ds.Y             = StatePrimitiveFromConservative_fwd(gas, s, ds.U);
345c6e8c570SJames Wright   return ds;
346c6e8c570SJames Wright }
347c6e8c570SJames Wright 
3483bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, const CeedScalar Y[5]) {
349dc805cc4SLeila Ghaffari   State s;
350dc805cc4SLeila Ghaffari   s.Y.pressure    = Y[0];
351dc805cc4SLeila Ghaffari   s.Y.velocity[0] = Y[1];
352dc805cc4SLeila Ghaffari   s.Y.velocity[1] = Y[2];
353dc805cc4SLeila Ghaffari   s.Y.velocity[2] = Y[3];
354dc805cc4SLeila Ghaffari   s.Y.temperature = Y[4];
3553bd61617SKenneth E. Jansen   s.U             = StateConservativeFromPrimitive(gas, s.Y);
356dc805cc4SLeila Ghaffari   return s;
357dc805cc4SLeila Ghaffari }
358dc805cc4SLeila Ghaffari 
3593bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dY[5]) {
360dc805cc4SLeila Ghaffari   State ds;
361dc805cc4SLeila Ghaffari   ds.Y.pressure    = dY[0];
362dc805cc4SLeila Ghaffari   ds.Y.velocity[0] = dY[1];
363dc805cc4SLeila Ghaffari   ds.Y.velocity[1] = dY[2];
364dc805cc4SLeila Ghaffari   ds.Y.velocity[2] = dY[3];
365dc805cc4SLeila Ghaffari   ds.Y.temperature = dY[4];
3663bd61617SKenneth E. Jansen   ds.U             = StateConservativeFromPrimitive_fwd(gas, s, ds.Y);
36702b29df7SJames Wright   return ds;
36802b29df7SJames Wright }
36902b29df7SJames Wright 
37002b29df7SJames Wright CEED_QFUNCTION_HELPER State StateFromV(NewtonianIdealGasContext gas, const CeedScalar V[5]) {
37102b29df7SJames Wright   State        s;
372eba67db8SJames Wright   StateEntropy state_V;
373eba67db8SJames Wright   state_V.S_density     = V[0];
374eba67db8SJames Wright   state_V.S_momentum[0] = V[1];
375eba67db8SJames Wright   state_V.S_momentum[1] = V[2];
376eba67db8SJames Wright   state_V.S_momentum[2] = V[3];
377eba67db8SJames Wright   state_V.S_energy      = V[4];
378eba67db8SJames Wright   s.U                   = StateConservativeFromEntropy(gas, state_V);
379eba67db8SJames Wright   s.Y                   = StatePrimitiveFromEntropy(gas, state_V);
38002b29df7SJames Wright   return s;
38102b29df7SJames Wright }
38202b29df7SJames Wright 
38302b29df7SJames Wright CEED_QFUNCTION_HELPER State StateFromV_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dV[5]) {
38402b29df7SJames Wright   State        ds;
385eba67db8SJames Wright   StateEntropy state_dV;
386eba67db8SJames Wright   state_dV.S_density     = dV[0];
387eba67db8SJames Wright   state_dV.S_momentum[0] = dV[1];
388eba67db8SJames Wright   state_dV.S_momentum[1] = dV[2];
389eba67db8SJames Wright   state_dV.S_momentum[2] = dV[3];
390eba67db8SJames Wright   state_dV.S_energy      = dV[4];
391eba67db8SJames Wright   ds.U                   = StateConservativeFromEntropy_fwd(gas, s, state_dV);
392eba67db8SJames Wright   ds.Y                   = StatePrimitiveFromEntropy_fwd(gas, s, state_dV);
393dc805cc4SLeila Ghaffari   return ds;
394dc805cc4SLeila Ghaffari }
395dc805cc4SLeila Ghaffari 
3963bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromQ(NewtonianIdealGasContext gas, const CeedScalar Q[5], StateVariable state_var) {
397be91e165SJames Wright   State s;
398be91e165SJames Wright   switch (state_var) {
399be91e165SJames Wright     case STATEVAR_CONSERVATIVE:
4003bd61617SKenneth E. Jansen       s = StateFromU(gas, Q);
401be91e165SJames Wright       break;
402be91e165SJames Wright     case STATEVAR_PRIMITIVE:
4033bd61617SKenneth E. Jansen       s = StateFromY(gas, Q);
404be91e165SJames Wright       break;
40502b29df7SJames Wright     case STATEVAR_ENTROPY:
40602b29df7SJames Wright       s = StateFromV(gas, Q);
40702b29df7SJames Wright       break;
408be91e165SJames Wright   }
409be91e165SJames Wright   return s;
410be91e165SJames Wright }
411be91e165SJames Wright 
41228c2e84aSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromQ_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dQ[5], StateVariable state_var) {
413be91e165SJames Wright   State ds;
414be91e165SJames Wright   switch (state_var) {
415be91e165SJames Wright     case STATEVAR_CONSERVATIVE:
4163bd61617SKenneth E. Jansen       ds = StateFromU_fwd(gas, s, dQ);
417be91e165SJames Wright       break;
418be91e165SJames Wright     case STATEVAR_PRIMITIVE:
4193bd61617SKenneth E. Jansen       ds = StateFromY_fwd(gas, s, dQ);
420be91e165SJames Wright       break;
42102b29df7SJames Wright     case STATEVAR_ENTROPY:
42202b29df7SJames Wright       ds = StateFromV_fwd(gas, s, dQ);
42302b29df7SJames Wright       break;
424be91e165SJames Wright   }
425be91e165SJames Wright   return ds;
426be91e165SJames Wright }
427be91e165SJames Wright 
4282b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, StateConservative Flux[3]) {
429c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
430c6e8c570SJames Wright     Flux[i].density = s.U.momentum[i];
4312b730f8bSJeremy 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);
432c6e8c570SJames Wright     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
433c6e8c570SJames Wright   }
434c6e8c570SJames Wright }
435c6e8c570SJames Wright 
4362b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, State s, State ds, StateConservative dFlux[3]) {
437f0a19222SJames Wright   for (CeedInt i = 0; i < 3; i++) {
438f0a19222SJames Wright     dFlux[i].density = ds.U.momentum[i];
4392b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
4402b730f8bSJeremy 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);
4412b730f8bSJeremy L Thompson     }
4422b730f8bSJeremy 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];
443f0a19222SJames Wright   }
444f0a19222SJames Wright }
445f0a19222SJames Wright 
4462b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal(NewtonianIdealGasContext gas, State s, const CeedScalar normal[3]) {
447738af36cSAdelekeBankole   StateConservative Flux[3], Flux_dot_n = {0};
448738af36cSAdelekeBankole   FluxInviscid(gas, s, Flux);
449738af36cSAdelekeBankole   for (CeedInt i = 0; i < 3; i++) {
450738af36cSAdelekeBankole     Flux_dot_n.density += Flux[i].density * normal[i];
4512b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += Flux[i].momentum[j] * normal[i];
452738af36cSAdelekeBankole     Flux_dot_n.E_total += Flux[i].E_total * normal[i];
453738af36cSAdelekeBankole   }
454738af36cSAdelekeBankole   return Flux_dot_n;
455738af36cSAdelekeBankole }
456738af36cSAdelekeBankole 
4572b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal_fwd(NewtonianIdealGasContext gas, State s, State ds, const CeedScalar normal[3]) {
458f0a19222SJames Wright   StateConservative dFlux[3], Flux_dot_n = {0};
459f0a19222SJames Wright   FluxInviscid_fwd(gas, s, ds, dFlux);
460c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
461f0a19222SJames Wright     Flux_dot_n.density += dFlux[i].density * normal[i];
4622b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += dFlux[i].momentum[j] * normal[i];
463f0a19222SJames Wright     Flux_dot_n.E_total += dFlux[i].E_total * normal[i];
464c6e8c570SJames Wright   }
465f0a19222SJames Wright   return Flux_dot_n;
466c6e8c570SJames Wright }
467c6e8c570SJames Wright 
4682b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, State s, State ds[3], CeedScalar strong_conv[5]) {
4692b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 5; i++) strong_conv[i] = 0;
4702b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 3; i++) {
4712b89d87eSLeila Ghaffari     StateConservative dF[3];
4722b89d87eSLeila Ghaffari     FluxInviscid_fwd(gas, s, ds[i], dF);
4732b89d87eSLeila Ghaffari     CeedScalar dF_i[5];
4742b89d87eSLeila Ghaffari     UnpackState_U(dF[i], dF_i);
4752b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) strong_conv[j] += dF_i[j];
4762b89d87eSLeila Ghaffari   }
4772b89d87eSLeila Ghaffari }
4782b89d87eSLeila Ghaffari 
4792b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3], CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) {
4802b89d87eSLeila Ghaffari   for (CeedInt j = 0; j < 3; j++) {
4812b89d87eSLeila Ghaffari     Flux[0][j] = F_inviscid[j].density;
4822b730f8bSJeremy L Thompson     for (CeedInt k = 0; k < 3; k++) Flux[k + 1][j] = F_inviscid[j].momentum[k] - stress[k][j];
4832b89d87eSLeila Ghaffari     Flux[4][j] = F_inviscid[j].E_total + Fe[j];
4842b89d87eSLeila Ghaffari   }
4852b89d87eSLeila Ghaffari }
4862b89d87eSLeila Ghaffari 
4872b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal_Boundary(const StateConservative F_inviscid[3], const CeedScalar stress[3][3], const CeedScalar Fe[3],
4882b730f8bSJeremy L Thompson                                               const CeedScalar normal[3], CeedScalar Flux[5]) {
4895bce47c7SJames Wright   for (CeedInt j = 0; j < 5; j++) Flux[j] = 0.;
4905bce47c7SJames Wright   for (CeedInt j = 0; j < 3; j++) {
4915bce47c7SJames Wright     Flux[0] += F_inviscid[j].density * normal[j];
4925bce47c7SJames Wright     for (CeedInt k = 0; k < 3; k++) {
4935bce47c7SJames Wright       Flux[k + 1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j];
4945bce47c7SJames Wright     }
4955bce47c7SJames Wright     Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j];
4965bce47c7SJames Wright   }
4975bce47c7SJames Wright }
4985bce47c7SJames Wright 
4998a94a473SJed Brown CEED_QFUNCTION_HELPER void FluxTotal_RiemannBoundary(const StateConservative F_inviscid_normal, const CeedScalar stress[3][3], const CeedScalar Fe[3],
5008a94a473SJed Brown                                                      const CeedScalar normal[3], CeedScalar Flux[5]) {
5018a94a473SJed Brown   Flux[0] = F_inviscid_normal.density;
5028a94a473SJed Brown   for (CeedInt k = 0; k < 3; k++) Flux[k + 1] = F_inviscid_normal.momentum[k];
5038a94a473SJed Brown   Flux[4] = F_inviscid_normal.E_total;
5048a94a473SJed Brown   for (CeedInt j = 0; j < 3; j++) {
5058a94a473SJed Brown     for (CeedInt k = 0; k < 3; k++) {
5068a94a473SJed Brown       Flux[k + 1] -= stress[k][j] * normal[j];
5078a94a473SJed Brown     }
5088a94a473SJed Brown     Flux[4] += Fe[j] * normal[j];
5098a94a473SJed Brown   }
5108a94a473SJed Brown }
5118a94a473SJed Brown 
512d08fcc28SJames Wright CEED_QFUNCTION_HELPER void VelocityGradient(const State grad_s[3], CeedScalar grad_velocity[3][3]) {
513d08fcc28SJames Wright   grad_velocity[0][0] = grad_s[0].Y.velocity[0];
514d08fcc28SJames Wright   grad_velocity[0][1] = grad_s[1].Y.velocity[0];
515d08fcc28SJames Wright   grad_velocity[0][2] = grad_s[2].Y.velocity[0];
516d08fcc28SJames Wright   grad_velocity[1][0] = grad_s[0].Y.velocity[1];
517d08fcc28SJames Wright   grad_velocity[1][1] = grad_s[1].Y.velocity[1];
518d08fcc28SJames Wright   grad_velocity[1][2] = grad_s[2].Y.velocity[1];
519d08fcc28SJames Wright   grad_velocity[2][0] = grad_s[0].Y.velocity[2];
520d08fcc28SJames Wright   grad_velocity[2][1] = grad_s[1].Y.velocity[2];
521d08fcc28SJames Wright   grad_velocity[2][2] = grad_s[2].Y.velocity[2];
522d08fcc28SJames Wright }
523d08fcc28SJames Wright 
524d08fcc28SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const CeedScalar grad_velocity[3][3], CeedScalar strain_rate[6]) {
525d08fcc28SJames Wright   const CeedScalar weight = 1 / sqrt(2.);  // Really sqrt(2.) / 2
526d08fcc28SJames Wright   strain_rate[0]          = grad_velocity[0][0];
527d08fcc28SJames Wright   strain_rate[1]          = grad_velocity[1][1];
528d08fcc28SJames Wright   strain_rate[2]          = grad_velocity[2][2];
529d08fcc28SJames Wright   strain_rate[3]          = weight * (grad_velocity[1][2] + grad_velocity[2][1]);
530d08fcc28SJames Wright   strain_rate[4]          = weight * (grad_velocity[0][2] + grad_velocity[2][0]);
531d08fcc28SJames Wright   strain_rate[5]          = weight * (grad_velocity[0][1] + grad_velocity[1][0]);
532d08fcc28SJames Wright }
533d08fcc28SJames Wright 
534c6e8c570SJames Wright // Kelvin-Mandel notation
535d08fcc28SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate_State(const State grad_s[3], CeedScalar strain_rate[6]) {
536d08fcc28SJames Wright   CeedScalar grad_velocity[3][3];
537d08fcc28SJames Wright   VelocityGradient(grad_s, grad_velocity);
538d08fcc28SJames Wright   KMStrainRate(grad_velocity, strain_rate);
539d08fcc28SJames Wright }
540d08fcc28SJames Wright 
5417618d7b7SJames Wright //@brief Given velocity gradient du_i/dx_j, return 0.5*(du_i/dx_j - du_j/dx_i)
542d08fcc28SJames Wright CEED_QFUNCTION_HELPER void RotationRate(const CeedScalar grad_velocity[3][3], CeedScalar rotation_rate[3][3]) {
543d08fcc28SJames Wright   rotation_rate[0][0] = 0;
544d08fcc28SJames Wright   rotation_rate[1][1] = 0;
545d08fcc28SJames Wright   rotation_rate[2][2] = 0;
546d08fcc28SJames Wright   rotation_rate[1][2] = 0.5 * (grad_velocity[1][2] - grad_velocity[2][1]);
547d08fcc28SJames Wright   rotation_rate[0][2] = 0.5 * (grad_velocity[0][2] - grad_velocity[2][0]);
548d08fcc28SJames Wright   rotation_rate[0][1] = 0.5 * (grad_velocity[0][1] - grad_velocity[1][0]);
549d08fcc28SJames Wright   rotation_rate[2][1] = -rotation_rate[1][2];
550d08fcc28SJames Wright   rotation_rate[2][0] = -rotation_rate[0][2];
551d08fcc28SJames Wright   rotation_rate[1][0] = -rotation_rate[0][1];
552c6e8c570SJames Wright }
553c6e8c570SJames Wright 
5542b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, const CeedScalar strain_rate[6], CeedScalar stress[6]) {
555c6e8c570SJames Wright   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
556c6e8c570SJames Wright   for (CeedInt i = 0; i < 6; i++) {
557c6e8c570SJames Wright     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
558c6e8c570SJames Wright   }
559c6e8c570SJames Wright }
560c6e8c570SJames Wright 
5612b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
562c6e8c570SJames Wright                                              CeedScalar Fe[3]) {
563c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
5642b730f8bSJeremy 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;
565c6e8c570SJames Wright   }
566c6e8c570SJames Wright }
567c6e8c570SJames Wright 
5682b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
5692b730f8bSJeremy L Thompson                                                  const CeedScalar stress[3][3], const CeedScalar dstress[3][3], CeedScalar dFe[3]) {
570c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
5712b730f8bSJeremy 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] -
5722b730f8bSJeremy L Thompson              Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] - gas->k * grad_ds[i].Y.temperature;
573c6e8c570SJames Wright   }
574c6e8c570SJames Wright }
575c6e8c570SJames Wright 
576d08fcc28SJames Wright CEED_QFUNCTION_HELPER void Vorticity(const State grad_s[3], CeedScalar vorticity[3]) {
577d08fcc28SJames Wright   CeedScalar grad_velocity[3][3];
578d08fcc28SJames Wright   VelocityGradient(grad_s, grad_velocity);
579d08fcc28SJames Wright   Curl3(grad_velocity, vorticity);
580d08fcc28SJames Wright }
581d08fcc28SJames Wright 
58228c2e84aSKenneth E. Jansen CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s, StateVariable state_var,
58328c2e84aSKenneth E. Jansen                                                               const CeedScalar *grad_q, const CeedScalar dXdx[3][3], State grad_s[3]) {
5849b6a821dSJames Wright   for (CeedInt k = 0; k < 3; k++) {
5853bd61617SKenneth E. Jansen     CeedScalar dqi[5];
5869b6a821dSJames Wright     for (CeedInt j = 0; j < 5; j++) {
5879b6a821dSJames Wright       dqi[j] =
5889b6a821dSJames 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];
5899b6a821dSJames Wright     }
5903bd61617SKenneth E. Jansen     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
5919b6a821dSJames Wright   }
5929b6a821dSJames Wright }
5939b6a821dSJames Wright 
5949b6a821dSJames Wright CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_Boundary(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s,
59528c2e84aSKenneth E. Jansen                                                                        StateVariable state_var, const CeedScalar *grad_q, const CeedScalar dXdx[2][3],
59628c2e84aSKenneth E. Jansen                                                                        State grad_s[3]) {
5979b6a821dSJames Wright   for (CeedInt k = 0; k < 3; k++) {
5983bd61617SKenneth E. Jansen     CeedScalar dqi[5];
5999b6a821dSJames Wright     for (CeedInt j = 0; j < 5; j++) {
6009b6a821dSJames 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];
6019b6a821dSJames Wright     }
6023bd61617SKenneth E. Jansen     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
6039b6a821dSJames Wright   }
6049b6a821dSJames Wright }
605