xref: /libCEED/examples/fluids/qfunctions/newtonian_state.h (revision 509d4af65d23546c690c9766d8b29e47dc3b3afb)
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
10*509d4af6SJeremy 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 
412b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio(NewtonianIdealGasContext gas) { return gas->cp / gas->cv; }
422518f336SLeila Ghaffari 
432b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar GasConstant(NewtonianIdealGasContext gas) { return gas->cp - gas->cv; }
442518f336SLeila Ghaffari 
452b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) { return gas->cp * gas->mu / gas->k; }
462518f336SLeila Ghaffari 
472b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas, CeedScalar T) { return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T); }
482518f336SLeila Ghaffari 
492b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas, CeedScalar T, CeedScalar u) { return u / SoundSpeed(gas, T); }
502518f336SLeila Ghaffari 
512b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy(NewtonianIdealGasContext gas, const State s) {
52f0a19222SJames Wright   // Ignoring potential energy
53f0a19222SJames Wright   CeedScalar e_internal = gas->cv * s.Y.temperature;
54f0a19222SJames Wright   CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
55f0a19222SJames Wright   return e_internal + e_kinetic + s.Y.pressure / s.U.density;
56f0a19222SJames Wright }
57f0a19222SJames Wright 
582b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy_fwd(NewtonianIdealGasContext gas, const State s, const State ds) {
59f0a19222SJames Wright   // Ignoring potential energy
60f0a19222SJames Wright   CeedScalar de_kinetic  = Dot3(ds.Y.velocity, s.Y.velocity);
61f0a19222SJames Wright   CeedScalar de_internal = gas->cv * ds.Y.temperature;
622b730f8bSJeremy L Thompson   return de_internal + de_kinetic + ds.Y.pressure / s.U.density - s.Y.pressure / Square(s.U.density) * ds.U.density;
63f0a19222SJames Wright }
64f0a19222SJames Wright 
653bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(NewtonianIdealGasContext gas, StateConservative U) {
66c6e8c570SJames Wright   StatePrimitive Y;
67c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) Y.velocity[i] = U.momentum[i] / U.density;
68c6e8c570SJames Wright   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
69c6e8c570SJames Wright   CeedScalar e_total    = U.E_total / U.density;
703bd61617SKenneth E. Jansen   CeedScalar e_internal = e_total - e_kinetic;
71c6e8c570SJames Wright   Y.temperature         = e_internal / gas->cv;
722518f336SLeila Ghaffari   Y.pressure            = (HeatCapacityRatio(gas) - 1) * U.density * e_internal;
73c6e8c570SJames Wright   return Y;
74c6e8c570SJames Wright }
75c6e8c570SJames Wright 
763bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) {
77c6e8c570SJames Wright   StatePrimitive dY;
78c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
79c6e8c570SJames Wright     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
80c6e8c570SJames Wright   }
81c6e8c570SJames Wright   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
82c6e8c570SJames Wright   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
83c6e8c570SJames Wright   CeedScalar e_total     = s.U.E_total / s.U.density;
84c6e8c570SJames Wright   CeedScalar de_total    = (dU.E_total - e_total * dU.density) / s.U.density;
853bd61617SKenneth E. Jansen   CeedScalar e_internal  = e_total - e_kinetic;
863bd61617SKenneth E. Jansen   CeedScalar de_internal = de_total - de_kinetic;
87c6e8c570SJames Wright   dY.temperature         = de_internal / gas->cv;
882b730f8bSJeremy L Thompson   dY.pressure            = (HeatCapacityRatio(gas) - 1) * (dU.density * e_internal + s.U.density * de_internal);
89c6e8c570SJames Wright   return dY;
90c6e8c570SJames Wright }
91c6e8c570SJames Wright 
923bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
93dc805cc4SLeila Ghaffari   StateConservative U;
942518f336SLeila Ghaffari   U.density = Y.pressure / (GasConstant(gas) * Y.temperature);
95dc805cc4SLeila Ghaffari   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * Y.velocity[i];
96dc805cc4SLeila Ghaffari   CeedScalar e_internal = gas->cv * Y.temperature;
97dc805cc4SLeila Ghaffari   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
983bd61617SKenneth E. Jansen   CeedScalar e_total    = e_internal + e_kinetic;
99dc805cc4SLeila Ghaffari   U.E_total             = U.density * e_total;
100dc805cc4SLeila Ghaffari   return U;
101dc805cc4SLeila Ghaffari }
102dc805cc4SLeila Ghaffari 
1033bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
104dc805cc4SLeila Ghaffari   StateConservative dU;
1052b730f8bSJeremy L Thompson   dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
106dc805cc4SLeila Ghaffari   for (int i = 0; i < 3; i++) {
107dc805cc4SLeila Ghaffari     dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i];
108dc805cc4SLeila Ghaffari   }
109dc805cc4SLeila Ghaffari   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
110dc805cc4SLeila Ghaffari   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
111dc805cc4SLeila Ghaffari   CeedScalar e_internal  = gas->cv * s.Y.temperature;
112dc805cc4SLeila Ghaffari   CeedScalar de_internal = gas->cv * dY.temperature;
1133bd61617SKenneth E. Jansen   CeedScalar e_total     = e_internal + e_kinetic;
1143bd61617SKenneth E. Jansen   CeedScalar de_total    = de_internal + de_kinetic;
115dc805cc4SLeila Ghaffari   dU.E_total             = dU.density * e_total + s.U.density * de_total;
116dc805cc4SLeila Ghaffari   return dU;
117dc805cc4SLeila Ghaffari }
118dc805cc4SLeila Ghaffari 
1193bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
1203bd61617SKenneth E. Jansen   StateConservative U = StateConservativeFromPrimitive(gas, Y);
121d310b3d3SAdeleke O. Bankole   State             s;
122d310b3d3SAdeleke O. Bankole   s.U = U;
123d310b3d3SAdeleke O. Bankole   s.Y = Y;
124d310b3d3SAdeleke O. Bankole   return s;
125d310b3d3SAdeleke O. Bankole }
126d310b3d3SAdeleke O. Bankole 
1273bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
1283bd61617SKenneth E. Jansen   StateConservative dU = StateConservativeFromPrimitive_fwd(gas, s, dY);
1298a94a473SJed Brown   State             ds;
1308a94a473SJed Brown   ds.U = dU;
1318a94a473SJed Brown   ds.Y = dY;
1328a94a473SJed Brown   return ds;
1338a94a473SJed Brown }
1348a94a473SJed Brown 
135c95797efSJed Brown // linear combination of n states
136c95797efSJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeMult(CeedInt n, const CeedScalar a[], const StateConservative X[]) {
137c95797efSJed Brown   StateConservative R = {0};
138c95797efSJed Brown   for (CeedInt i = 0; i < n; i++) {
139c95797efSJed Brown     R.density += a[i] * X[i].density;
140c95797efSJed Brown     for (int j = 0; j < 3; j++) R.momentum[j] += a[i] * X[i].momentum[j];
141c95797efSJed Brown     R.E_total += a[i] * X[i].E_total;
142c95797efSJed Brown   }
14384ae27ebSJed Brown   return R;
14484ae27ebSJed Brown }
14584ae27ebSJed Brown 
14684ae27ebSJed Brown CEED_QFUNCTION_HELPER StateConservative StateConservativeAXPBYPCZ(CeedScalar a, StateConservative X, CeedScalar b, StateConservative Y, CeedScalar c,
14784ae27ebSJed Brown                                                                   StateConservative Z) {
14884ae27ebSJed Brown   StateConservative R;
14984ae27ebSJed Brown   R.density = a * X.density + b * Y.density + c * Z.density;
15084ae27ebSJed Brown   for (int i = 0; i < 3; i++) R.momentum[i] = a * X.momentum[i] + b * Y.momentum[i] + c * Z.momentum[i];
15184ae27ebSJed Brown   R.E_total = a * X.E_total + b * Y.E_total + c * Z.E_total;
15284ae27ebSJed Brown   return R;
15384ae27ebSJed Brown }
15484ae27ebSJed Brown 
155d310b3d3SAdeleke O. Bankole CEED_QFUNCTION_HELPER void StateToU(NewtonianIdealGasContext gas, const State input, CeedScalar U[5]) { UnpackState_U(input.U, U); }
156d310b3d3SAdeleke O. Bankole 
157d310b3d3SAdeleke O. Bankole CEED_QFUNCTION_HELPER void StateToY(NewtonianIdealGasContext gas, const State input, CeedScalar Y[5]) { UnpackState_Y(input.Y, Y); }
158f179a332SJames Wright 
159be91e165SJames Wright CEED_QFUNCTION_HELPER void StateToQ(NewtonianIdealGasContext gas, const State input, CeedScalar Q[5], StateVariable state_var) {
160be91e165SJames Wright   switch (state_var) {
161be91e165SJames Wright     case STATEVAR_CONSERVATIVE:
162be91e165SJames Wright       StateToU(gas, input, Q);
163be91e165SJames Wright       break;
164be91e165SJames Wright     case STATEVAR_PRIMITIVE:
165be91e165SJames Wright       StateToY(gas, input, Q);
166be91e165SJames Wright       break;
167be91e165SJames Wright   }
168be91e165SJames Wright }
16920840d50SJames Wright 
1703bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, const CeedScalar U[5]) {
171c6e8c570SJames Wright   State s;
172c6e8c570SJames Wright   s.U.density     = U[0];
173c6e8c570SJames Wright   s.U.momentum[0] = U[1];
174c6e8c570SJames Wright   s.U.momentum[1] = U[2];
175c6e8c570SJames Wright   s.U.momentum[2] = U[3];
176c6e8c570SJames Wright   s.U.E_total     = U[4];
1773bd61617SKenneth E. Jansen   s.Y             = StatePrimitiveFromConservative(gas, s.U);
178c6e8c570SJames Wright   return s;
179c6e8c570SJames Wright }
180c6e8c570SJames Wright 
1813bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dU[5]) {
182c6e8c570SJames Wright   State ds;
183c6e8c570SJames Wright   ds.U.density     = dU[0];
184c6e8c570SJames Wright   ds.U.momentum[0] = dU[1];
185c6e8c570SJames Wright   ds.U.momentum[1] = dU[2];
186c6e8c570SJames Wright   ds.U.momentum[2] = dU[3];
187c6e8c570SJames Wright   ds.U.E_total     = dU[4];
1883bd61617SKenneth E. Jansen   ds.Y             = StatePrimitiveFromConservative_fwd(gas, s, ds.U);
189c6e8c570SJames Wright   return ds;
190c6e8c570SJames Wright }
191c6e8c570SJames Wright 
1923bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, const CeedScalar Y[5]) {
193dc805cc4SLeila Ghaffari   State s;
194dc805cc4SLeila Ghaffari   s.Y.pressure    = Y[0];
195dc805cc4SLeila Ghaffari   s.Y.velocity[0] = Y[1];
196dc805cc4SLeila Ghaffari   s.Y.velocity[1] = Y[2];
197dc805cc4SLeila Ghaffari   s.Y.velocity[2] = Y[3];
198dc805cc4SLeila Ghaffari   s.Y.temperature = Y[4];
1993bd61617SKenneth E. Jansen   s.U             = StateConservativeFromPrimitive(gas, s.Y);
200dc805cc4SLeila Ghaffari   return s;
201dc805cc4SLeila Ghaffari }
202dc805cc4SLeila Ghaffari 
2033bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dY[5]) {
204dc805cc4SLeila Ghaffari   State ds;
205dc805cc4SLeila Ghaffari   ds.Y.pressure    = dY[0];
206dc805cc4SLeila Ghaffari   ds.Y.velocity[0] = dY[1];
207dc805cc4SLeila Ghaffari   ds.Y.velocity[1] = dY[2];
208dc805cc4SLeila Ghaffari   ds.Y.velocity[2] = dY[3];
209dc805cc4SLeila Ghaffari   ds.Y.temperature = dY[4];
2103bd61617SKenneth E. Jansen   ds.U             = StateConservativeFromPrimitive_fwd(gas, s, ds.Y);
211dc805cc4SLeila Ghaffari   return ds;
212dc805cc4SLeila Ghaffari }
213dc805cc4SLeila Ghaffari 
2143bd61617SKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromQ(NewtonianIdealGasContext gas, const CeedScalar Q[5], StateVariable state_var) {
215be91e165SJames Wright   State s;
216be91e165SJames Wright   switch (state_var) {
217be91e165SJames Wright     case STATEVAR_CONSERVATIVE:
2183bd61617SKenneth E. Jansen       s = StateFromU(gas, Q);
219be91e165SJames Wright       break;
220be91e165SJames Wright     case STATEVAR_PRIMITIVE:
2213bd61617SKenneth E. Jansen       s = StateFromY(gas, Q);
222be91e165SJames Wright       break;
223be91e165SJames Wright   }
224be91e165SJames Wright   return s;
225be91e165SJames Wright }
226be91e165SJames Wright 
22728c2e84aSKenneth E. Jansen CEED_QFUNCTION_HELPER State StateFromQ_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dQ[5], StateVariable state_var) {
228be91e165SJames Wright   State ds;
229be91e165SJames Wright   switch (state_var) {
230be91e165SJames Wright     case STATEVAR_CONSERVATIVE:
2313bd61617SKenneth E. Jansen       ds = StateFromU_fwd(gas, s, dQ);
232be91e165SJames Wright       break;
233be91e165SJames Wright     case STATEVAR_PRIMITIVE:
2343bd61617SKenneth E. Jansen       ds = StateFromY_fwd(gas, s, dQ);
235be91e165SJames Wright       break;
236be91e165SJames Wright   }
237be91e165SJames Wright   return ds;
238be91e165SJames Wright }
239be91e165SJames Wright 
2402b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, StateConservative Flux[3]) {
241c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
242c6e8c570SJames Wright     Flux[i].density = s.U.momentum[i];
2432b730f8bSJeremy 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);
244c6e8c570SJames Wright     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
245c6e8c570SJames Wright   }
246c6e8c570SJames Wright }
247c6e8c570SJames Wright 
2482b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, State s, State ds, StateConservative dFlux[3]) {
249f0a19222SJames Wright   for (CeedInt i = 0; i < 3; i++) {
250f0a19222SJames Wright     dFlux[i].density = ds.U.momentum[i];
2512b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
2522b730f8bSJeremy 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);
2532b730f8bSJeremy L Thompson     }
2542b730f8bSJeremy 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];
255f0a19222SJames Wright   }
256f0a19222SJames Wright }
257f0a19222SJames Wright 
2582b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal(NewtonianIdealGasContext gas, State s, const CeedScalar normal[3]) {
259738af36cSAdelekeBankole   StateConservative Flux[3], Flux_dot_n = {0};
260738af36cSAdelekeBankole   FluxInviscid(gas, s, Flux);
261738af36cSAdelekeBankole   for (CeedInt i = 0; i < 3; i++) {
262738af36cSAdelekeBankole     Flux_dot_n.density += Flux[i].density * normal[i];
2632b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += Flux[i].momentum[j] * normal[i];
264738af36cSAdelekeBankole     Flux_dot_n.E_total += Flux[i].E_total * normal[i];
265738af36cSAdelekeBankole   }
266738af36cSAdelekeBankole   return Flux_dot_n;
267738af36cSAdelekeBankole }
268738af36cSAdelekeBankole 
2692b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal_fwd(NewtonianIdealGasContext gas, State s, State ds, const CeedScalar normal[3]) {
270f0a19222SJames Wright   StateConservative dFlux[3], Flux_dot_n = {0};
271f0a19222SJames Wright   FluxInviscid_fwd(gas, s, ds, dFlux);
272c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
273f0a19222SJames Wright     Flux_dot_n.density += dFlux[i].density * normal[i];
2742b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += dFlux[i].momentum[j] * normal[i];
275f0a19222SJames Wright     Flux_dot_n.E_total += dFlux[i].E_total * normal[i];
276c6e8c570SJames Wright   }
277f0a19222SJames Wright   return Flux_dot_n;
278c6e8c570SJames Wright }
279c6e8c570SJames Wright 
2802b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, State s, State ds[3], CeedScalar strong_conv[5]) {
2812b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 5; i++) strong_conv[i] = 0;
2822b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 3; i++) {
2832b89d87eSLeila Ghaffari     StateConservative dF[3];
2842b89d87eSLeila Ghaffari     FluxInviscid_fwd(gas, s, ds[i], dF);
2852b89d87eSLeila Ghaffari     CeedScalar dF_i[5];
2862b89d87eSLeila Ghaffari     UnpackState_U(dF[i], dF_i);
2872b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) strong_conv[j] += dF_i[j];
2882b89d87eSLeila Ghaffari   }
2892b89d87eSLeila Ghaffari }
2902b89d87eSLeila Ghaffari 
2912b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3], CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) {
2922b89d87eSLeila Ghaffari   for (CeedInt j = 0; j < 3; j++) {
2932b89d87eSLeila Ghaffari     Flux[0][j] = F_inviscid[j].density;
2942b730f8bSJeremy L Thompson     for (CeedInt k = 0; k < 3; k++) Flux[k + 1][j] = F_inviscid[j].momentum[k] - stress[k][j];
2952b89d87eSLeila Ghaffari     Flux[4][j] = F_inviscid[j].E_total + Fe[j];
2962b89d87eSLeila Ghaffari   }
2972b89d87eSLeila Ghaffari }
2982b89d87eSLeila Ghaffari 
2992b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void FluxTotal_Boundary(const StateConservative F_inviscid[3], const CeedScalar stress[3][3], const CeedScalar Fe[3],
3002b730f8bSJeremy L Thompson                                               const CeedScalar normal[3], CeedScalar Flux[5]) {
3015bce47c7SJames Wright   for (CeedInt j = 0; j < 5; j++) Flux[j] = 0.;
3025bce47c7SJames Wright   for (CeedInt j = 0; j < 3; j++) {
3035bce47c7SJames Wright     Flux[0] += F_inviscid[j].density * normal[j];
3045bce47c7SJames Wright     for (CeedInt k = 0; k < 3; k++) {
3055bce47c7SJames Wright       Flux[k + 1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j];
3065bce47c7SJames Wright     }
3075bce47c7SJames Wright     Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j];
3085bce47c7SJames Wright   }
3095bce47c7SJames Wright }
3105bce47c7SJames Wright 
3118a94a473SJed Brown CEED_QFUNCTION_HELPER void FluxTotal_RiemannBoundary(const StateConservative F_inviscid_normal, const CeedScalar stress[3][3], const CeedScalar Fe[3],
3128a94a473SJed Brown                                                      const CeedScalar normal[3], CeedScalar Flux[5]) {
3138a94a473SJed Brown   Flux[0] = F_inviscid_normal.density;
3148a94a473SJed Brown   for (CeedInt k = 0; k < 3; k++) Flux[k + 1] = F_inviscid_normal.momentum[k];
3158a94a473SJed Brown   Flux[4] = F_inviscid_normal.E_total;
3168a94a473SJed Brown   for (CeedInt j = 0; j < 3; j++) {
3178a94a473SJed Brown     for (CeedInt k = 0; k < 3; k++) {
3188a94a473SJed Brown       Flux[k + 1] -= stress[k][j] * normal[j];
3198a94a473SJed Brown     }
3208a94a473SJed Brown     Flux[4] += Fe[j] * normal[j];
3218a94a473SJed Brown   }
3228a94a473SJed Brown }
3238a94a473SJed Brown 
324d08fcc28SJames Wright CEED_QFUNCTION_HELPER void VelocityGradient(const State grad_s[3], CeedScalar grad_velocity[3][3]) {
325d08fcc28SJames Wright   grad_velocity[0][0] = grad_s[0].Y.velocity[0];
326d08fcc28SJames Wright   grad_velocity[0][1] = grad_s[1].Y.velocity[0];
327d08fcc28SJames Wright   grad_velocity[0][2] = grad_s[2].Y.velocity[0];
328d08fcc28SJames Wright   grad_velocity[1][0] = grad_s[0].Y.velocity[1];
329d08fcc28SJames Wright   grad_velocity[1][1] = grad_s[1].Y.velocity[1];
330d08fcc28SJames Wright   grad_velocity[1][2] = grad_s[2].Y.velocity[1];
331d08fcc28SJames Wright   grad_velocity[2][0] = grad_s[0].Y.velocity[2];
332d08fcc28SJames Wright   grad_velocity[2][1] = grad_s[1].Y.velocity[2];
333d08fcc28SJames Wright   grad_velocity[2][2] = grad_s[2].Y.velocity[2];
334d08fcc28SJames Wright }
335d08fcc28SJames Wright 
336d08fcc28SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate(const CeedScalar grad_velocity[3][3], CeedScalar strain_rate[6]) {
337d08fcc28SJames Wright   const CeedScalar weight = 1 / sqrt(2.);  // Really sqrt(2.) / 2
338d08fcc28SJames Wright   strain_rate[0]          = grad_velocity[0][0];
339d08fcc28SJames Wright   strain_rate[1]          = grad_velocity[1][1];
340d08fcc28SJames Wright   strain_rate[2]          = grad_velocity[2][2];
341d08fcc28SJames Wright   strain_rate[3]          = weight * (grad_velocity[1][2] + grad_velocity[2][1]);
342d08fcc28SJames Wright   strain_rate[4]          = weight * (grad_velocity[0][2] + grad_velocity[2][0]);
343d08fcc28SJames Wright   strain_rate[5]          = weight * (grad_velocity[0][1] + grad_velocity[1][0]);
344d08fcc28SJames Wright }
345d08fcc28SJames Wright 
346c6e8c570SJames Wright // Kelvin-Mandel notation
347d08fcc28SJames Wright CEED_QFUNCTION_HELPER void KMStrainRate_State(const State grad_s[3], CeedScalar strain_rate[6]) {
348d08fcc28SJames Wright   CeedScalar grad_velocity[3][3];
349d08fcc28SJames Wright   VelocityGradient(grad_s, grad_velocity);
350d08fcc28SJames Wright   KMStrainRate(grad_velocity, strain_rate);
351d08fcc28SJames Wright }
352d08fcc28SJames Wright 
3537618d7b7SJames Wright //@brief Given velocity gradient du_i/dx_j, return 0.5*(du_i/dx_j - du_j/dx_i)
354d08fcc28SJames Wright CEED_QFUNCTION_HELPER void RotationRate(const CeedScalar grad_velocity[3][3], CeedScalar rotation_rate[3][3]) {
355d08fcc28SJames Wright   rotation_rate[0][0] = 0;
356d08fcc28SJames Wright   rotation_rate[1][1] = 0;
357d08fcc28SJames Wright   rotation_rate[2][2] = 0;
358d08fcc28SJames Wright   rotation_rate[1][2] = 0.5 * (grad_velocity[1][2] - grad_velocity[2][1]);
359d08fcc28SJames Wright   rotation_rate[0][2] = 0.5 * (grad_velocity[0][2] - grad_velocity[2][0]);
360d08fcc28SJames Wright   rotation_rate[0][1] = 0.5 * (grad_velocity[0][1] - grad_velocity[1][0]);
361d08fcc28SJames Wright   rotation_rate[2][1] = -rotation_rate[1][2];
362d08fcc28SJames Wright   rotation_rate[2][0] = -rotation_rate[0][2];
363d08fcc28SJames Wright   rotation_rate[1][0] = -rotation_rate[0][1];
364c6e8c570SJames Wright }
365c6e8c570SJames Wright 
3662b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, const CeedScalar strain_rate[6], CeedScalar stress[6]) {
367c6e8c570SJames Wright   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
368c6e8c570SJames Wright   for (CeedInt i = 0; i < 6; i++) {
369c6e8c570SJames Wright     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
370c6e8c570SJames Wright   }
371c6e8c570SJames Wright }
372c6e8c570SJames Wright 
3732b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
374c6e8c570SJames Wright                                              CeedScalar Fe[3]) {
375c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
3762b730f8bSJeremy 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;
377c6e8c570SJames Wright   }
378c6e8c570SJames Wright }
379c6e8c570SJames Wright 
3802b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
3812b730f8bSJeremy L Thompson                                                  const CeedScalar stress[3][3], const CeedScalar dstress[3][3], CeedScalar dFe[3]) {
382c6e8c570SJames Wright   for (CeedInt i = 0; i < 3; i++) {
3832b730f8bSJeremy 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] -
3842b730f8bSJeremy L Thompson              Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] - gas->k * grad_ds[i].Y.temperature;
385c6e8c570SJames Wright   }
386c6e8c570SJames Wright }
387c6e8c570SJames Wright 
388d08fcc28SJames Wright CEED_QFUNCTION_HELPER void Vorticity(const State grad_s[3], CeedScalar vorticity[3]) {
389d08fcc28SJames Wright   CeedScalar grad_velocity[3][3];
390d08fcc28SJames Wright   VelocityGradient(grad_s, grad_velocity);
391d08fcc28SJames Wright   Curl3(grad_velocity, vorticity);
392d08fcc28SJames Wright }
393d08fcc28SJames Wright 
39428c2e84aSKenneth E. Jansen CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s, StateVariable state_var,
39528c2e84aSKenneth E. Jansen                                                               const CeedScalar *grad_q, const CeedScalar dXdx[3][3], State grad_s[3]) {
3969b6a821dSJames Wright   for (CeedInt k = 0; k < 3; k++) {
3973bd61617SKenneth E. Jansen     CeedScalar dqi[5];
3989b6a821dSJames Wright     for (CeedInt j = 0; j < 5; j++) {
3999b6a821dSJames Wright       dqi[j] =
4009b6a821dSJames 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];
4019b6a821dSJames Wright     }
4023bd61617SKenneth E. Jansen     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
4039b6a821dSJames Wright   }
4049b6a821dSJames Wright }
4059b6a821dSJames Wright 
4069b6a821dSJames Wright CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_Boundary(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s,
40728c2e84aSKenneth E. Jansen                                                                        StateVariable state_var, const CeedScalar *grad_q, const CeedScalar dXdx[2][3],
40828c2e84aSKenneth E. Jansen                                                                        State grad_s[3]) {
4099b6a821dSJames Wright   for (CeedInt k = 0; k < 3; k++) {
4103bd61617SKenneth E. Jansen     CeedScalar dqi[5];
4119b6a821dSJames Wright     for (CeedInt j = 0; j < 5; j++) {
4129b6a821dSJames 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];
4139b6a821dSJames Wright     }
4143bd61617SKenneth E. Jansen     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
4159b6a821dSJames Wright   }
4169b6a821dSJames Wright }
417