xref: /honee/qfunctions/newtonian_state.h (revision da02a6e7947cab8e173fd83bf2b4eda896c06b14)
1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3 
4 /// @file
5 /// Structs and helper functions regarding the state of a newtonian simulation
6 #pragma once
7 
8 #include <ceed.h>
9 #include <math.h>
10 
11 #include "newtonian_types.h"
12 #include "utils.h"
13 
14 typedef struct {
15   CeedScalar density;
16   CeedScalar momentum[3];
17   CeedScalar E_total;
18 } StateConservative;
19 
20 typedef struct {
21   StateConservative U;
22   StatePrimitive    Y;
23 } State;
24 
25 CEED_QFUNCTION_HELPER void UnpackState_U(StateConservative s, CeedScalar U[5]) {
26   U[0] = s.density;
27   for (int i = 0; i < 3; i++) U[i + 1] = s.momentum[i];
28   U[4] = s.E_total;
29 }
30 
31 CEED_QFUNCTION_HELPER void UnpackState_Y(StatePrimitive s, CeedScalar Y[5]) {
32   Y[0] = s.pressure;
33   for (int i = 0; i < 3; i++) Y[i + 1] = s.velocity[i];
34   Y[4] = s.temperature;
35 }
36 
37 CEED_QFUNCTION_HELPER void UnpackState_V(StateEntropy s, CeedScalar V[5]) {
38   V[0] = s.S_density;
39   for (int i = 0; i < 3; i++) V[i + 1] = s.S_momentum[i];
40   V[4] = s.S_energy;
41 }
42 
43 CEED_QFUNCTION_HELPER CeedScalar HeatCapacityRatio(NewtonianIdealGasContext gas) { return gas->cp / gas->cv; }
44 
45 CEED_QFUNCTION_HELPER CeedScalar GasConstant(NewtonianIdealGasContext gas) { return gas->cp - gas->cv; }
46 
47 CEED_QFUNCTION_HELPER CeedScalar Prandtl(NewtonianIdealGasContext gas) { return gas->cp * gas->mu / gas->k; }
48 
49 CEED_QFUNCTION_HELPER CeedScalar SoundSpeed(NewtonianIdealGasContext gas, CeedScalar T) { return sqrt(gas->cp * (HeatCapacityRatio(gas) - 1.) * T); }
50 
51 CEED_QFUNCTION_HELPER CeedScalar Mach(NewtonianIdealGasContext gas, CeedScalar T, CeedScalar u) { return u / SoundSpeed(gas, T); }
52 
53 CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy(NewtonianIdealGasContext gas, const State s) {
54   CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
55   CeedScalar e_internal = gas->cv * s.Y.temperature;
56   return e_internal + e_kinetic + s.Y.pressure / s.U.density;
57 }
58 
59 CEED_QFUNCTION_HELPER CeedScalar TotalSpecificEnthalpy_fwd(NewtonianIdealGasContext gas, const State s, const State ds) {
60   CeedScalar de_kinetic  = Dot3(ds.Y.velocity, s.Y.velocity);
61   CeedScalar de_internal = gas->cv * ds.Y.temperature;
62   return de_internal + de_kinetic + ds.Y.pressure / s.U.density - s.Y.pressure / Square(s.U.density) * ds.U.density;
63 }
64 
65 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative(NewtonianIdealGasContext gas, StateConservative U) {
66   StatePrimitive Y;
67   for (CeedInt i = 0; i < 3; i++) Y.velocity[i] = U.momentum[i] / U.density;
68   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
69   CeedScalar e_total    = U.E_total / U.density;
70   CeedScalar e_internal = e_total - e_kinetic;
71   Y.temperature         = e_internal / gas->cv;
72   Y.pressure            = (HeatCapacityRatio(gas) - 1) * U.density * e_internal;
73   return Y;
74 }
75 
76 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) {
77   StatePrimitive dY;
78   for (CeedInt i = 0; i < 3; i++) {
79     dY.velocity[i] = (dU.momentum[i] - s.Y.velocity[i] * dU.density) / s.U.density;
80   }
81   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
82   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
83   CeedScalar e_total     = s.U.E_total / s.U.density;
84   CeedScalar de_total    = (dU.E_total - e_total * dU.density) / s.U.density;
85   CeedScalar e_internal  = e_total - e_kinetic;
86   CeedScalar de_internal = de_total - de_kinetic;
87   dY.temperature         = de_internal / gas->cv;
88   dY.pressure            = (HeatCapacityRatio(gas) - 1) * (dU.density * e_internal + s.U.density * de_internal);
89   return dY;
90 }
91 
92 CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
93   StateEntropy     V;
94   const CeedScalar gamma     = HeatCapacityRatio(gas);
95   const CeedScalar rho       = Y.pressure / (GasConstant(gas) * Y.temperature);
96   const CeedScalar entropy   = log(Y.pressure) - gamma * log(rho);
97   const CeedScalar rho_div_p = rho / Y.pressure;
98   const CeedScalar e_kinetic = 0.5 * Dot3(Y.velocity, Y.velocity);
99 
100   V.S_density = (gamma - entropy) / (gamma - 1) - rho_div_p * e_kinetic;
101   for (int i = 0; i < 3; i++) V.S_momentum[i] = rho_div_p * Y.velocity[i];
102   V.S_energy = -rho_div_p;
103   return V;
104 }
105 
106 CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
107   StateEntropy     dV;
108   const CeedScalar gamma = HeatCapacityRatio(gas);
109   CeedScalar       drho  = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
110 
111   const CeedScalar e_kinetic  = .5 * Dot3(s.Y.velocity, s.Y.velocity);
112   const CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
113   const CeedScalar rho_div_p  = s.U.density / s.Y.pressure;
114   const CeedScalar drho_div_p = (drho * s.Y.pressure - s.U.density * dY.pressure) / Square(s.Y.pressure);
115 
116   CeedScalar dentropy = dY.pressure / s.Y.pressure - gamma * drho / s.U.density;
117 
118   dV.S_density = -dentropy / (gamma - 1) - de_kinetic * rho_div_p - e_kinetic * drho_div_p;
119   for (CeedInt i = 0; i < 3; i++) dV.S_momentum[i] = rho_div_p * dY.velocity[i] + drho_div_p * s.Y.velocity[i];
120   dV.S_energy = -drho_div_p;
121   return dV;
122 }
123 
124 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromEntropy(NewtonianIdealGasContext gas, StateEntropy V) {
125   StatePrimitive Y;
126   for (int i = 0; i < 3; i++) Y.velocity[i] = -V.S_momentum[i] / V.S_energy;
127   Y.temperature              = -1 / (GasConstant(gas) * V.S_energy);
128   const CeedScalar gamma     = HeatCapacityRatio(gas);
129   const CeedScalar e_kinetic = 0.5 * Dot3(Y.velocity, Y.velocity);
130   const CeedScalar entropy   = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
131   const CeedScalar log_P     = -(entropy + gamma * log(-V.S_energy)) / (gamma - 1);
132   Y.pressure                 = exp(log_P);
133   return Y;
134 }
135 
136 CEED_QFUNCTION_HELPER StatePrimitive StatePrimitiveFromEntropy_fwd(NewtonianIdealGasContext gas, State s, StateEntropy dV) {
137   StatePrimitive dY;
138   StateEntropy   V = StateEntropyFromPrimitive(gas, s.Y);
139   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;
140   dY.temperature              = dV.S_energy / (GasConstant(gas) * V.S_energy * V.S_energy);
141   const CeedScalar gamma      = HeatCapacityRatio(gas);
142   const CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
143   const CeedScalar de_kinetic = Dot3(dY.velocity, s.Y.velocity);
144   const CeedScalar dentropy   = (1 - gamma) * (dV.S_density - e_kinetic * dV.S_energy - de_kinetic * V.S_energy);
145   dY.pressure                 = s.Y.pressure * (-dentropy - gamma * dV.S_energy / V.S_energy) / (gamma - 1);
146   return dY;
147 }
148 
149 CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
150   StateConservative U;
151   U.density = Y.pressure / (GasConstant(gas) * Y.temperature);
152   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * Y.velocity[i];
153   CeedScalar e_internal = gas->cv * Y.temperature;
154   CeedScalar e_kinetic  = .5 * Dot3(Y.velocity, Y.velocity);
155   CeedScalar e_total    = e_internal + e_kinetic;
156   U.E_total             = U.density * e_total;
157   return U;
158 }
159 
160 CEED_QFUNCTION_HELPER StateConservative StateConservativeFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
161   StateConservative dU;
162   dU.density = (dY.pressure * s.Y.temperature - s.Y.pressure * dY.temperature) / (GasConstant(gas) * s.Y.temperature * s.Y.temperature);
163   for (int i = 0; i < 3; i++) {
164     dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dY.velocity[i];
165   }
166   CeedScalar e_kinetic   = .5 * Dot3(s.Y.velocity, s.Y.velocity);
167   CeedScalar de_kinetic  = Dot3(dY.velocity, s.Y.velocity);
168   CeedScalar e_internal  = gas->cv * s.Y.temperature;
169   CeedScalar de_internal = gas->cv * dY.temperature;
170   CeedScalar e_total     = e_internal + e_kinetic;
171   CeedScalar de_total    = de_internal + de_kinetic;
172   dU.E_total             = dU.density * e_total + s.U.density * de_total;
173   return dU;
174 }
175 
176 CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromConservative(NewtonianIdealGasContext gas, StateConservative U) {
177   StateEntropy     V;
178   const CeedScalar gamma      = HeatCapacityRatio(gas);
179   const CeedScalar e_kinetic  = .5 * Dot3(U.momentum, U.momentum) / U.density;
180   const CeedScalar e_internal = U.E_total - e_kinetic;
181   const CeedScalar p          = (gamma - 1) * e_internal;
182   const CeedScalar entropy    = log(p) - gamma * log(U.density);
183 
184   V.S_density = (gamma - entropy) / (gamma - 1) - e_kinetic / p;
185   for (int i = 0; i < 3; i++) V.S_momentum[i] = U.momentum[i] / p;
186   V.S_energy = -U.density / p;
187   return V;
188 }
189 
190 CEED_QFUNCTION_HELPER StateEntropy StateEntropyFromConservative_fwd(NewtonianIdealGasContext gas, State s, StateConservative dU) {
191   StateEntropy     dV;
192   const CeedScalar gamma       = HeatCapacityRatio(gas);
193   const CeedScalar e_kinetic   = .5 * Dot3(s.U.momentum, s.U.momentum) / s.U.density;
194   const CeedScalar de_kinetic  = (Dot3(s.U.momentum, dU.momentum) - e_kinetic * dU.density) / s.U.density;
195   const CeedScalar de_internal = dU.E_total - de_kinetic;
196   const CeedScalar p           = s.Y.pressure;
197   const CeedScalar dp          = (gamma - 1) * de_internal;
198 
199   CeedScalar dentropy = dp / p - gamma * dU.density / s.U.density;
200 
201   dV.S_density = -dentropy / (gamma - 1) - de_kinetic / p + dp * e_kinetic / Square(p);
202   for (CeedInt i = 0; i < 3; i++) {
203     dV.S_momentum[i] = (dU.momentum[i] - s.U.momentum[i] * dp / p) / p;
204   }
205   dV.S_energy = -(dU.density - s.U.density * dp / p) / p;
206   return dV;
207 }
208 
209 CEED_QFUNCTION_HELPER StateConservative StateConservativeFromEntropy(NewtonianIdealGasContext gas, StateEntropy V) {
210   StateConservative U;
211   CeedScalar        velocity[3];
212   for (int i = 0; i < 3; i++) velocity[i] = -V.S_momentum[i] / V.S_energy;
213   const CeedScalar gamma     = HeatCapacityRatio(gas);
214   const CeedScalar e_kinetic = 0.5 * Dot3(velocity, velocity);
215   const CeedScalar entropy   = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
216   const CeedScalar log_rho   = -(entropy + log(-V.S_energy)) / (gamma - 1);
217   U.density                  = exp(log_rho);
218   for (int i = 0; i < 3; i++) U.momentum[i] = U.density * velocity[i];
219 
220   const CeedScalar e_internal = -gas->cv / (GasConstant(gas) * V.S_energy);
221   U.E_total                   = U.density * (e_internal + e_kinetic);
222   return U;
223 }
224 
225 CEED_QFUNCTION_HELPER StateConservative StateConservativeFromEntropy_fwd(NewtonianIdealGasContext gas, State s, StateEntropy dV) {
226   StateConservative dU;
227   CeedScalar        dvelocity[3];
228   StateEntropy      V = StateEntropyFromPrimitive(gas, s.Y);
229   for (int i = 0; i < 3; i++) dvelocity[i] = (-dV.S_momentum[i] - s.Y.velocity[i] * dV.S_energy) / V.S_energy;
230   const CeedScalar gamma      = HeatCapacityRatio(gas);
231   const CeedScalar e_kinetic  = 0.5 * Dot3(s.Y.velocity, s.Y.velocity);
232   const CeedScalar de_kinetic = Dot3(dvelocity, s.Y.velocity);
233   const CeedScalar entropy    = gamma - (gamma - 1) * (V.S_density - e_kinetic * V.S_energy);
234   const CeedScalar dentropy   = -(gamma - 1) * (dV.S_density - (de_kinetic * V.S_energy + e_kinetic * dV.S_energy));
235   const CeedScalar log_rho    = -(entropy + log(-V.S_energy)) / (gamma - 1);
236   const CeedScalar rho        = exp(log_rho);
237   dU.density                  = -rho / (gamma - 1) * (dentropy + dV.S_energy / V.S_energy);
238   for (int i = 0; i < 3; i++) dU.momentum[i] = dU.density * s.Y.velocity[i] + s.U.density * dvelocity[i];
239 
240   const CeedScalar e_internal  = -gas->cv / (GasConstant(gas) * V.S_energy);
241   const CeedScalar de_internal = gas->cv * dV.S_energy / (GasConstant(gas) * V.S_energy * V.S_energy);
242   const CeedScalar e_total     = e_internal + e_kinetic;
243   dU.E_total                   = dU.density * e_total + s.U.density * (de_internal + de_kinetic);
244   return dU;
245 }
246 
247 CEED_QFUNCTION_HELPER State StateFromPrimitive(NewtonianIdealGasContext gas, StatePrimitive Y) {
248   StateConservative U = StateConservativeFromPrimitive(gas, Y);
249   State             s;
250   s.U = U;
251   s.Y = Y;
252   return s;
253 }
254 
255 CEED_QFUNCTION_HELPER State StateFromPrimitive_fwd(NewtonianIdealGasContext gas, State s, StatePrimitive dY) {
256   StateConservative dU = StateConservativeFromPrimitive_fwd(gas, s, dY);
257   State             ds;
258   ds.U = dU;
259   ds.Y = dY;
260   return ds;
261 }
262 
263 // linear combination of n states
264 CEED_QFUNCTION_HELPER StateConservative StateConservativeMult(CeedInt n, const CeedScalar a[], const StateConservative X[]) {
265   StateConservative R = {0};
266   for (CeedInt i = 0; i < n; i++) {
267     R.density += a[i] * X[i].density;
268     for (int j = 0; j < 3; j++) R.momentum[j] += a[i] * X[i].momentum[j];
269     R.E_total += a[i] * X[i].E_total;
270   }
271   return R;
272 }
273 
274 CEED_QFUNCTION_HELPER StateConservative StateConservativeAXPBYPCZ(CeedScalar a, StateConservative X, CeedScalar b, StateConservative Y, CeedScalar c,
275                                                                   StateConservative Z) {
276   StateConservative R;
277   R.density = a * X.density + b * Y.density + c * Z.density;
278   for (int i = 0; i < 3; i++) R.momentum[i] = a * X.momentum[i] + b * Y.momentum[i] + c * Z.momentum[i];
279   R.E_total = a * X.E_total + b * Y.E_total + c * Z.E_total;
280   return R;
281 }
282 
283 CEED_QFUNCTION_HELPER void StateToU(NewtonianIdealGasContext gas, const State input, CeedScalar U[5]) { UnpackState_U(input.U, U); }
284 
285 CEED_QFUNCTION_HELPER void StateToY(NewtonianIdealGasContext gas, const State input, CeedScalar Y[5]) { UnpackState_Y(input.Y, Y); }
286 
287 CEED_QFUNCTION_HELPER void StateToV(NewtonianIdealGasContext gas, const State input, CeedScalar V[5]) {
288   StateEntropy state_V = StateEntropyFromPrimitive(gas, input.Y);
289   UnpackState_V(state_V, V);
290 }
291 
292 CEED_QFUNCTION_HELPER void StateToQ(NewtonianIdealGasContext gas, const State input, CeedScalar Q[5], StateVariable state_var) {
293   switch (state_var) {
294     case STATEVAR_CONSERVATIVE:
295       StateToU(gas, input, Q);
296       break;
297     case STATEVAR_PRIMITIVE:
298       StateToY(gas, input, Q);
299       break;
300     case STATEVAR_ENTROPY:
301       StateToV(gas, input, Q);
302       break;
303     default:
304       SetValueN(Q, -1, 5);
305       break;
306   }
307 }
308 
309 CEED_QFUNCTION_HELPER void StateToQ_fwd(NewtonianIdealGasContext gas, const State input, const State dinput, CeedScalar dQ[5],
310                                         StateVariable state_var) {
311   switch (state_var) {
312     case STATEVAR_CONSERVATIVE:
313     case STATEVAR_PRIMITIVE:
314       StateToQ(gas, dinput, dQ, state_var);
315       break;
316     case STATEVAR_ENTROPY: {
317       StateEntropy dstate_v;
318 
319       dstate_v = StateEntropyFromPrimitive_fwd(gas, input, dinput.Y);
320       UnpackState_V(dstate_v, dQ);
321     } break;
322   }
323 }
324 
325 CEED_QFUNCTION_HELPER State StateFromU(NewtonianIdealGasContext gas, const CeedScalar U[5]) {
326   State s;
327   s.U.density     = U[0];
328   s.U.momentum[0] = U[1];
329   s.U.momentum[1] = U[2];
330   s.U.momentum[2] = U[3];
331   s.U.E_total     = U[4];
332   s.Y             = StatePrimitiveFromConservative(gas, s.U);
333   return s;
334 }
335 
336 CEED_QFUNCTION_HELPER State StateFromU_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dU[5]) {
337   State ds;
338   ds.U.density     = dU[0];
339   ds.U.momentum[0] = dU[1];
340   ds.U.momentum[1] = dU[2];
341   ds.U.momentum[2] = dU[3];
342   ds.U.E_total     = dU[4];
343   ds.Y             = StatePrimitiveFromConservative_fwd(gas, s, ds.U);
344   return ds;
345 }
346 
347 CEED_QFUNCTION_HELPER State StateFromY(NewtonianIdealGasContext gas, const CeedScalar Y[5]) {
348   State s;
349   s.Y.pressure    = Y[0];
350   s.Y.velocity[0] = Y[1];
351   s.Y.velocity[1] = Y[2];
352   s.Y.velocity[2] = Y[3];
353   s.Y.temperature = Y[4];
354   s.U             = StateConservativeFromPrimitive(gas, s.Y);
355   return s;
356 }
357 
358 CEED_QFUNCTION_HELPER State StateFromY_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dY[5]) {
359   State ds;
360   ds.Y.pressure    = dY[0];
361   ds.Y.velocity[0] = dY[1];
362   ds.Y.velocity[1] = dY[2];
363   ds.Y.velocity[2] = dY[3];
364   ds.Y.temperature = dY[4];
365   ds.U             = StateConservativeFromPrimitive_fwd(gas, s, ds.Y);
366   return ds;
367 }
368 
369 CEED_QFUNCTION_HELPER State StateFromV(NewtonianIdealGasContext gas, const CeedScalar V[5]) {
370   State        s;
371   StateEntropy state_V;
372   state_V.S_density     = V[0];
373   state_V.S_momentum[0] = V[1];
374   state_V.S_momentum[1] = V[2];
375   state_V.S_momentum[2] = V[3];
376   state_V.S_energy      = V[4];
377   s.U                   = StateConservativeFromEntropy(gas, state_V);
378   s.Y                   = StatePrimitiveFromEntropy(gas, state_V);
379   return s;
380 }
381 
382 CEED_QFUNCTION_HELPER State StateFromV_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dV[5]) {
383   State        ds;
384   StateEntropy state_dV;
385   state_dV.S_density     = dV[0];
386   state_dV.S_momentum[0] = dV[1];
387   state_dV.S_momentum[1] = dV[2];
388   state_dV.S_momentum[2] = dV[3];
389   state_dV.S_energy      = dV[4];
390   ds.U                   = StateConservativeFromEntropy_fwd(gas, s, state_dV);
391   ds.Y                   = StatePrimitiveFromEntropy_fwd(gas, s, state_dV);
392   return ds;
393 }
394 
395 CEED_QFUNCTION_HELPER State StateFromQ(NewtonianIdealGasContext gas, const CeedScalar Q[5], StateVariable state_var) {
396   State s;
397   switch (state_var) {
398     case STATEVAR_CONSERVATIVE:
399       s = StateFromU(gas, Q);
400       break;
401     case STATEVAR_PRIMITIVE:
402       s = StateFromY(gas, Q);
403       break;
404     case STATEVAR_ENTROPY:
405       s = StateFromV(gas, Q);
406       break;
407   }
408   return s;
409 }
410 
411 CEED_QFUNCTION_HELPER State StateFromQ_fwd(NewtonianIdealGasContext gas, State s, const CeedScalar dQ[5], StateVariable state_var) {
412   State ds;
413   switch (state_var) {
414     case STATEVAR_CONSERVATIVE:
415       ds = StateFromU_fwd(gas, s, dQ);
416       break;
417     case STATEVAR_PRIMITIVE:
418       ds = StateFromY_fwd(gas, s, dQ);
419       break;
420     case STATEVAR_ENTROPY:
421       ds = StateFromV_fwd(gas, s, dQ);
422       break;
423   }
424   return ds;
425 }
426 
427 CEED_QFUNCTION_HELPER void FluxInviscid(NewtonianIdealGasContext gas, State s, StateConservative Flux[3]) {
428   for (CeedInt i = 0; i < 3; i++) {
429     Flux[i].density = s.U.momentum[i];
430     for (CeedInt j = 0; j < 3; j++) Flux[i].momentum[j] = s.U.momentum[i] * s.Y.velocity[j] + s.Y.pressure * (i == j);
431     Flux[i].E_total = (s.U.E_total + s.Y.pressure) * s.Y.velocity[i];
432   }
433 }
434 
435 CEED_QFUNCTION_HELPER void FluxInviscid_fwd(NewtonianIdealGasContext gas, State s, State ds, StateConservative dFlux[3]) {
436   for (CeedInt i = 0; i < 3; i++) {
437     dFlux[i].density = ds.U.momentum[i];
438     for (CeedInt j = 0; j < 3; j++) {
439       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);
440     }
441     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];
442   }
443 }
444 
445 CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal(NewtonianIdealGasContext gas, State s, const CeedScalar normal[3]) {
446   StateConservative Flux[3], Flux_dot_n = {0};
447   FluxInviscid(gas, s, Flux);
448   for (CeedInt i = 0; i < 3; i++) {
449     Flux_dot_n.density += Flux[i].density * normal[i];
450     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += Flux[i].momentum[j] * normal[i];
451     Flux_dot_n.E_total += Flux[i].E_total * normal[i];
452   }
453   return Flux_dot_n;
454 }
455 
456 CEED_QFUNCTION_HELPER StateConservative FluxInviscidDotNormal_fwd(NewtonianIdealGasContext gas, State s, State ds, const CeedScalar normal[3]) {
457   StateConservative dFlux[3], Flux_dot_n = {0};
458   FluxInviscid_fwd(gas, s, ds, dFlux);
459   for (CeedInt i = 0; i < 3; i++) {
460     Flux_dot_n.density += dFlux[i].density * normal[i];
461     for (CeedInt j = 0; j < 3; j++) Flux_dot_n.momentum[j] += dFlux[i].momentum[j] * normal[i];
462     Flux_dot_n.E_total += dFlux[i].E_total * normal[i];
463   }
464   return Flux_dot_n;
465 }
466 
467 CEED_QFUNCTION_HELPER void FluxInviscidStrong(NewtonianIdealGasContext gas, State s, State ds[3], CeedScalar strong_conv[5]) {
468   for (CeedInt i = 0; i < 5; i++) strong_conv[i] = 0;
469   for (CeedInt i = 0; i < 3; i++) {
470     StateConservative dF[3];
471     FluxInviscid_fwd(gas, s, ds[i], dF);
472     CeedScalar dF_i[5];
473     UnpackState_U(dF[i], dF_i);
474     for (CeedInt j = 0; j < 5; j++) strong_conv[j] += dF_i[j];
475   }
476 }
477 
478 CEED_QFUNCTION_HELPER void FluxTotal(const StateConservative F_inviscid[3], CeedScalar stress[3][3], CeedScalar Fe[3], CeedScalar Flux[5][3]) {
479   for (CeedInt j = 0; j < 3; j++) {
480     Flux[0][j] = F_inviscid[j].density;
481     for (CeedInt k = 0; k < 3; k++) Flux[k + 1][j] = F_inviscid[j].momentum[k] - stress[k][j];
482     Flux[4][j] = F_inviscid[j].E_total + Fe[j];
483   }
484 }
485 
486 CEED_QFUNCTION_HELPER void FluxTotal_Boundary(const StateConservative F_inviscid[3], const CeedScalar stress[3][3], const CeedScalar Fe[3],
487                                               const CeedScalar normal[3], CeedScalar Flux[5]) {
488   for (CeedInt j = 0; j < 5; j++) Flux[j] = 0.;
489   for (CeedInt j = 0; j < 3; j++) {
490     Flux[0] += F_inviscid[j].density * normal[j];
491     for (CeedInt k = 0; k < 3; k++) {
492       Flux[k + 1] += (F_inviscid[j].momentum[k] - stress[k][j]) * normal[j];
493     }
494     Flux[4] += (F_inviscid[j].E_total + Fe[j]) * normal[j];
495   }
496 }
497 
498 CEED_QFUNCTION_HELPER void FluxTotal_RiemannBoundary(const StateConservative F_inviscid_normal, const CeedScalar stress[3][3], const CeedScalar Fe[3],
499                                                      const CeedScalar normal[3], CeedScalar Flux[5]) {
500   Flux[0] = F_inviscid_normal.density;
501   for (CeedInt k = 0; k < 3; k++) Flux[k + 1] = F_inviscid_normal.momentum[k];
502   Flux[4] = F_inviscid_normal.E_total;
503   for (CeedInt j = 0; j < 3; j++) {
504     for (CeedInt k = 0; k < 3; k++) {
505       Flux[k + 1] -= stress[k][j] * normal[j];
506     }
507     Flux[4] += Fe[j] * normal[j];
508   }
509 }
510 
511 CEED_QFUNCTION_HELPER void VelocityGradient(const State grad_s[3], CeedScalar grad_velocity[3][3]) {
512   grad_velocity[0][0] = grad_s[0].Y.velocity[0];
513   grad_velocity[0][1] = grad_s[1].Y.velocity[0];
514   grad_velocity[0][2] = grad_s[2].Y.velocity[0];
515   grad_velocity[1][0] = grad_s[0].Y.velocity[1];
516   grad_velocity[1][1] = grad_s[1].Y.velocity[1];
517   grad_velocity[1][2] = grad_s[2].Y.velocity[1];
518   grad_velocity[2][0] = grad_s[0].Y.velocity[2];
519   grad_velocity[2][1] = grad_s[1].Y.velocity[2];
520   grad_velocity[2][2] = grad_s[2].Y.velocity[2];
521 }
522 
523 CEED_QFUNCTION_HELPER void KMStrainRate(const CeedScalar grad_velocity[3][3], CeedScalar strain_rate[6]) {
524   const CeedScalar weight = 1 / sqrt(2.);  // Really sqrt(2.) / 2
525   strain_rate[0]          = grad_velocity[0][0];
526   strain_rate[1]          = grad_velocity[1][1];
527   strain_rate[2]          = grad_velocity[2][2];
528   strain_rate[3]          = weight * (grad_velocity[1][2] + grad_velocity[2][1]);
529   strain_rate[4]          = weight * (grad_velocity[0][2] + grad_velocity[2][0]);
530   strain_rate[5]          = weight * (grad_velocity[0][1] + grad_velocity[1][0]);
531 }
532 
533 // Kelvin-Mandel notation
534 CEED_QFUNCTION_HELPER void KMStrainRate_State(const State grad_s[3], CeedScalar strain_rate[6]) {
535   CeedScalar grad_velocity[3][3];
536   VelocityGradient(grad_s, grad_velocity);
537   KMStrainRate(grad_velocity, strain_rate);
538 }
539 
540 //@brief Given velocity gradient du_i/dx_j, return 0.5*(du_i/dx_j - du_j/dx_i)
541 CEED_QFUNCTION_HELPER void RotationRate(const CeedScalar grad_velocity[3][3], CeedScalar rotation_rate[3][3]) {
542   rotation_rate[0][0] = 0;
543   rotation_rate[1][1] = 0;
544   rotation_rate[2][2] = 0;
545   rotation_rate[1][2] = 0.5 * (grad_velocity[1][2] - grad_velocity[2][1]);
546   rotation_rate[0][2] = 0.5 * (grad_velocity[0][2] - grad_velocity[2][0]);
547   rotation_rate[0][1] = 0.5 * (grad_velocity[0][1] - grad_velocity[1][0]);
548   rotation_rate[2][1] = -rotation_rate[1][2];
549   rotation_rate[2][0] = -rotation_rate[0][2];
550   rotation_rate[1][0] = -rotation_rate[0][1];
551 }
552 
553 CEED_QFUNCTION_HELPER void NewtonianStress(NewtonianIdealGasContext gas, const CeedScalar strain_rate[6], CeedScalar stress[6]) {
554   CeedScalar div_u = strain_rate[0] + strain_rate[1] + strain_rate[2];
555   for (CeedInt i = 0; i < 6; i++) {
556     stress[i] = gas->mu * (2 * strain_rate[i] + gas->lambda * div_u * (i < 3));
557   }
558 }
559 
560 CEED_QFUNCTION_HELPER void ViscousEnergyFlux(NewtonianIdealGasContext gas, StatePrimitive Y, const State grad_s[3], const CeedScalar stress[3][3],
561                                              CeedScalar Fe[3]) {
562   for (CeedInt i = 0; i < 3; i++) {
563     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;
564   }
565 }
566 
567 CEED_QFUNCTION_HELPER void ViscousEnergyFlux_fwd(NewtonianIdealGasContext gas, StatePrimitive Y, StatePrimitive dY, const State grad_ds[3],
568                                                  const CeedScalar stress[3][3], const CeedScalar dstress[3][3], CeedScalar dFe[3]) {
569   for (CeedInt i = 0; i < 3; i++) {
570     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] -
571              Y.velocity[2] * dstress[2][i] - dY.velocity[2] * stress[2][i] - gas->k * grad_ds[i].Y.temperature;
572   }
573 }
574 
575 CEED_QFUNCTION_HELPER void Vorticity(const State grad_s[3], CeedScalar vorticity[3]) {
576   CeedScalar grad_velocity[3][3];
577   VelocityGradient(grad_s, grad_velocity);
578   Curl3(grad_velocity, vorticity);
579 }
580 
581 CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s, StateVariable state_var,
582                                                               const CeedScalar *grad_q, const CeedScalar dXdx[3][3], State grad_s[3]) {
583   CeedScalar grad_qi[5][3], dq[5][3] = {{0.}};
584 
585   GradUnpack3(Q, i, 5, grad_q, grad_qi);
586   MatMatNM((CeedScalar *)grad_qi, (CeedScalar *)dXdx, (CeedScalar *)dq, 5, 3, 3);
587   for (CeedInt j = 0; j < 3; j++) {
588     CeedScalar dqi[5];
589 
590     for (CeedInt k = 0; k < 5; k++) dqi[k] = dq[k][j];
591     grad_s[j] = StateFromQ_fwd(gas, s, dqi, state_var);
592   }
593 }
594 
595 CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_Boundary(CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s,
596                                                                        StateVariable state_var, const CeedScalar *grad_q, const CeedScalar dXdx[2][3],
597                                                                        State grad_s[3]) {
598   for (CeedInt k = 0; k < 3; k++) {
599     CeedScalar dqi[5];
600     for (CeedInt j = 0; j < 5; j++) {
601       dqi[j] = grad_q[(Q * 5) * 0 + Q * j + i] * dXdx[0][k] + grad_q[(Q * 5) * 1 + Q * j + i] * dXdx[1][k];
602     }
603     grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
604   }
605 }
606