xref: /honee/qfunctions/newtonian.h (revision ff684e427965631cdbadddfe3a31cce37a63c7b7)
1 // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2 // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3 
4 /// @file
5 /// Newtonian fluids operator for HONEE
6 #include <ceed/types.h>
7 
8 #include "newtonian_state.h"
9 #include "newtonian_types.h"
10 #include "stabilization.h"
11 #include "utils.h"
12 
13 CEED_QFUNCTION_HELPER void InternalDampingLayer(const NewtonianIdealGasContext context, const State s, const CeedScalar sigma, CeedScalar damp_Y[5],
14                                                 CeedScalar damp_residual[5]) {
15   ScaleN(damp_Y, sigma, 5);
16   State damp_s = StateFromY_fwd(context, s, damp_Y);
17 
18   CeedScalar U[5];
19   UnpackState_U(damp_s.U, U);
20   for (int i = 0; i < 5; i++) damp_residual[i] += U[i];
21 }
22 
23 // *****************************************************************************
24 // This QFunction sets a "still" initial condition for generic Newtonian IG problems
25 // *****************************************************************************
26 CEED_QFUNCTION_HELPER int ICsNewtonianIG(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
27   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
28 
29   const SetupContext context = (SetupContext)ctx;
30 
31   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
32     CeedScalar q[5];
33     State      s = StateFromPrimitive(&context->gas, context->reference);
34     StateToQ(&context->gas, s, q, state_var);
35     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
36   }
37   return 0;
38 }
39 
40 CEED_QFUNCTION(ICsNewtonianIG_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
41   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
42 }
43 
44 CEED_QFUNCTION(ICsNewtonianIG_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
45   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_PRIMITIVE);
46 }
47 
48 CEED_QFUNCTION(ICsNewtonianIG_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
49   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_ENTROPY);
50 }
51 
52 CEED_QFUNCTION_HELPER int MassFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
53   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
54   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[1];
55   const CeedScalar(*q_data)            = in[2];
56   CeedScalar(*v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
57   CeedScalar(*Grad_v)[5][CEED_Q_VLA]   = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
58 
59   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
60 
61   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
62     const CeedScalar qi[5]     = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
63     const CeedScalar qi_dot[5] = {q_dot[0][i], q_dot[1][i], q_dot[2][i], q_dot[3][i], q_dot[4][i]};
64     const State      s         = StateFromQ(context, qi, state_var);
65     const State      s_dot     = StateFromQ(context, qi_dot, state_var);
66     CeedScalar       wdetJ, dXdx[3][3];
67     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
68 
69     // Standard mass matrix term
70     for (CeedInt f = 0; f < 5; f++) {
71       v[f][i] = wdetJ * qi_dot[f];
72     }
73 
74     // Stabilization method: none (Galerkin), SU, or SUPG
75     State      grad_s[3] = {{{0.}}};
76     CeedScalar Tau_d[3], stab[5][3], body_force[5] = {0.}, divFdiff[5] = {0.}, U_dot[5];
77     UnpackState_U(s_dot.U, U_dot);
78     Tau_diagPrim(context, s, dXdx, context->dt, Tau_d);
79     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff, stab);
80 
81     // Stabilized mass term
82     for (CeedInt j = 0; j < 5; j++) {
83       for (CeedInt k = 0; k < 3; k++) {
84         Grad_v[k][j][i] = wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]);
85       }
86     }
87   }
88   return 0;
89 }
90 
91 CEED_QFUNCTION(MassFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
92   return MassFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
93 }
94 
95 // *****************************************************************************
96 // This QFunction implements the following formulation of Navier-Stokes with explicit time stepping method
97 //
98 // This is 3D compressible Navier-Stokes in conservation form with state variables of density, momentum density, and total energy density.
99 //
100 // State Variables: q = ( rho, U1, U2, U3, E )
101 //   rho - Mass Density
102 //   Ui  - Momentum Density,      Ui = rho ui
103 //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
104 //
105 // Navier-Stokes Equations:
106 //   drho/dt + div( U )                               = 0
107 //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
108 //   dE/dt   + div( (E + P) u )                       = div( Fe )
109 //
110 // Viscous Stress:
111 //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
112 //
113 // Thermal Stress:
114 //   Fe = u Fu + k grad( T )
115 // Equation of State
116 //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
117 //
118 // Stabilization:
119 //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
120 //     f1 = rho  sqrt(ui uj gij)
121 //     gij = dXi/dX * dXi/dX
122 //     TauC = Cc f1 / (8 gii)
123 //     TauM = min( 1 , 1 / f1 )
124 //     TauE = TauM / (Ce cv)
125 //
126 //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
127 //
128 // Constants:
129 //   lambda = - 2 / 3,  From Stokes hypothesis
130 //   mu              ,  Dynamic viscosity
131 //   k               ,  Thermal conductivity
132 //   cv              ,  Specific heat, constant volume
133 //   cp              ,  Specific heat, constant pressure
134 //   g               ,  Gravity
135 //   gamma  = cp / cv,  Specific heat ratio
136 //
137 // We require the product of the inverse of the Jacobian (dXdx_j,k) and its transpose (dXdx_k,j) to properly compute integrals of the form: int( gradv
138 // gradu )
139 // *****************************************************************************
140 CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
141   const CeedScalar(*q)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[0];
142   const CeedScalar(*Grad_q)          = in[1];
143   const CeedScalar(*q_data)          = in[2];
144   const CeedScalar(*x)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[3];
145   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
146   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
147 
148   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
149   const CeedScalar        *g       = context->g;
150   const CeedScalar         dt      = context->dt;
151   const CeedScalar         P0      = context->idl_pressure;
152 
153   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
154     CeedScalar       U[5], wdetJ, dXdx[3][3];
155     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
156     for (int j = 0; j < 5; j++) U[j] = q[j][i];
157     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
158     State s = StateFromU(context, U);
159 
160     State grad_s[3];
161     StatePhysicalGradientFromReference(Q, i, context, s, STATEVAR_CONSERVATIVE, Grad_q, dXdx, grad_s);
162 
163     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
164     KMStrainRate_State(grad_s, strain_rate);
165     NewtonianStress(context, strain_rate, kmstress);
166     KMUnpack(kmstress, stress);
167     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
168 
169     StateConservative F_inviscid[3];
170     FluxInviscid(context, s, F_inviscid);
171 
172     // Total flux
173     CeedScalar Flux[5][3];
174     FluxTotal(F_inviscid, stress, Fe, Flux);
175 
176     for (CeedInt j = 0; j < 5; j++) {
177       for (CeedInt k = 0; k < 3; k++) Grad_v[k][j][i] = wdetJ * (dXdx[k][0] * Flux[j][0] + dXdx[k][1] * Flux[j][1] + dXdx[k][2] * Flux[j][2]);
178     }
179 
180     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], Dot3(s.U.momentum, g)};
181     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
182 
183     if (context->idl_enable) {
184       const CeedScalar sigma         = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
185       CeedScalar       damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
186       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
187       for (int j = 0; j < 5; j++) v[j][i] -= wdetJ * idl_residual[j];
188     }
189 
190     // -- Stabilization method: none (Galerkin), SU, or SUPG
191     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, zeroFlux[5] = {0.};
192     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
193     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, zeroFlux, stab);
194 
195     for (CeedInt j = 0; j < 5; j++) {
196       for (CeedInt k = 0; k < 3; k++) Grad_v[k][j][i] -= wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]);
197     }
198   }
199   return 0;
200 }
201 
202 // *****************************************************************************
203 // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
204 //
205 //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
206 //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
207 //                                       (diffusive terms will be added later)
208 // *****************************************************************************
209 CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
210   NewtonianIdealGasContext context      = (NewtonianIdealGasContext)ctx;
211   const bool               use_divFdiff = context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE;
212 
213   const CeedScalar(*q)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[0];
214   const CeedScalar(*Grad_q)               = in[1];
215   const CeedScalar(*q_dot)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])in[2];
216   const CeedScalar(*q_data)               = in[3];
217   const CeedScalar(*x)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[4];
218   const CeedScalar(*divFdiff)[CEED_Q_VLA] = use_divFdiff ? (const CeedScalar(*)[CEED_Q_VLA])in[5] : NULL;
219   CeedScalar(*v)[CEED_Q_VLA]              = (CeedScalar(*)[CEED_Q_VLA])out[0];
220   CeedScalar(*Grad_v)[5][CEED_Q_VLA]      = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
221   CeedScalar(*jac_data)                   = out[2];
222 
223   const CeedScalar *g            = context->g;
224   const CeedScalar  dt           = context->dt;
225   const CeedScalar  idl_pressure = context->idl_pressure;
226 
227   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
228     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
229     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
230     const State      s      = StateFromQ(context, qi, state_var);
231 
232     CeedScalar wdetJ, dXdx[3][3];
233     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
234     State grad_s[3];
235     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
236 
237     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
238     KMStrainRate_State(grad_s, strain_rate);
239     NewtonianStress(context, strain_rate, kmstress);
240     KMUnpack(kmstress, stress);
241     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
242 
243     StateConservative F_inviscid[3];
244     FluxInviscid(context, s, F_inviscid);
245 
246     // Total flux
247     CeedScalar Flux[5][3];
248     FluxTotal(F_inviscid, stress, Fe, Flux);
249 
250     for (CeedInt j = 0; j < 5; j++) {
251       for (CeedInt k = 0; k < 3; k++) {
252         Grad_v[k][j][i] = -wdetJ * (dXdx[k][0] * Flux[j][0] + dXdx[k][1] * Flux[j][1] + dXdx[k][2] * Flux[j][2]);
253       }
254     }
255 
256     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], Dot3(s.U.momentum, g)};
257 
258     // -- Stabilization method: none (Galerkin), SU, or SUPG
259     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5];
260     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
261     State s_dot = StateFromQ_fwd(context, s, qi_dot, state_var);
262     UnpackState_U(s_dot.U, U_dot);
263 
264     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
265     if (context->idl_enable) {
266       const CeedScalar sigma = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
267       StoredValuesPack(Q, i, 14, 1, &sigma, jac_data);
268       CeedScalar damp_state[5] = {s.Y.pressure - idl_pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
269       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
270       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
271     }
272 
273     CeedScalar divFdiff_i[5] = {0.};
274     if (use_divFdiff) {
275       for (int j = 1; j < 5; j++) divFdiff_i[j] = divFdiff[j - 1][i];
276     }
277     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
278     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff_i, stab);
279 
280     for (CeedInt j = 0; j < 5; j++) {
281       for (CeedInt k = 0; k < 3; k++) {
282         Grad_v[k][j][i] += wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]);
283       }
284     }
285     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
286     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
287     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
288   }
289   return 0;
290 }
291 
292 CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
293   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
294 }
295 
296 CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
297   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
298 }
299 
300 CEED_QFUNCTION(IFunction_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
301   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
302 }
303 
304 // *****************************************************************************
305 // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
306 // *****************************************************************************
307 CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
308   const CeedScalar(*dq)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[0];
309   const CeedScalar(*Grad_dq)         = in[1];
310   const CeedScalar(*q_data)          = in[2];
311   const CeedScalar(*jac_data)        = in[3];
312   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
313   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
314 
315   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
316   const CeedScalar        *g       = context->g;
317 
318   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
319     CeedScalar wdetJ, dXdx[3][3];
320     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
321 
322     CeedScalar qi[5], kmstress[6], Tau_d[3];
323     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
324     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
325     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
326     State s = StateFromQ(context, qi, state_var);
327 
328     CeedScalar dqi[5];
329     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
330     State ds = StateFromQ_fwd(context, s, dqi, state_var);
331 
332     State grad_ds[3];
333     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
334 
335     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
336     KMStrainRate_State(grad_ds, dstrain_rate);
337     NewtonianStress(context, dstrain_rate, dkmstress);
338     KMUnpack(dkmstress, dstress);
339     KMUnpack(kmstress, stress);
340     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
341 
342     StateConservative dF_inviscid[3];
343     FluxInviscid_fwd(context, s, ds, dF_inviscid);
344 
345     // Total flux
346     CeedScalar dFlux[5][3];
347     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
348 
349     for (int j = 0; j < 5; j++) {
350       for (int k = 0; k < 3; k++) Grad_v[k][j][i] = -wdetJ * (dXdx[k][0] * dFlux[j][0] + dXdx[k][1] * dFlux[j][1] + dXdx[k][2] * dFlux[j][2]);
351     }
352 
353     const CeedScalar dbody_force[5] = {0, ds.U.density * g[0], ds.U.density * g[1], ds.U.density * g[2], Dot3(ds.U.momentum, g)};
354     CeedScalar       dU[5]          = {0.};
355     UnpackState_U(ds.U, dU);
356     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
357 
358     if (context->idl_enable) {
359       const CeedScalar sigma         = jac_data[14 * Q + i];
360       CeedScalar       damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
361       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
362       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
363       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
364     }
365 
366     // -- Stabilization method: none (Galerkin), SU, or SUPG
367     CeedScalar dstab[5][3], U_dot[5] = {0};
368     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
369     const CeedScalar zeroFlux[5] = {0.};
370     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, zeroFlux, dstab);
371 
372     for (int j = 0; j < 5; j++) {
373       for (int k = 0; k < 3; k++) Grad_v[k][j][i] += wdetJ * (dstab[j][0] * dXdx[k][0] + dstab[j][1] * dXdx[k][1] + dstab[j][2] * dXdx[k][2]);
374     }
375   }
376   return 0;
377 }
378 
379 CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
380   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
381 }
382 
383 CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
384   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
385 }
386 
387 CEED_QFUNCTION(IJacobian_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
388   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
389 }
390 
391 // *****************************************************************************
392 // Compute boundary integral (ie. for strongly set inflows)
393 // *****************************************************************************
394 CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
395   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
396   const CeedScalar(*q)[CEED_Q_VLA]       = (const CeedScalar(*)[CEED_Q_VLA])in[0];
397   const CeedScalar(*Grad_q)              = in[1];
398   const CeedScalar(*q_data_sur)          = in[2];
399   CeedScalar(*v)[CEED_Q_VLA]             = (CeedScalar(*)[CEED_Q_VLA])out[0];
400   CeedScalar(*jac_data_sur)              = context->is_implicit ? out[1] : NULL;
401 
402   const bool is_implicit = context->is_implicit;
403 
404   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
405     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
406     State            s     = StateFromQ(context, qi, state_var);
407 
408     CeedScalar wdetJb, dXdx[2][3], normal[3];
409     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
410     wdetJb *= is_implicit ? -1. : 1.;
411 
412     State grad_s[3];
413     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
414 
415     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
416     KMStrainRate_State(grad_s, strain_rate);
417     NewtonianStress(context, strain_rate, kmstress);
418     KMUnpack(kmstress, stress);
419     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
420 
421     StateConservative F_inviscid[3];
422     FluxInviscid(context, s, F_inviscid);
423 
424     CeedScalar Flux[5];
425     FluxTotal_Boundary(F_inviscid, stress, Fe, normal, Flux);
426 
427     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
428 
429     if (is_implicit) {
430       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
431       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
432     }
433   }
434   return 0;
435 }
436 
437 CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
438   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
439 }
440 
441 CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
442   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
443 }
444 
445 CEED_QFUNCTION(BoundaryIntegral_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
446   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_ENTROPY);
447 }
448 
449 // *****************************************************************************
450 // Jacobian for "set nothing" boundary integral
451 // *****************************************************************************
452 CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
453                                                     StateVariable state_var) {
454   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
455   const CeedScalar(*Grad_dq)        = in[1];
456   const CeedScalar(*q_data_sur)     = in[2];
457   const CeedScalar(*jac_data_sur)   = in[4];
458   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
459 
460   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
461   const bool                     is_implicit = context->is_implicit;
462 
463   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
464     CeedScalar wdetJb, dXdx[2][3], normal[3];
465     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
466     wdetJb *= is_implicit ? -1. : 1.;
467 
468     CeedScalar qi[5], kmstress[6], dqi[5];
469     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
470     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
471     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
472 
473     State s  = StateFromQ(context, qi, state_var);
474     State ds = StateFromQ_fwd(context, s, dqi, state_var);
475 
476     State grad_ds[3];
477     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
478 
479     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
480     KMStrainRate_State(grad_ds, dstrain_rate);
481     NewtonianStress(context, dstrain_rate, dkmstress);
482     KMUnpack(dkmstress, dstress);
483     KMUnpack(kmstress, stress);
484     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
485 
486     StateConservative dF_inviscid[3];
487     FluxInviscid_fwd(context, s, ds, dF_inviscid);
488 
489     CeedScalar dFlux[5];
490     FluxTotal_Boundary(dF_inviscid, dstress, dFe, normal, dFlux);
491 
492     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
493   }
494   return 0;
495 }
496 
497 CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
498   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
499 }
500 
501 CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
502   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
503 }
504 
505 CEED_QFUNCTION(BoundaryIntegral_Jacobian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
506   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_ENTROPY);
507 }
508 
509 // @brief Volume integral for RHS of divergence of diffusive flux direct projection
510 CEED_QFUNCTION_HELPER int DivDiffusiveFluxVolumeRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
511                                                        StateVariable state_var) {
512   const CeedScalar(*q)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[0];
513   const CeedScalar(*Grad_q)          = in[1];
514   const CeedScalar(*q_data)          = in[2];
515   CeedScalar(*Grad_v)[4][CEED_Q_VLA] = (CeedScalar(*)[4][CEED_Q_VLA])out[0];
516 
517   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
518   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
519 
520   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
521     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
522     const State      s     = StateFromQ(context, qi, state_var);
523     CeedScalar       wdetJ, dXdx[3][3];
524     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
525 
526     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
527     {  // Get stress and Fe
528       State      grad_s[3];
529       CeedScalar strain_rate[6], kmstress[6];
530 
531       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
532       KMStrainRate_State(grad_s, strain_rate);
533       NewtonianStress(context, strain_rate, kmstress);
534       KMUnpack(kmstress, stress);
535       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
536     }
537 
538     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
539 
540     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
541       for (CeedInt k = 0; k < 3; k++) {
542         Grad_v[k][j - 1][i] = -wdetJ * Dot3(dXdx[k], Fdiff[j]);
543       }
544     }
545   }
546   return 0;
547 }
548 
549 CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
550   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
551 }
552 
553 CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
554   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
555 }
556 
557 CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
558   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
559 }
560 
561 // @brief Boundary integral for RHS of divergence of diffusive flux direct projection
562 CEED_QFUNCTION_HELPER int DivDiffusiveFluxBoundaryRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
563                                                          StateVariable state_var) {
564   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
565   const CeedScalar(*Grad_q)        = in[1];
566   const CeedScalar(*q_data)        = in[2];
567   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
568 
569   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
570   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
571 
572   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
573     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
574     const State      s     = StateFromQ(context, qi, state_var);
575     CeedScalar       wdetJ, dXdx[3][3], normal[3];
576     CeedScalar       stress[3][3], Fe[3], Fdiff[5];
577 
578     QdataBoundaryGradientUnpack_3D(Q, i, q_data, &wdetJ, dXdx, normal);
579     {  // Get stress and Fe
580       State      grad_s[3];
581       CeedScalar strain_rate[6], kmstress[6];
582 
583       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
584       KMStrainRate_State(grad_s, strain_rate);
585       NewtonianStress(context, strain_rate, kmstress);
586       KMUnpack(kmstress, stress);
587       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
588     }
589 
590     FluxTotal_Boundary(ZeroInviscidFluxes, stress, Fe, normal, Fdiff);
591 
592     // Continuity has no diffusive flux, therefore skip
593     for (CeedInt j = 1; j < 5; j++) v[j - 1][i] = wdetJ * Fdiff[j];
594   }
595   return 0;
596 }
597 
598 CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
599   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
600 }
601 
602 CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
603   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
604 }
605 
606 CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
607   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
608 }
609 
610 // @brief Integral for RHS of diffusive flux indirect projection
611 CEED_QFUNCTION_HELPER int DiffusiveFluxRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
612   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
613   const CeedScalar(*Grad_q)        = in[1];
614   const CeedScalar(*q_data)        = in[2];
615   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
616 
617   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
618   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
619 
620   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
621     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
622     const State      s     = StateFromQ(context, qi, state_var);
623     CeedScalar       wdetJ, dXdx[3][3];
624     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
625 
626     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
627     {  // Get stress and Fe
628       State      grad_s[3];
629       CeedScalar strain_rate[6], kmstress[6];
630 
631       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
632       KMStrainRate_State(grad_s, strain_rate);
633       NewtonianStress(context, strain_rate, kmstress);
634       KMUnpack(kmstress, stress);
635       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
636     }
637 
638     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
639 
640     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
641       for (CeedInt k = 0; k < 3; k++) {
642         v[(j - 1) * 3 + k][i] = wdetJ * Fdiff[j][k];
643       }
644     }
645   }
646   return 0;
647 }
648 
649 CEED_QFUNCTION(DiffusiveFluxRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
650   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
651 }
652 
653 CEED_QFUNCTION(DiffusiveFluxRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
654   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
655 }
656 
657 CEED_QFUNCTION(DiffusiveFluxRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
658   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
659 }
660