xref: /honee/qfunctions/newtonian_state.h (revision c7ece6efd17014bd7b01fc517a8c82707db4fa34)
1dc936754SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2475b2820SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3475b2820SJames Wright //
4475b2820SJames Wright // SPDX-License-Identifier: BSD-2-Clause
5475b2820SJames Wright //
6475b2820SJames Wright // This file is part of CEED:  http://github.com/ceed
7475b2820SJames Wright 
8475b2820SJames Wright /// @file
9475b2820SJames Wright /// Structs and helper functions regarding the state of a newtonian simulation
10*c7ece6efSJeremy L Thompson #pragma once
11475b2820SJames Wright 
12475b2820SJames Wright #include <ceed.h>
13d0cce58aSJeremy L Thompson #include <math.h>
142b916ea7SJeremy L Thompson 
15475b2820SJames Wright #include "newtonian_types.h"
16704b8bbeSJames Wright #include "utils.h"
17475b2820SJames Wright 
18475b2820SJames Wright typedef struct {
19475b2820SJames Wright   CeedScalar density;
20475b2820SJames Wright   CeedScalar momentum[3];
21475b2820SJames Wright   CeedScalar E_total;
22475b2820SJames Wright } StateConservative;
23475b2820SJames Wright 
24475b2820SJames Wright typedef struct {
25475b2820SJames Wright   StateConservative U;
26475b2820SJames Wright   StatePrimitive    Y;
27475b2820SJames Wright } State;
28475b2820SJames Wright 
29d1b9ef12SLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_U(StateConservative s, CeedScalar U[5]) {
30d1b9ef12SLeila Ghaffari   U[0] = s.density;
31d1b9ef12SLeila Ghaffari   for (int i = 0; i < 3; i++) U[i + 1] = s.momentum[i];
32d1b9ef12SLeila Ghaffari   U[4] = s.E_total;
33d1b9ef12SLeila Ghaffari }
34d1b9ef12SLeila Ghaffari 
35d1b9ef12SLeila Ghaffari CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) {
36d1b9ef12SLeila Ghaffari   Y[0] = s.pressure;
37d1b9ef12SLeila Ghaffari   for (int i = 0; i < 3; i++) Y[i + 1] = s.velocity[i];
38d1b9ef12SLeila Ghaffari   Y[4] = s.temperature;
39d1b9ef12SLeila Ghaffari }
40d1b9ef12SLeila Ghaffari 
412b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio(NewtonianIdealGasContext gas) { return gas->cp / gas->cv; }
42e0d1a4dfSLeila Ghaffari 
432b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar GasConstant(NewtonianIdealGasContext gas) { return gas->cp - gas->cv; }
44e0d1a4dfSLeila Ghaffari 
452b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) { return gas->cp * gas->mu / gas->k; }
46e0d1a4dfSLeila Ghaffari 
472b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas, CeedScalar T) { return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T); }
48e0d1a4dfSLeila Ghaffari 
492b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas, CeedScalar T, CeedScalar u) { return u / SoundSpeed(gas, T); }
50e0d1a4dfSLeila Ghaffari 
512b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy(NewtonianIdealGasContext gas, const State s) {
52c50f56e5SJames Wright   // Ignoring potential energy
53c50f56e5SJames Wright   CeedScalar e_internal = gas->cv * s.Y.temperature;
54c50f56e5SJames Wright   CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
55c50f56e5SJames Wright   return e_internal + e_kinetic + s.Y.pressure / s.U.density;
56c50f56e5SJames Wright }
57c50f56e5SJames Wright 
582b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy_fwd(NewtonianIdealGasContext gas, const State s, const State ds) {
59c50f56e5SJames Wright   // Ignoring potential energy
60c50f56e5SJames Wright   CeedScalar de_kinetic  = Dot3(ds.Y.velocity, s.Y.velocity);
61c50f56e5SJames Wright   CeedScalar de_internal = gas->cv * ds.Y.temperature;
622b916ea7SJeremy L Thompson   return de_internal + de_kinetic + ds.Y.pressure / s.U.density - s.Y.pressure / Square(s.U.density) * ds.U.density;
63c50f56e5SJames Wright }
64c50f56e5SJames Wright 
65edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(NewtonianIdealGasContext gas, StateConservative U) {
66475b2820SJames Wright   StatePrimitive Y;
67475b2820SJames Wright   for (CeedInt i = 0; i < 3; i++) Y.velocity[i] = U.momentum[i] / U.density;
68475b2820SJames Wright   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
69475b2820SJames Wright   CeedScalar e_total    = U.E_total / U.density;
70edcfef1bSKenneth E. Jansen   CeedScalar e_internal = e_total - e_kinetic;
71475b2820SJames Wright   Y.temperature         = e_internal / gas->cv;
72e0d1a4dfSLeila Ghaffari   Y.pressure            = (HeatCapacityRatio(gas) - 1) * U.density * e_internal;
73475b2820SJames Wright   return Y;
74475b2820SJames Wright }
75475b2820SJames Wright 
76edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) {
77475b2820SJames Wright   StatePrimitive dY;
78475b2820SJames Wright   for (CeedInt i = 0; i < 3; i++) {
79475b2820SJames Wright     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
80475b2820SJames Wright   }
81475b2820SJames Wright   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
82475b2820SJames Wright   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
83475b2820SJames Wright   CeedScalar e_total     = s.U.E_total / s.U.density;
84475b2820SJames Wright   CeedScalar de_total    = (dU.E_total - e_total * dU.density) / s.U.density;
85edcfef1bSKenneth E. Jansen   CeedScalar e_internal  = e_total - e_kinetic;
86edcfef1bSKenneth E. Jansen   CeedScalar de_internal = de_total - de_kinetic;
87475b2820SJames Wright   dY.temperature         = de_internal / gas->cv;
882b916ea7SJeremy L Thompson   dY.pressure            = (HeatCapacityRatio(gas) - 1) * (dU.density * e_internal + s.U.density * de_internal);
89475b2820SJames Wright   return dY;
90475b2820SJames Wright }
91475b2820SJames Wright 
92edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
93cbe60e31SLeila Ghaffari   StateConservative U;
94e0d1a4dfSLeila Ghaffari   U.density = Y.pressure / (GasConstant(gas) * Y.temperature);
95cbe60e31SLeila Ghaffari   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * Y.velocity[i];
96cbe60e31SLeila Ghaffari   CeedScalar e_internal = gas->cv * Y.temperature;
97cbe60e31SLeila Ghaffari   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
98edcfef1bSKenneth E. Jansen   CeedScalar e_total    = e_internal + e_kinetic;
99cbe60e31SLeila Ghaffari   U.E_total             = U.density * e_total;
100cbe60e31SLeila Ghaffari   return U;
101cbe60e31SLeila Ghaffari }
102cbe60e31SLeila Ghaffari 
103edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
104cbe60e31SLeila Ghaffari   StateConservative dU;
1052b916ea7SJeremy L Thompson   dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
106cbe60e31SLeila Ghaffari   for (int i = 0; i < 3; i++) {
107cbe60e31SLeila Ghaffari     dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i];
108cbe60e31SLeila Ghaffari   }
109cbe60e31SLeila Ghaffari   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
110cbe60e31SLeila Ghaffari   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
111cbe60e31SLeila Ghaffari   CeedScalar e_internal  = gas->cv * s.Y.temperature;
112cbe60e31SLeila Ghaffari   CeedScalar de_internal = gas->cv * dY.temperature;
113edcfef1bSKenneth E. Jansen   CeedScalar e_total     = e_internal + e_kinetic;
114edcfef1bSKenneth E. Jansen   CeedScalar de_total    = de_internal + de_kinetic;
115cbe60e31SLeila Ghaffari   dU.E_total             = dU.density * e_total + s.U.density * de_total;
116cbe60e31SLeila Ghaffari   return dU;
117cbe60e31SLeila Ghaffari }
118cbe60e31SLeila Ghaffari 
119edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
120edcfef1bSKenneth E. Jansen   StateConservative U = StateConservativeFromPrimitive(gas, Y);
121b8fb7609SAdeleke O. Bankole   State             s;
122b8fb7609SAdeleke O. Bankole   s.U = U;
123b8fb7609SAdeleke O. Bankole   s.Y = Y;
124b8fb7609SAdeleke O. Bankole   return s;
125b8fb7609SAdeleke O. Bankole }
126b8fb7609SAdeleke O. Bankole 
127edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
128edcfef1bSKenneth E. Jansen   StateConservative dU = StateConservativeFromPrimitive_fwd(gas, s, dY);
129c8c30d87SJed Brown   State             ds;
130c8c30d87SJed Brown   ds.U = dU;
131c8c30d87SJed Brown   ds.Y = dY;
132c8c30d87SJed Brown   return ds;
133c8c30d87SJed Brown }
134c8c30d87SJed Brown 
135c2c93676SJed Brown // linear combination of n states
136c2c93676SJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeMult(CeedInt n, const CeedScalar a[], const StateConservative X[]) {
137c2c93676SJed Brown   StateConservative R = {0};
138c2c93676SJed Brown   for (CeedInt i = 0; i < n; i++) {
139c2c93676SJed Brown     R.density += a[i] * X[i].density;
140c2c93676SJed Brown     for (int j = 0; j < 3; j++) R.momentum[j] += a[i] * X[i].momentum[j];
141c2c93676SJed Brown     R.E_total += a[i] * X[i].E_total;
142c2c93676SJed Brown   }
143dbb62a19SJed Brown   return R;
144dbb62a19SJed Brown }
145dbb62a19SJed Brown 
146dbb62a19SJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeAXPBYPCZ(CeedScalar a, StateConservative X, CeedScalar b, StateConservative Y, CeedScalar c,
147dbb62a19SJed Brown                                                                   StateConservative Z) {
148dbb62a19SJed Brown   StateConservative R;
149dbb62a19SJed Brown   R.density = a * X.density + b * Y.density + c * Z.density;
150dbb62a19SJed Brown   for (int i = 0; i < 3; i++) R.momentum[i] = a * X.momentum[i] + b * Y.momentum[i] + c * Z.momentum[i];
151dbb62a19SJed Brown   R.E_total = a * X.E_total + b * Y.E_total + c * Z.E_total;
152dbb62a19SJed Brown   return R;
153dbb62a19SJed Brown }
154dbb62a19SJed Brown 
155b8fb7609SAdeleke O. Bankole CEED_QFUNCTION_HELPER void StateToU(NewtonianIdealGasContext gas, const State input, CeedScalar U[5]) { UnpackState_U(input.U, U); }
156b8fb7609SAdeleke O. Bankole 
157b8fb7609SAdeleke O. Bankole CEED_QFUNCTION_HELPER void StateToY(NewtonianIdealGasContext gas, const State input, CeedScalar Y[5]) { UnpackState_Y(input.Y, Y); }
158a8bca8acSJames Wright 
1598fff8293SJames Wright CEED_QFUNCTION_HELPER void StateToQ(NewtonianIdealGasContext gas, const State input, CeedScalar Q[5], StateVariable state_var) {
1608fff8293SJames Wright   switch (state_var) {
1618fff8293SJames Wright     case STATEVAR_CONSERVATIVE:
1628fff8293SJames Wright       StateToU(gas, input, Q);
1638fff8293SJames Wright       break;
1648fff8293SJames Wright     case STATEVAR_PRIMITIVE:
1658fff8293SJames Wright       StateToY(gas, input, Q);
1668fff8293SJames Wright       break;
1678fff8293SJames Wright   }
1688fff8293SJames Wright }
169d4559bbeSJames Wright 
170edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, const CeedScalar U[5]) {
171475b2820SJames Wright   State s;
172475b2820SJames Wright   s.U.density     = U[0];
173475b2820SJames Wright   s.U.momentum[0] = U[1];
174475b2820SJames Wright   s.U.momentum[1] = U[2];
175475b2820SJames Wright   s.U.momentum[2] = U[3];
176475b2820SJames Wright   s.U.E_total     = U[4];
177edcfef1bSKenneth E. Jansen   s.Y             = StatePrimitiveFromConservative(gas, s.U);
178475b2820SJames Wright   return s;
179475b2820SJames Wright }
180475b2820SJames Wright 
181edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dU[5]) {
182475b2820SJames Wright   State ds;
183475b2820SJames Wright   ds.U.density     = dU[0];
184475b2820SJames Wright   ds.U.momentum[0] = dU[1];
185475b2820SJames Wright   ds.U.momentum[1] = dU[2];
186475b2820SJames Wright   ds.U.momentum[2] = dU[3];
187475b2820SJames Wright   ds.U.E_total     = dU[4];
188edcfef1bSKenneth E. Jansen   ds.Y             = StatePrimitiveFromConservative_fwd(gas, s, ds.U);
189475b2820SJames Wright   return ds;
190475b2820SJames Wright }
191475b2820SJames Wright 
192edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, const CeedScalar Y[5]) {
193cbe60e31SLeila Ghaffari   State s;
194cbe60e31SLeila Ghaffari   s.Y.pressure    = Y[0];
195cbe60e31SLeila Ghaffari   s.Y.velocity[0] = Y[1];
196cbe60e31SLeila Ghaffari   s.Y.velocity[1] = Y[2];
197cbe60e31SLeila Ghaffari   s.Y.velocity[2] = Y[3];
198cbe60e31SLeila Ghaffari   s.Y.temperature = Y[4];
199edcfef1bSKenneth E. Jansen   s.U             = StateConservativeFromPrimitive(gas, s.Y);
200cbe60e31SLeila Ghaffari   return s;
201cbe60e31SLeila Ghaffari }
202cbe60e31SLeila Ghaffari 
203edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dY[5]) {
204cbe60e31SLeila Ghaffari   State ds;
205cbe60e31SLeila Ghaffari   ds.Y.pressure    = dY[0];
206cbe60e31SLeila Ghaffari   ds.Y.velocity[0] = dY[1];
207cbe60e31SLeila Ghaffari   ds.Y.velocity[1] = dY[2];
208cbe60e31SLeila Ghaffari   ds.Y.velocity[2] = dY[3];
209cbe60e31SLeila Ghaffari   ds.Y.temperature = dY[4];
210edcfef1bSKenneth E. Jansen   ds.U             = StateConservativeFromPrimitive_fwd(gas, s, ds.Y);
211cbe60e31SLeila Ghaffari   return ds;
212cbe60e31SLeila Ghaffari }
213cbe60e31SLeila Ghaffari 
214edcfef1bSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromQ(NewtonianIdealGasContext gas, const CeedScalar Q[5], StateVariable state_var) {
2158fff8293SJames Wright   State s;
2168fff8293SJames Wright   switch (state_var) {
2178fff8293SJames Wright     case STATEVAR_CONSERVATIVE:
218edcfef1bSKenneth E. Jansen       s = StateFromU(gas, Q);
2198fff8293SJames Wright       break;
2208fff8293SJames Wright     case STATEVAR_PRIMITIVE:
221edcfef1bSKenneth E. Jansen       s = StateFromY(gas, Q);
2228fff8293SJames Wright       break;
2238fff8293SJames Wright   }
2248fff8293SJames Wright   return s;
2258fff8293SJames Wright }
2268fff8293SJames Wright 
2272e7597b6SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromQ_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dQ[5], StateVariable state_var) {
2288fff8293SJames Wright   State ds;
2298fff8293SJames Wright   switch (state_var) {
2308fff8293SJames Wright     case STATEVAR_CONSERVATIVE:
231edcfef1bSKenneth E. Jansen       ds = StateFromU_fwd(gas, s, dQ);
2328fff8293SJames Wright       break;
2338fff8293SJames Wright     case STATEVAR_PRIMITIVE:
234edcfef1bSKenneth E. Jansen       ds = StateFromY_fwd(gas, s, dQ);
2358fff8293SJames Wright       break;
2368fff8293SJames Wright   }
2378fff8293SJames Wright   return ds;
2388fff8293SJames Wright }
2398fff8293SJames Wright 
2402b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, StateConservative Flux[3]) {
241475b2820SJames Wright   for (CeedInt i = 0; i < 3; i++) {
242475b2820SJames Wright     Flux[i].density = s.U.momentum[i];
2432b916ea7SJeremy 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);
244475b2820SJames Wright     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
245475b2820SJames Wright   }
246475b2820SJames Wright }
247475b2820SJames Wright 
2482b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, State s, State ds, StateConservative dFlux[3]) {
249c50f56e5SJames Wright   for (CeedInt i = 0; i < 3; i++) {
250c50f56e5SJames Wright     dFlux[i].density = ds.U.momentum[i];
2512b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
2522b916ea7SJeremy 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);
2532b916ea7SJeremy L Thompson     }
2542b916ea7SJeremy 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];
255c50f56e5SJames Wright   }
256c50f56e5SJames Wright }
257c50f56e5SJames Wright 
2582b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal(NewtonianIdealGasContext gas, State s, const CeedScalar normal[3]) {
2597b530f2aSAdelekeBankole   StateConservative Flux[3], Flux_dot_n = {0};
2607b530f2aSAdelekeBankole   FluxInviscid(gas, s, Flux);
2617b530f2aSAdelekeBankole   for (CeedInt i = 0; i < 3; i++) {
2627b530f2aSAdelekeBankole     Flux_dot_n.density += Flux[i].density * normal[i];
2632b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += Flux[i].momentum[j] * normal[i];
2647b530f2aSAdelekeBankole     Flux_dot_n.E_total += Flux[i].E_total * normal[i];
2657b530f2aSAdelekeBankole   }
2667b530f2aSAdelekeBankole   return Flux_dot_n;
2677b530f2aSAdelekeBankole }
2687b530f2aSAdelekeBankole 
2692b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal_fwd(NewtonianIdealGasContext gas, State s, State ds, const CeedScalar normal[3]) {
270c50f56e5SJames Wright   StateConservative dFlux[3], Flux_dot_n = {0};
271c50f56e5SJames Wright   FluxInviscid_fwd(gas, s, ds, dFlux);
272475b2820SJames Wright   for (CeedInt i = 0; i < 3; i++) {
273c50f56e5SJames Wright     Flux_dot_n.density += dFlux[i].density * normal[i];
2742b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += dFlux[i].momentum[j] * normal[i];
275c50f56e5SJames Wright     Flux_dot_n.E_total += dFlux[i].E_total * normal[i];
276475b2820SJames Wright   }
277c50f56e5SJames Wright   return Flux_dot_n;
278475b2820SJames Wright }
279475b2820SJames Wright 
2802b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, State s, State ds[3], CeedScalar strong_conv[5]) {
281d1b9ef12SLeila Ghaffari   for (CeedInt i = 0; i < 5; i++) strong_conv[i] = 0;
282d1b9ef12SLeila Ghaffari   for (CeedInt i = 0; i < 3; i++) {
283d1b9ef12SLeila Ghaffari     StateConservative dF[3];
284d1b9ef12SLeila Ghaffari     FluxInviscid_fwd(gas, s, ds[i], dF);
285d1b9ef12SLeila Ghaffari     CeedScalar dF_i[5];
286d1b9ef12SLeila Ghaffari     UnpackState_U(dF[i], dF_i);
2872b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) strong_conv[j] += dF_i[j];
288d1b9ef12SLeila Ghaffari   }
289d1b9ef12SLeila Ghaffari }
290d1b9ef12SLeila Ghaffari 
2912b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3], CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) {
292d1b9ef12SLeila Ghaffari   for (CeedInt j = 0; j < 3; j++) {
293d1b9ef12SLeila Ghaffari     Flux[0][j] = F_inviscid[j].density;
2942b916ea7SJeremy L Thompson     for (CeedInt k = 0; k < 3; k++) Flux[k + 1][j] = F_inviscid[j].momentum[k] - stress[k][j];
295d1b9ef12SLeila Ghaffari     Flux[4][j] = F_inviscid[j].E_total + Fe[j];
296d1b9ef12SLeila Ghaffari   }
297d1b9ef12SLeila Ghaffari }
298d1b9ef12SLeila Ghaffari 
2992b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal_Boundary(const StateConservative F_inviscid[3], const CeedScalar stress[3][3], const CeedScalar Fe[3],
3002b916ea7SJeremy L Thompson                                               const CeedScalar normal[3], CeedScalar Flux[5]) {
301c5740391SJames Wright   for (CeedInt j = 0; j < 5; j++) Flux[j] = 0.;
302c5740391SJames Wright   for (CeedInt j = 0; j < 3; j++) {
303c5740391SJames Wright     Flux[0] += F_inviscid[j].density * normal[j];
304c5740391SJames Wright     for (CeedInt k = 0; k < 3; k++) {
305c5740391SJames Wright       Flux[k + 1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j];
306c5740391SJames Wright     }
307c5740391SJames Wright     Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j];
308c5740391SJames Wright   }
309c5740391SJames Wright }
310c5740391SJames Wright 
311c8c30d87SJed Brown CEED_QFUNCTION_HELPER void FluxTotal_RiemannBoundary(const StateConservative F_inviscid_normal, const CeedScalar stress[3][3], const CeedScalar Fe[3],
312c8c30d87SJed Brown                                                      const CeedScalar normal[3], CeedScalar Flux[5]) {
313c8c30d87SJed Brown   Flux[0] = F_inviscid_normal.density;
314c8c30d87SJed Brown   for (CeedInt k = 0; k < 3; k++) Flux[k + 1] = F_inviscid_normal.momentum[k];
315c8c30d87SJed Brown   Flux[4] = F_inviscid_normal.E_total;
316c8c30d87SJed Brown   for (CeedInt j = 0; j < 3; j++) {
317c8c30d87SJed Brown     for (CeedInt k = 0; k < 3; k++) {
318c8c30d87SJed Brown       Flux[k + 1] -= stress[k][j] * normal[j];
319c8c30d87SJed Brown     }
320c8c30d87SJed Brown     Flux[4] += Fe[j] * normal[j];
321c8c30d87SJed Brown   }
322c8c30d87SJed Brown }
323c8c30d87SJed Brown 
32440a33f2dSJames Wright CEED_QFUNCTION_HELPER void VelocityGradient(const State grad_s[3], CeedScalar grad_velocity[3][3]) {
32540a33f2dSJames Wright   grad_velocity[0][0] = grad_s[0].Y.velocity[0];
32640a33f2dSJames Wright   grad_velocity[0][1] = grad_s[1].Y.velocity[0];
32740a33f2dSJames Wright   grad_velocity[0][2] = grad_s[2].Y.velocity[0];
32840a33f2dSJames Wright   grad_velocity[1][0] = grad_s[0].Y.velocity[1];
32940a33f2dSJames Wright   grad_velocity[1][1] = grad_s[1].Y.velocity[1];
33040a33f2dSJames Wright   grad_velocity[1][2] = grad_s[2].Y.velocity[1];
33140a33f2dSJames Wright   grad_velocity[2][0] = grad_s[0].Y.velocity[2];
33240a33f2dSJames Wright   grad_velocity[2][1] = grad_s[1].Y.velocity[2];
33340a33f2dSJames Wright   grad_velocity[2][2] = grad_s[2].Y.velocity[2];
33440a33f2dSJames Wright }
33540a33f2dSJames Wright 
33640a33f2dSJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const CeedScalar grad_velocity[3][3], CeedScalar strain_rate[6]) {
33740a33f2dSJames Wright   const CeedScalar weight = 1 / sqrt(2.);  // Really sqrt(2.) / 2
33840a33f2dSJames Wright   strain_rate[0]          = grad_velocity[0][0];
33940a33f2dSJames Wright   strain_rate[1]          = grad_velocity[1][1];
34040a33f2dSJames Wright   strain_rate[2]          = grad_velocity[2][2];
34140a33f2dSJames Wright   strain_rate[3]          = weight * (grad_velocity[1][2] + grad_velocity[2][1]);
34240a33f2dSJames Wright   strain_rate[4]          = weight * (grad_velocity[0][2] + grad_velocity[2][0]);
34340a33f2dSJames Wright   strain_rate[5]          = weight * (grad_velocity[0][1] + grad_velocity[1][0]);
34440a33f2dSJames Wright }
34540a33f2dSJames Wright 
346475b2820SJames Wright // Kelvin-Mandel notation
34740a33f2dSJames Wright CEED_QFUNCTION_HELPER void KMStrainRate_State(const State grad_s[3], CeedScalar strain_rate[6]) {
34840a33f2dSJames Wright   CeedScalar grad_velocity[3][3];
34940a33f2dSJames Wright   VelocityGradient(grad_s, grad_velocity);
35040a33f2dSJames Wright   KMStrainRate(grad_velocity, strain_rate);
35140a33f2dSJames Wright }
35240a33f2dSJames Wright 
353f6ac214eSJames Wright //@brief Given velocity gradient du_i/dx_j, return 0.5*(du_i/dx_j - du_j/dx_i)
35440a33f2dSJames Wright CEED_QFUNCTION_HELPER void RotationRate(const CeedScalar grad_velocity[3][3], CeedScalar rotation_rate[3][3]) {
35540a33f2dSJames Wright   rotation_rate[0][0] = 0;
35640a33f2dSJames Wright   rotation_rate[1][1] = 0;
35740a33f2dSJames Wright   rotation_rate[2][2] = 0;
35840a33f2dSJames Wright   rotation_rate[1][2] = 0.5 * (grad_velocity[1][2] - grad_velocity[2][1]);
35940a33f2dSJames Wright   rotation_rate[0][2] = 0.5 * (grad_velocity[0][2] - grad_velocity[2][0]);
36040a33f2dSJames Wright   rotation_rate[0][1] = 0.5 * (grad_velocity[0][1] - grad_velocity[1][0]);
36140a33f2dSJames Wright   rotation_rate[2][1] = -rotation_rate[1][2];
36240a33f2dSJames Wright   rotation_rate[2][0] = -rotation_rate[0][2];
36340a33f2dSJames Wright   rotation_rate[1][0] = -rotation_rate[0][1];
364475b2820SJames Wright }
365475b2820SJames Wright 
3662b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, const CeedScalar strain_rate[6], CeedScalar stress[6]) {
367475b2820SJames Wright   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
368475b2820SJames Wright   for (CeedInt i = 0; i < 6; i++) {
369475b2820SJames Wright     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
370475b2820SJames Wright   }
371475b2820SJames Wright }
372475b2820SJames Wright 
3732b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
374475b2820SJames Wright                                              CeedScalar Fe[3]) {
375475b2820SJames Wright   for (CeedInt i = 0; i < 3; i++) {
3762b916ea7SJeremy 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;
377475b2820SJames Wright   }
378475b2820SJames Wright }
379475b2820SJames Wright 
3802b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
3812b916ea7SJeremy L Thompson                                                  const CeedScalar stress[3][3], const CeedScalar dstress[3][3], CeedScalar dFe[3]) {
382475b2820SJames Wright   for (CeedInt i = 0; i < 3; i++) {
3832b916ea7SJeremy 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] -
3842b916ea7SJeremy L Thompson              Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] - gas->k * grad_ds[i].Y.temperature;
385475b2820SJames Wright   }
386475b2820SJames Wright }
387475b2820SJames Wright 
38840a33f2dSJames Wright CEED_QFUNCTION_HELPER void Vorticity(const State grad_s[3], CeedScalar vorticity[3]) {
38940a33f2dSJames Wright   CeedScalar grad_velocity[3][3];
39040a33f2dSJames Wright   VelocityGradient(grad_s, grad_velocity);
39140a33f2dSJames Wright   Curl3(grad_velocity, vorticity);
39240a33f2dSJames Wright }
39340a33f2dSJames Wright 
3942e7597b6SKenneth E. Jansen CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s, StateVariable state_var,
3952e7597b6SKenneth E. Jansen                                                               const CeedScalar *grad_q, const CeedScalar dXdx[3][3], State grad_s[3]) {
39687bd45e7SJames Wright   for (CeedInt k = 0; k < 3; k++) {
397edcfef1bSKenneth E. Jansen     CeedScalar dqi[5];
39887bd45e7SJames Wright     for (CeedInt j = 0; j < 5; j++) {
39987bd45e7SJames Wright       dqi[j] =
40087bd45e7SJames 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];
40187bd45e7SJames Wright     }
402edcfef1bSKenneth E. Jansen     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
40387bd45e7SJames Wright   }
40487bd45e7SJames Wright }
40587bd45e7SJames Wright 
40687bd45e7SJames Wright CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_Boundary(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s,
4072e7597b6SKenneth E. Jansen                                                                        StateVariable state_var, const CeedScalar *grad_q, const CeedScalar dXdx[2][3],
4082e7597b6SKenneth E. Jansen                                                                        State grad_s[3]) {
40987bd45e7SJames Wright   for (CeedInt k = 0; k < 3; k++) {
410edcfef1bSKenneth E. Jansen     CeedScalar dqi[5];
41187bd45e7SJames Wright     for (CeedInt j = 0; j < 5; j++) {
41287bd45e7SJames 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];
41387bd45e7SJames Wright     }
414edcfef1bSKenneth E. Jansen     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
41587bd45e7SJames Wright   }
41687bd45e7SJames Wright }
417