xref: /libCEED/examples/fluids/qfunctions/newtonian_state.h (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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 
12c0b5abf0SJeremy L Thompson #include <ceed/types.h>
13c0b5abf0SJeremy L Thompson #ifndef CEED_RUNNING_JIT_PASS
14c9c2c079SJeremy L Thompson #include <math.h>
15c0b5abf0SJeremy L Thompson #endif
162b730f8bSJeremy L Thompson 
17c6e8c570SJames Wright #include "newtonian_types.h"
1813fa47b2SJames Wright #include "utils.h"
19c6e8c570SJames Wright 
20c6e8c570SJames Wright typedef struct {
21c6e8c570SJames Wright   CeedScalar density;
22c6e8c570SJames Wright   CeedScalar momentum[3];
23c6e8c570SJames Wright   CeedScalar E_total;
24c6e8c570SJames Wright } StateConservative;
25c6e8c570SJames Wright 
26c6e8c570SJames Wright typedef struct {
27c6e8c570SJames Wright   StateConservative U;
28c6e8c570SJames Wright   StatePrimitive    Y;
29c6e8c570SJames Wright } State;
30c6e8c570SJames Wright 
UnpackState_U(StateConservative s,CeedScalar U[5])312b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_U(StateConservative s, CeedScalar U[5]) {
322b89d87eSLeila Ghaffari   U[0] = s.density;
332b89d87eSLeila Ghaffari   for (int i = 0; i < 3; i++) U[i + 1] = s.momentum[i];
342b89d87eSLeila Ghaffari   U[4] = s.E_total;
352b89d87eSLeila Ghaffari }
362b89d87eSLeila Ghaffari 
UnpackState_Y(StatePrimitive s,CeedScalar Y[5])372b89d87eSLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) {
382b89d87eSLeila Ghaffari   Y[0] = s.pressure;
392b89d87eSLeila Ghaffari   for (int i = 0; i < 3; i++) Y[i + 1] = s.velocity[i];
402b89d87eSLeila Ghaffari   Y[4] = s.temperature;
412b89d87eSLeila Ghaffari }
422b89d87eSLeila Ghaffari 
UnpackState_V(StateEntropy s,CeedScalar V[5])4302b29df7SJames Wright CEED_QFUNCTION_HELPER void UnpackState_V(StateEntropy s, CeedScalar V[5]) {
4402b29df7SJames Wright   V[0] = s.S_density;
4502b29df7SJames Wright   for (int i = 0; i < 3; i++) V[i + 1] = s.S_momentum[i];
4602b29df7SJames Wright   V[4] = s.S_energy;
4702b29df7SJames Wright }
4802b29df7SJames Wright 
HeatCapacityRatio(NewtonianIdealGasContext gas)492b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio(NewtonianIdealGasContext gas) { return gas->cp / gas->cv; }
502518f336SLeila Ghaffari 
GasConstant(NewtonianIdealGasContext gas)512b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar GasConstant(NewtonianIdealGasContext gas) { return gas->cp - gas->cv; }
522518f336SLeila Ghaffari 
Prandtl(NewtonianIdealGasContext gas)532b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) { return gas->cp * gas->mu / gas->k; }
542518f336SLeila Ghaffari 
SoundSpeed(NewtonianIdealGasContext gas,CeedScalar T)552b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas, CeedScalar T) { return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T); }
562518f336SLeila Ghaffari 
Mach(NewtonianIdealGasContext gas,CeedScalar T,CeedScalar u)572b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas, CeedScalar T, CeedScalar u) { return u / SoundSpeed(gas, T); }
582518f336SLeila Ghaffari 
TotalSpecificEnthalpy(NewtonianIdealGasContext gas,const State s)592b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy(NewtonianIdealGasContext gas, const State s) {
60f0a19222SJames Wright   CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
616fa2c4daSJames Wright   CeedScalar e_internal = gas->cv * s.Y.temperature;
62f0a19222SJames Wright   return e_internal + e_kinetic + s.Y.pressure / s.U.density;
63f0a19222SJames Wright }
64f0a19222SJames Wright 
TotalSpecificEnthalpy_fwd(NewtonianIdealGasContext gas,const State s,const State ds)652b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy_fwd(NewtonianIdealGasContext gas, const State s, const State ds) {
66f0a19222SJames Wright   CeedScalar de_kinetic  = Dot3(ds.Y.velocity, s.Y.velocity);
67f0a19222SJames Wright   CeedScalar de_internal = gas->cv * ds.Y.temperature;
682b730f8bSJeremy L Thompson   return de_internal + de_kinetic + ds.Y.pressure / s.U.density - s.Y.pressure / Square(s.U.density) * ds.U.density;
69f0a19222SJames Wright }
70f0a19222SJames Wright 
StatePrimitiveFromConservative(NewtonianIdealGasContext gas,StateConservative U)713bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(NewtonianIdealGasContext gas, StateConservative U) {
72c6e8c570SJames Wright   StatePrimitive Y;
73c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) Y.velocity[i] = U.momentum[i] / U.density;
74c6e8c570SJames Wright   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
75c6e8c570SJames Wright   CeedScalar e_total    = U.E_total / U.density;
763bd61617SKenneth E. Jansen   CeedScalar e_internal = e_total - e_kinetic;
77c6e8c570SJames Wright   Y.temperature         = e_internal / gas->cv;
782518f336SLeila Ghaffari   Y.pressure            = (HeatCapacityRatio(gas) - 1) * U.density * e_internal;
79c6e8c570SJames Wright   return Y;
80c6e8c570SJames Wright }
81c6e8c570SJames Wright 
StatePrimitiveFromConservative_fwd(NewtonianIdealGasContext gas,State s,StateConservative dU)823bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) {
83c6e8c570SJames Wright   StatePrimitive dY;
84c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
85c6e8c570SJames Wright     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
86c6e8c570SJames Wright   }
87c6e8c570SJames Wright   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
88c6e8c570SJames Wright   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
89c6e8c570SJames Wright   CeedScalar e_total     = s.U.E_total / s.U.density;
90c6e8c570SJames Wright   CeedScalar de_total    = (dU.E_total - e_total * dU.density) / s.U.density;
913bd61617SKenneth E. Jansen   CeedScalar e_internal  = e_total - e_kinetic;
923bd61617SKenneth E. Jansen   CeedScalar de_internal = de_total - de_kinetic;
93c6e8c570SJames Wright   dY.temperature         = de_internal / gas->cv;
942b730f8bSJeremy L Thompson   dY.pressure            = (HeatCapacityRatio(gas) - 1) * (dU.density * e_internal + s.U.density * de_internal);
95c6e8c570SJames Wright   return dY;
96c6e8c570SJames Wright }
97c6e8c570SJames Wright 
StateEntropyFromPrimitive(NewtonianIdealGasContext gas,StatePrimitive Y)98eba67db8SJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
99eba67db8SJames Wright   StateEntropy     V;
100eba67db8SJames Wright   const CeedScalar gamma     = HeatCapacityRatio(gas);
101eba67db8SJames Wright   const CeedScalar rho       = Y.pressure / (GasConstant(gas) * Y.temperature);
102eba67db8SJames Wright   const CeedScalar entropy   = log(Y.pressure) - gamma * log(rho);
103eba67db8SJames Wright   const CeedScalar rho_div_p = rho / Y.pressure;
104eba67db8SJames Wright   const CeedScalar e_kinetic = 0.5 * Dot3(Y.velocity, Y.velocity);
105eba67db8SJames Wright 
106eba67db8SJames Wright   V.S_density = (gamma - entropy) / (gamma - 1) - rho_div_p * e_kinetic;
107eba67db8SJames Wright   for (int i = 0; i < 3; i++) V.S_momentum[i] = rho_div_p * Y.velocity[i];
108eba67db8SJames Wright   V.S_energy = -rho_div_p;
109eba67db8SJames Wright   return V;
110eba67db8SJames Wright }
111eba67db8SJames Wright 
StateEntropyFromPrimitive_fwd(NewtonianIdealGasContext gas,State s,StatePrimitive dY)112eba67db8SJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
113eba67db8SJames Wright   StateEntropy     dV;
114eba67db8SJames Wright   const CeedScalar gamma = HeatCapacityRatio(gas);
115eba67db8SJames Wright   CeedScalar       drho  = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
116eba67db8SJames Wright 
117eba67db8SJames Wright   const CeedScalar e_kinetic  = .5 * Dot3(s.Y.velocity, s.Y.velocity);
118eba67db8SJames Wright   const CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
119eba67db8SJames Wright   const CeedScalar rho_div_p  = s.U.density / s.Y.pressure;
120eba67db8SJames Wright   const CeedScalar drho_div_p = (drho * s.Y.pressure - s.U.density * dY.pressure) / Square(s.Y.pressure);
121eba67db8SJames Wright 
122eba67db8SJames Wright   CeedScalar dentropy = dY.pressure / s.Y.pressure - gamma * drho / s.U.density;
123eba67db8SJames Wright 
124eba67db8SJames Wright   dV.S_density = -dentropy / (gamma - 1) - de_kinetic * rho_div_p - e_kinetic * drho_div_p;
125eba67db8SJames 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];
126eba67db8SJames Wright   dV.S_energy = -drho_div_p;
127eba67db8SJames Wright   return dV;
128eba67db8SJames Wright }
129eba67db8SJames Wright 
StatePrimitiveFromEntropy(NewtonianIdealGasContext gas,StateEntropy V)130eba67db8SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromEntropy(NewtonianIdealGasContext gas, StateEntropy V) {
131eba67db8SJames Wright   StatePrimitive Y;
132eba67db8SJames Wright   for (int i = 0; i < 3; i++) Y.velocity[i] = -V.S_momentum[i] / V.S_energy;
133eba67db8SJames Wright   Y.temperature              = -1 / (GasConstant(gas) * V.S_energy);
134eba67db8SJames Wright   const CeedScalar gamma     = HeatCapacityRatio(gas);
135eba67db8SJames Wright   const CeedScalar e_kinetic = 0.5 * Dot3(Y.velocity, Y.velocity);
136eba67db8SJames Wright   const CeedScalar entropy   = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
137eba67db8SJames Wright   const CeedScalar log_P     = -(entropy + gamma * log(-V.S_energy)) / (gamma - 1);
138eba67db8SJames Wright   Y.pressure                 = exp(log_P);
139eba67db8SJames Wright   return Y;
140eba67db8SJames Wright }
141eba67db8SJames Wright 
StatePrimitiveFromEntropy_fwd(NewtonianIdealGasContext gas,State s,StateEntropy dV)142eba67db8SJames Wright CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromEntropy_fwd(NewtonianIdealGasContext gas, State s, StateEntropy dV) {
143eba67db8SJames Wright   StatePrimitive dY;
144eba67db8SJames Wright   StateEntropy   V = StateEntropyFromPrimitive(gas, s.Y);
145eba67db8SJames 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;
146eba67db8SJames Wright   dY.temperature              = dV.S_energy / (GasConstant(gas) * V.S_energy * V.S_energy);
147eba67db8SJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
148eba67db8SJames Wright   const CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
149eba67db8SJames Wright   const CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
150eba67db8SJames Wright   const CeedScalar dentropy   = (1 - gamma) * (dV.S_density - e_kinetic * dV.S_energy - de_kinetic * V.S_energy);
151eba67db8SJames Wright   dY.pressure                 = s.Y.pressure * (-dentropy - gamma * dV.S_energy / V.S_energy) / (gamma - 1);
152eba67db8SJames Wright   return dY;
153eba67db8SJames Wright }
154eba67db8SJames Wright 
StateConservativeFromPrimitive(NewtonianIdealGasContext gas,StatePrimitive Y)1553bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
156dc805cc4SLeila Ghaffari   StateConservative U;
1572518f336SLeila Ghaffari   U.density = Y.pressure / (GasConstant(gas) * Y.temperature);
158dc805cc4SLeila Ghaffari   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * Y.velocity[i];
159dc805cc4SLeila Ghaffari   CeedScalar e_internal = gas->cv * Y.temperature;
160dc805cc4SLeila Ghaffari   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
1613bd61617SKenneth E. Jansen   CeedScalar e_total    = e_internal + e_kinetic;
162dc805cc4SLeila Ghaffari   U.E_total             = U.density * e_total;
163dc805cc4SLeila Ghaffari   return U;
164dc805cc4SLeila Ghaffari }
165dc805cc4SLeila Ghaffari 
StateConservativeFromPrimitive_fwd(NewtonianIdealGasContext gas,State s,StatePrimitive dY)1663bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
167dc805cc4SLeila Ghaffari   StateConservative dU;
1682b730f8bSJeremy L Thompson   dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
169dc805cc4SLeila Ghaffari   for (int i = 0; i < 3; i++) {
170dc805cc4SLeila Ghaffari     dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i];
171dc805cc4SLeila Ghaffari   }
172dc805cc4SLeila Ghaffari   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
173dc805cc4SLeila Ghaffari   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
174dc805cc4SLeila Ghaffari   CeedScalar e_internal  = gas->cv * s.Y.temperature;
175dc805cc4SLeila Ghaffari   CeedScalar de_internal = gas->cv * dY.temperature;
1763bd61617SKenneth E. Jansen   CeedScalar e_total     = e_internal + e_kinetic;
1773bd61617SKenneth E. Jansen   CeedScalar de_total    = de_internal + de_kinetic;
178dc805cc4SLeila Ghaffari   dU.E_total             = dU.density * e_total + s.U.density * de_total;
179dc805cc4SLeila Ghaffari   return dU;
180dc805cc4SLeila Ghaffari }
181dc805cc4SLeila Ghaffari 
StateEntropyFromConservative(NewtonianIdealGasContext gas,StateConservative U)18202b29df7SJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromConservative(NewtonianIdealGasContext gas, StateConservative U) {
18302b29df7SJames Wright   StateEntropy     V;
18402b29df7SJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
18502b29df7SJames Wright   const CeedScalar e_kinetic  = .5 * Dot3(U.momentum, U.momentum) / U.density;
18602b29df7SJames Wright   const CeedScalar e_internal = U.E_total - e_kinetic;
18702b29df7SJames Wright   const CeedScalar p          = (gamma - 1) * e_internal;
18802b29df7SJames Wright   const CeedScalar entropy    = log(p) - gamma * log(U.density);
18902b29df7SJames Wright 
19002b29df7SJames Wright   V.S_density = (gamma - entropy) / (gamma - 1) - e_kinetic / p;
19102b29df7SJames Wright   for (int i = 0; i < 3; i++) V.S_momentum[i] = U.momentum[i] / p;
19202b29df7SJames Wright   V.S_energy = -U.density / p;
19302b29df7SJames Wright   return V;
19402b29df7SJames Wright }
19502b29df7SJames Wright 
StateEntropyFromConservative_fwd(NewtonianIdealGasContext gas,State s,StateConservative dU)19602b29df7SJames Wright CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) {
19702b29df7SJames Wright   StateEntropy     dV;
19802b29df7SJames Wright   const CeedScalar gamma       = HeatCapacityRatio(gas);
19902b29df7SJames Wright   const CeedScalar e_kinetic   = .5 * Dot3(s.U.momentum, s.U.momentum) / s.U.density;
20002b29df7SJames Wright   const CeedScalar de_kinetic  = (Dot3(s.U.momentum, dU.momentum) - e_kinetic * dU.density) / s.U.density;
20102b29df7SJames Wright   const CeedScalar de_internal = dU.E_total - de_kinetic;
20202b29df7SJames Wright   const CeedScalar p           = s.Y.pressure;
20302b29df7SJames Wright   const CeedScalar dp          = (gamma - 1) * de_internal;
20402b29df7SJames Wright 
20502b29df7SJames Wright   CeedScalar dentropy = dp / p - gamma * dU.density / s.U.density;
20602b29df7SJames Wright 
20702b29df7SJames Wright   dV.S_density = -dentropy / (gamma - 1) - de_kinetic / p + dp * e_kinetic / Square(p);
20802b29df7SJames Wright   for (CeedInt i = 0; i < 3; i++) {
20902b29df7SJames Wright     dV.S_momentum[i] = (dU.momentum[i] - s.U.momentum[i] * dp / p) / p;
21002b29df7SJames Wright   }
21102b29df7SJames Wright   dV.S_energy = -(dU.density - s.U.density * dp / p) / p;
21202b29df7SJames Wright   return dV;
21302b29df7SJames Wright }
21402b29df7SJames Wright 
StateConservativeFromEntropy(NewtonianIdealGasContext gas,StateEntropy V)21502b29df7SJames Wright CEED_QFUNCTION_HELPER StateConservative StateConservativeFromEntropy(NewtonianIdealGasContext gas, StateEntropy V) {
21602b29df7SJames Wright   StateConservative U;
21702b29df7SJames Wright   CeedScalar        velocity[3];
21802b29df7SJames Wright   for (int i = 0; i < 3; i++) velocity[i] = -V.S_momentum[i] / V.S_energy;
21902b29df7SJames Wright   const CeedScalar gamma     = HeatCapacityRatio(gas);
22002b29df7SJames Wright   const CeedScalar e_kinetic = 0.5 * Dot3(velocity, velocity);
22102b29df7SJames Wright   const CeedScalar entropy   = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
22202b29df7SJames Wright   const CeedScalar log_rho   = -(entropy + log(-V.S_energy)) / (gamma - 1);
22302b29df7SJames Wright   U.density                  = exp(log_rho);
22402b29df7SJames Wright   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * velocity[i];
22502b29df7SJames Wright 
22602b29df7SJames Wright   const CeedScalar e_internal = -gas->cv / (GasConstant(gas) * V.S_energy);
22702b29df7SJames Wright   U.E_total                   = U.density * (e_internal + e_kinetic);
22802b29df7SJames Wright   return U;
22902b29df7SJames Wright }
23002b29df7SJames Wright 
StateConservativeFromEntropy_fwd(NewtonianIdealGasContext gas,State s,StateEntropy dV)23102b29df7SJames Wright CEED_QFUNCTION_HELPER StateConservative StateConservativeFromEntropy_fwd(NewtonianIdealGasContext gas, State s, StateEntropy dV) {
23202b29df7SJames Wright   StateConservative dU;
23302b29df7SJames Wright   CeedScalar        dvelocity[3];
234eba67db8SJames Wright   StateEntropy      V = StateEntropyFromPrimitive(gas, s.Y);
235eba67db8SJames Wright   for (int i = 0; i < 3; i++) dvelocity[i] = (-dV.S_momentum[i] - s.Y.velocity[i] * dV.S_energy) / V.S_energy;
23602b29df7SJames Wright   const CeedScalar gamma      = HeatCapacityRatio(gas);
23702b29df7SJames Wright   const CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
23802b29df7SJames Wright   const CeedScalar de_kinetic = Dot3(dvelocity, s.Y.velocity);
239eba67db8SJames Wright   const CeedScalar entropy    = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
240eba67db8SJames Wright   const CeedScalar dentropy   = -(gamma - 1) * (dV.S_density - (de_kinetic * V.S_energy + e_kinetic * dV.S_energy));
241eba67db8SJames Wright   const CeedScalar log_rho    = -(entropy + log(-V.S_energy)) / (gamma - 1);
24202b29df7SJames Wright   const CeedScalar rho        = exp(log_rho);
243eba67db8SJames Wright   dU.density                  = -rho / (gamma - 1) * (dentropy + dV.S_energy / V.S_energy);
24402b29df7SJames Wright   for (int i = 0; i < 3; i++) dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dvelocity[i];
24502b29df7SJames Wright 
246eba67db8SJames Wright   const CeedScalar e_internal  = -gas->cv / (GasConstant(gas) * V.S_energy);
247eba67db8SJames Wright   const CeedScalar de_internal = gas->cv * dV.S_energy / (GasConstant(gas) * V.S_energy * V.S_energy);
24802b29df7SJames Wright   const CeedScalar e_total     = e_internal + e_kinetic;
24902b29df7SJames Wright   dU.E_total                   = dU.density * e_total + s.U.density * (de_internal + de_kinetic);
25002b29df7SJames Wright   return dU;
25102b29df7SJames Wright }
25202b29df7SJames Wright 
StateFromPrimitive(NewtonianIdealGasContext gas,StatePrimitive Y)2533bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
2543bd61617SKenneth E. Jansen   StateConservative U = StateConservativeFromPrimitive(gas, Y);
255d310b3d3SAdeleke O. Bankole   State             s;
256d310b3d3SAdeleke O. Bankole   s.U = U;
257d310b3d3SAdeleke O. Bankole   s.Y = Y;
258d310b3d3SAdeleke O. Bankole   return s;
259d310b3d3SAdeleke O. Bankole }
260d310b3d3SAdeleke O. Bankole 
StateFromPrimitive_fwd(NewtonianIdealGasContext gas,State s,StatePrimitive dY)2613bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
2623bd61617SKenneth E. Jansen   StateConservative dU = StateConservativeFromPrimitive_fwd(gas, s, dY);
2638a94a473SJed Brown   State             ds;
2648a94a473SJed Brown   ds.U = dU;
2658a94a473SJed Brown   ds.Y = dY;
2668a94a473SJed Brown   return ds;
2678a94a473SJed Brown }
2688a94a473SJed Brown 
269c95797efSJed Brown // linear combination of n states
StateConservativeMult(CeedInt n,const CeedScalar a[],const StateConservative X[])270c95797efSJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeMult(CeedInt n, const CeedScalar a[], const StateConservative X[]) {
271c95797efSJed Brown   StateConservative R = {0};
272c95797efSJed Brown   for (CeedInt i = 0; i < n; i++) {
273c95797efSJed Brown     R.density += a[i] * X[i].density;
274c95797efSJed Brown     for (int j = 0; j < 3; j++) R.momentum[j] += a[i] * X[i].momentum[j];
275c95797efSJed Brown     R.E_total += a[i] * X[i].E_total;
276c95797efSJed Brown   }
27784ae27ebSJed Brown   return R;
27884ae27ebSJed Brown }
27984ae27ebSJed Brown 
StateConservativeAXPBYPCZ(CeedScalar a,StateConservative X,CeedScalar b,StateConservative Y,CeedScalar c,StateConservative Z)28084ae27ebSJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeAXPBYPCZ(CeedScalar a, StateConservative X, CeedScalar b, StateConservative Y, CeedScalar c,
28184ae27ebSJed Brown                                                                   StateConservative Z) {
28284ae27ebSJed Brown   StateConservative R;
28384ae27ebSJed Brown   R.density = a * X.density + b * Y.density + c * Z.density;
28484ae27ebSJed Brown   for (int i = 0; i < 3; i++) R.momentum[i] = a * X.momentum[i] + b * Y.momentum[i] + c * Z.momentum[i];
28584ae27ebSJed Brown   R.E_total = a * X.E_total + b * Y.E_total + c * Z.E_total;
28684ae27ebSJed Brown   return R;
28784ae27ebSJed Brown }
28884ae27ebSJed Brown 
StateToU(NewtonianIdealGasContext gas,const State input,CeedScalar U[5])289d310b3d3SAdeleke O. Bankole CEED_QFUNCTION_HELPER void StateToU(NewtonianIdealGasContext gas, const State input, CeedScalar U[5]) { UnpackState_U(input.U, U); }
290d310b3d3SAdeleke O. Bankole 
StateToY(NewtonianIdealGasContext gas,const State input,CeedScalar Y[5])291d310b3d3SAdeleke O. Bankole CEED_QFUNCTION_HELPER void StateToY(NewtonianIdealGasContext gas, const State input, CeedScalar Y[5]) { UnpackState_Y(input.Y, Y); }
292f179a332SJames Wright 
StateToV(NewtonianIdealGasContext gas,const State input,CeedScalar V[5])293eba67db8SJames Wright CEED_QFUNCTION_HELPER void StateToV(NewtonianIdealGasContext gas, const State input, CeedScalar V[5]) {
294eba67db8SJames Wright   StateEntropy state_V = StateEntropyFromPrimitive(gas, input.Y);
295eba67db8SJames Wright   UnpackState_V(state_V, V);
296eba67db8SJames Wright }
29702b29df7SJames Wright 
StateToQ(NewtonianIdealGasContext gas,const State input,CeedScalar Q[5],StateVariable state_var)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;
30602b29df7SJames Wright     case STATEVAR_ENTROPY:
30702b29df7SJames Wright       StateToV(gas, input, Q);
30802b29df7SJames Wright       break;
309be91e165SJames Wright   }
310be91e165SJames Wright }
31120840d50SJames Wright 
StateToQ_fwd(NewtonianIdealGasContext gas,const State input,const State dinput,CeedScalar dQ[5],StateVariable state_var)312eba67db8SJames Wright CEED_QFUNCTION_HELPER void StateToQ_fwd(NewtonianIdealGasContext gas, const State input, const State dinput, CeedScalar dQ[5],
313eba67db8SJames Wright                                         StateVariable state_var) {
314eba67db8SJames Wright   switch (state_var) {
315eba67db8SJames Wright     case STATEVAR_CONSERVATIVE:
316eba67db8SJames Wright     case STATEVAR_PRIMITIVE:
317eba67db8SJames Wright       StateToQ(gas, dinput, dQ, state_var);
318eba67db8SJames Wright       break;
319eba67db8SJames Wright     case STATEVAR_ENTROPY: {
320eba67db8SJames Wright       StateEntropy dstate_v;
321eba67db8SJames Wright 
322eba67db8SJames Wright       dstate_v = StateEntropyFromPrimitive_fwd(gas, input, dinput.Y);
323eba67db8SJames Wright       UnpackState_V(dstate_v, dQ);
324eba67db8SJames Wright     } break;
325eba67db8SJames Wright   }
326eba67db8SJames Wright }
327eba67db8SJames Wright 
StateFromU(NewtonianIdealGasContext gas,const CeedScalar U[5])3283bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, const CeedScalar U[5]) {
329c6e8c570SJames Wright   State s;
330c6e8c570SJames Wright   s.U.density     = U[0];
331c6e8c570SJames Wright   s.U.momentum[0] = U[1];
332c6e8c570SJames Wright   s.U.momentum[1] = U[2];
333c6e8c570SJames Wright   s.U.momentum[2] = U[3];
334c6e8c570SJames Wright   s.U.E_total     = U[4];
3353bd61617SKenneth E. Jansen   s.Y             = StatePrimitiveFromConservative(gas, s.U);
336c6e8c570SJames Wright   return s;
337c6e8c570SJames Wright }
338c6e8c570SJames Wright 
StateFromU_fwd(NewtonianIdealGasContext gas,State s,const CeedScalar dU[5])3393bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dU[5]) {
340c6e8c570SJames Wright   State ds;
341c6e8c570SJames Wright   ds.U.density     = dU[0];
342c6e8c570SJames Wright   ds.U.momentum[0] = dU[1];
343c6e8c570SJames Wright   ds.U.momentum[1] = dU[2];
344c6e8c570SJames Wright   ds.U.momentum[2] = dU[3];
345c6e8c570SJames Wright   ds.U.E_total     = dU[4];
3463bd61617SKenneth E. Jansen   ds.Y             = StatePrimitiveFromConservative_fwd(gas, s, ds.U);
347c6e8c570SJames Wright   return ds;
348c6e8c570SJames Wright }
349c6e8c570SJames Wright 
StateFromY(NewtonianIdealGasContext gas,const CeedScalar Y[5])3503bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, const CeedScalar Y[5]) {
351dc805cc4SLeila Ghaffari   State s;
352dc805cc4SLeila Ghaffari   s.Y.pressure    = Y[0];
353dc805cc4SLeila Ghaffari   s.Y.velocity[0] = Y[1];
354dc805cc4SLeila Ghaffari   s.Y.velocity[1] = Y[2];
355dc805cc4SLeila Ghaffari   s.Y.velocity[2] = Y[3];
356dc805cc4SLeila Ghaffari   s.Y.temperature = Y[4];
3573bd61617SKenneth E. Jansen   s.U             = StateConservativeFromPrimitive(gas, s.Y);
358dc805cc4SLeila Ghaffari   return s;
359dc805cc4SLeila Ghaffari }
360dc805cc4SLeila Ghaffari 
StateFromY_fwd(NewtonianIdealGasContext gas,State s,const CeedScalar dY[5])3613bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dY[5]) {
362dc805cc4SLeila Ghaffari   State ds;
363dc805cc4SLeila Ghaffari   ds.Y.pressure    = dY[0];
364dc805cc4SLeila Ghaffari   ds.Y.velocity[0] = dY[1];
365dc805cc4SLeila Ghaffari   ds.Y.velocity[1] = dY[2];
366dc805cc4SLeila Ghaffari   ds.Y.velocity[2] = dY[3];
367dc805cc4SLeila Ghaffari   ds.Y.temperature = dY[4];
3683bd61617SKenneth E. Jansen   ds.U             = StateConservativeFromPrimitive_fwd(gas, s, ds.Y);
36902b29df7SJames Wright   return ds;
37002b29df7SJames Wright }
37102b29df7SJames Wright 
StateFromV(NewtonianIdealGasContext gas,const CeedScalar V[5])37202b29df7SJames Wright CEED_QFUNCTION_HELPER State StateFromV(NewtonianIdealGasContext gas, const CeedScalar V[5]) {
37302b29df7SJames Wright   State        s;
374eba67db8SJames Wright   StateEntropy state_V;
375eba67db8SJames Wright   state_V.S_density     = V[0];
376eba67db8SJames Wright   state_V.S_momentum[0] = V[1];
377eba67db8SJames Wright   state_V.S_momentum[1] = V[2];
378eba67db8SJames Wright   state_V.S_momentum[2] = V[3];
379eba67db8SJames Wright   state_V.S_energy      = V[4];
380eba67db8SJames Wright   s.U                   = StateConservativeFromEntropy(gas, state_V);
381eba67db8SJames Wright   s.Y                   = StatePrimitiveFromEntropy(gas, state_V);
38202b29df7SJames Wright   return s;
38302b29df7SJames Wright }
38402b29df7SJames Wright 
StateFromV_fwd(NewtonianIdealGasContext gas,State s,const CeedScalar dV[5])38502b29df7SJames Wright CEED_QFUNCTION_HELPER State StateFromV_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dV[5]) {
38602b29df7SJames Wright   State        ds;
387eba67db8SJames Wright   StateEntropy state_dV;
388eba67db8SJames Wright   state_dV.S_density     = dV[0];
389eba67db8SJames Wright   state_dV.S_momentum[0] = dV[1];
390eba67db8SJames Wright   state_dV.S_momentum[1] = dV[2];
391eba67db8SJames Wright   state_dV.S_momentum[2] = dV[3];
392eba67db8SJames Wright   state_dV.S_energy      = dV[4];
393eba67db8SJames Wright   ds.U                   = StateConservativeFromEntropy_fwd(gas, s, state_dV);
394eba67db8SJames Wright   ds.Y                   = StatePrimitiveFromEntropy_fwd(gas, s, state_dV);
395dc805cc4SLeila Ghaffari   return ds;
396dc805cc4SLeila Ghaffari }
397dc805cc4SLeila Ghaffari 
StateFromQ(NewtonianIdealGasContext gas,const CeedScalar Q[5],StateVariable state_var)3983bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromQ(NewtonianIdealGasContext gas, const CeedScalar Q[5], StateVariable state_var) {
399be91e165SJames Wright   State s;
400be91e165SJames Wright   switch (state_var) {
401be91e165SJames Wright     case STATEVAR_CONSERVATIVE:
4023bd61617SKenneth E. Jansen       s = StateFromU(gas, Q);
403be91e165SJames Wright       break;
404be91e165SJames Wright     case STATEVAR_PRIMITIVE:
4053bd61617SKenneth E. Jansen       s = StateFromY(gas, Q);
406be91e165SJames Wright       break;
40702b29df7SJames Wright     case STATEVAR_ENTROPY:
40802b29df7SJames Wright       s = StateFromV(gas, Q);
40902b29df7SJames Wright       break;
410be91e165SJames Wright   }
411be91e165SJames Wright   return s;
412be91e165SJames Wright }
413be91e165SJames Wright 
StateFromQ_fwd(NewtonianIdealGasContext gas,State s,const CeedScalar dQ[5],StateVariable state_var)41428c2e84aSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromQ_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dQ[5], StateVariable state_var) {
415be91e165SJames Wright   State ds;
416be91e165SJames Wright   switch (state_var) {
417be91e165SJames Wright     case STATEVAR_CONSERVATIVE:
4183bd61617SKenneth E. Jansen       ds = StateFromU_fwd(gas, s, dQ);
419be91e165SJames Wright       break;
420be91e165SJames Wright     case STATEVAR_PRIMITIVE:
4213bd61617SKenneth E. Jansen       ds = StateFromY_fwd(gas, s, dQ);
422be91e165SJames Wright       break;
42302b29df7SJames Wright     case STATEVAR_ENTROPY:
42402b29df7SJames Wright       ds = StateFromV_fwd(gas, s, dQ);
42502b29df7SJames Wright       break;
426be91e165SJames Wright   }
427be91e165SJames Wright   return ds;
428be91e165SJames Wright }
429be91e165SJames Wright 
FluxInviscid(NewtonianIdealGasContext gas,State s,StateConservative Flux[3])4302b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, StateConservative Flux[3]) {
431c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
432c6e8c570SJames Wright     Flux[i].density = s.U.momentum[i];
4332b730f8bSJeremy 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);
434c6e8c570SJames Wright     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
435c6e8c570SJames Wright   }
436c6e8c570SJames Wright }
437c6e8c570SJames Wright 
FluxInviscid_fwd(NewtonianIdealGasContext gas,State s,State ds,StateConservative dFlux[3])4382b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, State s, State ds, StateConservative dFlux[3]) {
439f0a19222SJames Wright   for (CeedInt i = 0; i < 3; i++) {
440f0a19222SJames Wright     dFlux[i].density = ds.U.momentum[i];
4412b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
4422b730f8bSJeremy 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);
4432b730f8bSJeremy L Thompson     }
4442b730f8bSJeremy 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];
445f0a19222SJames Wright   }
446f0a19222SJames Wright }
447f0a19222SJames Wright 
FluxInviscidDotNormal(NewtonianIdealGasContext gas,State s,const CeedScalar normal[3])4482b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal(NewtonianIdealGasContext gas, State s, const CeedScalar normal[3]) {
449738af36cSAdelekeBankole   StateConservative Flux[3], Flux_dot_n = {0};
450738af36cSAdelekeBankole   FluxInviscid(gas, s, Flux);
451738af36cSAdelekeBankole   for (CeedInt i = 0; i < 3; i++) {
452738af36cSAdelekeBankole     Flux_dot_n.density += Flux[i].density * normal[i];
4532b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += Flux[i].momentum[j] * normal[i];
454738af36cSAdelekeBankole     Flux_dot_n.E_total += Flux[i].E_total * normal[i];
455738af36cSAdelekeBankole   }
456738af36cSAdelekeBankole   return Flux_dot_n;
457738af36cSAdelekeBankole }
458738af36cSAdelekeBankole 
FluxInviscidDotNormal_fwd(NewtonianIdealGasContext gas,State s,State ds,const CeedScalar normal[3])4592b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal_fwd(NewtonianIdealGasContext gas, State s, State ds, const CeedScalar normal[3]) {
460f0a19222SJames Wright   StateConservative dFlux[3], Flux_dot_n = {0};
461f0a19222SJames Wright   FluxInviscid_fwd(gas, s, ds, dFlux);
462c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
463f0a19222SJames Wright     Flux_dot_n.density += dFlux[i].density * normal[i];
4642b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += dFlux[i].momentum[j] * normal[i];
465f0a19222SJames Wright     Flux_dot_n.E_total += dFlux[i].E_total * normal[i];
466c6e8c570SJames Wright   }
467f0a19222SJames Wright   return Flux_dot_n;
468c6e8c570SJames Wright }
469c6e8c570SJames Wright 
FluxInviscidStrong(NewtonianIdealGasContext gas,State s,State ds[3],CeedScalar strong_conv[5])4702b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, State s, State ds[3], CeedScalar strong_conv[5]) {
4712b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 5; i++) strong_conv[i] = 0;
4722b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 3; i++) {
4732b89d87eSLeila Ghaffari     StateConservative dF[3];
4742b89d87eSLeila Ghaffari     FluxInviscid_fwd(gas, s, ds[i], dF);
4752b89d87eSLeila Ghaffari     CeedScalar dF_i[5];
4762b89d87eSLeila Ghaffari     UnpackState_U(dF[i], dF_i);
4772b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) strong_conv[j] += dF_i[j];
4782b89d87eSLeila Ghaffari   }
4792b89d87eSLeila Ghaffari }
4802b89d87eSLeila Ghaffari 
FluxTotal(const StateConservative F_inviscid[3],CeedScalar stress[3][3],CeedScalar Fe[3],CeedScalar Flux[5][3])4812b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3], CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) {
4822b89d87eSLeila Ghaffari   for (CeedInt j = 0; j < 3; j++) {
4832b89d87eSLeila Ghaffari     Flux[0][j] = F_inviscid[j].density;
4842b730f8bSJeremy L Thompson     for (CeedInt k = 0; k < 3; k++) Flux[k + 1][j] = F_inviscid[j].momentum[k] - stress[k][j];
4852b89d87eSLeila Ghaffari     Flux[4][j] = F_inviscid[j].E_total + Fe[j];
4862b89d87eSLeila Ghaffari   }
4872b89d87eSLeila Ghaffari }
4882b89d87eSLeila Ghaffari 
FluxTotal_Boundary(const StateConservative F_inviscid[3],const CeedScalar stress[3][3],const CeedScalar Fe[3],const CeedScalar normal[3],CeedScalar Flux[5])4892b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal_Boundary(const StateConservative F_inviscid[3], const CeedScalar stress[3][3], const CeedScalar Fe[3],
4902b730f8bSJeremy L Thompson                                               const CeedScalar normal[3], CeedScalar Flux[5]) {
4915bce47c7SJames Wright   for (CeedInt j = 0; j < 5; j++) Flux[j] = 0.;
4925bce47c7SJames Wright   for (CeedInt j = 0; j < 3; j++) {
4935bce47c7SJames Wright     Flux[0] += F_inviscid[j].density * normal[j];
4945bce47c7SJames Wright     for (CeedInt k = 0; k < 3; k++) {
4955bce47c7SJames Wright       Flux[k + 1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j];
4965bce47c7SJames Wright     }
4975bce47c7SJames Wright     Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j];
4985bce47c7SJames Wright   }
4995bce47c7SJames Wright }
5005bce47c7SJames Wright 
FluxTotal_RiemannBoundary(const StateConservative F_inviscid_normal,const CeedScalar stress[3][3],const CeedScalar Fe[3],const CeedScalar normal[3],CeedScalar Flux[5])5018a94a473SJed Brown CEED_QFUNCTION_HELPER void FluxTotal_RiemannBoundary(const StateConservative F_inviscid_normal, const CeedScalar stress[3][3], const CeedScalar Fe[3],
5028a94a473SJed Brown                                                      const CeedScalar normal[3], CeedScalar Flux[5]) {
5038a94a473SJed Brown   Flux[0] = F_inviscid_normal.density;
5048a94a473SJed Brown   for (CeedInt k = 0; k < 3; k++) Flux[k + 1] = F_inviscid_normal.momentum[k];
5058a94a473SJed Brown   Flux[4] = F_inviscid_normal.E_total;
5068a94a473SJed Brown   for (CeedInt j = 0; j < 3; j++) {
5078a94a473SJed Brown     for (CeedInt k = 0; k < 3; k++) {
5088a94a473SJed Brown       Flux[k + 1] -= stress[k][j] * normal[j];
5098a94a473SJed Brown     }
5108a94a473SJed Brown     Flux[4] += Fe[j] * normal[j];
5118a94a473SJed Brown   }
5128a94a473SJed Brown }
5138a94a473SJed Brown 
VelocityGradient(const State grad_s[3],CeedScalar grad_velocity[3][3])514d08fcc28SJames Wright CEED_QFUNCTION_HELPER void VelocityGradient(const State grad_s[3], CeedScalar grad_velocity[3][3]) {
515d08fcc28SJames Wright   grad_velocity[0][0] = grad_s[0].Y.velocity[0];
516d08fcc28SJames Wright   grad_velocity[0][1] = grad_s[1].Y.velocity[0];
517d08fcc28SJames Wright   grad_velocity[0][2] = grad_s[2].Y.velocity[0];
518d08fcc28SJames Wright   grad_velocity[1][0] = grad_s[0].Y.velocity[1];
519d08fcc28SJames Wright   grad_velocity[1][1] = grad_s[1].Y.velocity[1];
520d08fcc28SJames Wright   grad_velocity[1][2] = grad_s[2].Y.velocity[1];
521d08fcc28SJames Wright   grad_velocity[2][0] = grad_s[0].Y.velocity[2];
522d08fcc28SJames Wright   grad_velocity[2][1] = grad_s[1].Y.velocity[2];
523d08fcc28SJames Wright   grad_velocity[2][2] = grad_s[2].Y.velocity[2];
524d08fcc28SJames Wright }
525d08fcc28SJames Wright 
KMStrainRate(const CeedScalar grad_velocity[3][3],CeedScalar strain_rate[6])526d08fcc28SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const CeedScalar grad_velocity[3][3], CeedScalar strain_rate[6]) {
527d08fcc28SJames Wright   const CeedScalar weight = 1 / sqrt(2.);  // Really sqrt(2.) / 2
528d08fcc28SJames Wright   strain_rate[0]          = grad_velocity[0][0];
529d08fcc28SJames Wright   strain_rate[1]          = grad_velocity[1][1];
530d08fcc28SJames Wright   strain_rate[2]          = grad_velocity[2][2];
531d08fcc28SJames Wright   strain_rate[3]          = weight * (grad_velocity[1][2] + grad_velocity[2][1]);
532d08fcc28SJames Wright   strain_rate[4]          = weight * (grad_velocity[0][2] + grad_velocity[2][0]);
533d08fcc28SJames Wright   strain_rate[5]          = weight * (grad_velocity[0][1] + grad_velocity[1][0]);
534d08fcc28SJames Wright }
535d08fcc28SJames Wright 
536c6e8c570SJames Wright // Kelvin-Mandel notation
KMStrainRate_State(const State grad_s[3],CeedScalar strain_rate[6])537d08fcc28SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate_State(const State grad_s[3], CeedScalar strain_rate[6]) {
538d08fcc28SJames Wright   CeedScalar grad_velocity[3][3];
539d08fcc28SJames Wright   VelocityGradient(grad_s, grad_velocity);
540d08fcc28SJames Wright   KMStrainRate(grad_velocity, strain_rate);
541d08fcc28SJames Wright }
542d08fcc28SJames Wright 
5437618d7b7SJames Wright //@brief Given velocity gradient du_i/dx_j, return 0.5*(du_i/dx_j - du_j/dx_i)
RotationRate(const CeedScalar grad_velocity[3][3],CeedScalar rotation_rate[3][3])544d08fcc28SJames Wright CEED_QFUNCTION_HELPER void RotationRate(const CeedScalar grad_velocity[3][3], CeedScalar rotation_rate[3][3]) {
545d08fcc28SJames Wright   rotation_rate[0][0] = 0;
546d08fcc28SJames Wright   rotation_rate[1][1] = 0;
547d08fcc28SJames Wright   rotation_rate[2][2] = 0;
548d08fcc28SJames Wright   rotation_rate[1][2] = 0.5 * (grad_velocity[1][2] - grad_velocity[2][1]);
549d08fcc28SJames Wright   rotation_rate[0][2] = 0.5 * (grad_velocity[0][2] - grad_velocity[2][0]);
550d08fcc28SJames Wright   rotation_rate[0][1] = 0.5 * (grad_velocity[0][1] - grad_velocity[1][0]);
551d08fcc28SJames Wright   rotation_rate[2][1] = -rotation_rate[1][2];
552d08fcc28SJames Wright   rotation_rate[2][0] = -rotation_rate[0][2];
553d08fcc28SJames Wright   rotation_rate[1][0] = -rotation_rate[0][1];
554c6e8c570SJames Wright }
555c6e8c570SJames Wright 
NewtonianStress(NewtonianIdealGasContext gas,const CeedScalar strain_rate[6],CeedScalar stress[6])5562b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, const CeedScalar strain_rate[6], CeedScalar stress[6]) {
557c6e8c570SJames Wright   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
558c6e8c570SJames Wright   for (CeedInt i = 0; i < 6; i++) {
559c6e8c570SJames Wright     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
560c6e8c570SJames Wright   }
561c6e8c570SJames Wright }
562c6e8c570SJames Wright 
ViscousEnergyFlux(NewtonianIdealGasContext gas,StatePrimitive Y,const State grad_s[3],const CeedScalar stress[3][3],CeedScalar Fe[3])5632b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
564c6e8c570SJames Wright                                              CeedScalar Fe[3]) {
565c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
5662b730f8bSJeremy 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;
567c6e8c570SJames Wright   }
568c6e8c570SJames Wright }
569c6e8c570SJames Wright 
ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas,StatePrimitive Y,StatePrimitive dY,const State grad_ds[3],const CeedScalar stress[3][3],const CeedScalar dstress[3][3],CeedScalar dFe[3])5702b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
5712b730f8bSJeremy L Thompson                                                  const CeedScalar stress[3][3], const CeedScalar dstress[3][3], CeedScalar dFe[3]) {
572c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
5732b730f8bSJeremy 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] -
5742b730f8bSJeremy L Thompson              Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] - gas->k * grad_ds[i].Y.temperature;
575c6e8c570SJames Wright   }
576c6e8c570SJames Wright }
577c6e8c570SJames Wright 
Vorticity(const State grad_s[3],CeedScalar vorticity[3])578d08fcc28SJames Wright CEED_QFUNCTION_HELPER void Vorticity(const State grad_s[3], CeedScalar vorticity[3]) {
579d08fcc28SJames Wright   CeedScalar grad_velocity[3][3];
580d08fcc28SJames Wright   VelocityGradient(grad_s, grad_velocity);
581d08fcc28SJames Wright   Curl3(grad_velocity, vorticity);
582d08fcc28SJames Wright }
583d08fcc28SJames Wright 
StatePhysicalGradientFromReference(CeedInt Q,CeedInt i,NewtonianIdealGasContext gas,State s,StateVariable state_var,const CeedScalar * grad_q,const CeedScalar dXdx[3][3],State grad_s[3])58428c2e84aSKenneth E. Jansen CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s, StateVariable state_var,
58528c2e84aSKenneth E. Jansen                                                               const CeedScalar *grad_q, const CeedScalar dXdx[3][3], State grad_s[3]) {
5869b6a821dSJames Wright   for (CeedInt k = 0; k < 3; k++) {
5873bd61617SKenneth E. Jansen     CeedScalar dqi[5];
5889b6a821dSJames Wright     for (CeedInt j = 0; j < 5; j++) {
5899b6a821dSJames Wright       dqi[j] =
5909b6a821dSJames 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];
5919b6a821dSJames Wright     }
5923bd61617SKenneth E. Jansen     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
5939b6a821dSJames Wright   }
5949b6a821dSJames Wright }
5959b6a821dSJames Wright 
StatePhysicalGradientFromReference_Boundary(CeedInt Q,CeedInt i,NewtonianIdealGasContext gas,State s,StateVariable state_var,const CeedScalar * grad_q,const CeedScalar dXdx[2][3],State grad_s[3])5969b6a821dSJames Wright CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_Boundary(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s,
59728c2e84aSKenneth E. Jansen                                                                        StateVariable state_var, const CeedScalar *grad_q, const CeedScalar dXdx[2][3],
59828c2e84aSKenneth E. Jansen                                                                        State grad_s[3]) {
5999b6a821dSJames Wright   for (CeedInt k = 0; k < 3; k++) {
6003bd61617SKenneth E. Jansen     CeedScalar dqi[5];
6019b6a821dSJames Wright     for (CeedInt j = 0; j < 5; j++) {
6029b6a821dSJames 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];
6039b6a821dSJames Wright     }
6043bd61617SKenneth E. Jansen     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
6059b6a821dSJames Wright   }
6069b6a821dSJames Wright }
607