xref: /honee/qfunctions/newtonian.h (revision 30b5892f5b0732c24c19551ffa26ae78a7299d4d)
1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
33a8779fbSJames Wright 
43a8779fbSJames Wright /// @file
5ea615d4cSJames Wright /// Newtonian fluids operator for HONEE
63e17a7a1SJames Wright #include <ceed/types.h>
72b916ea7SJeremy L Thompson 
8475b2820SJames Wright #include "newtonian_state.h"
9d0cce58aSJeremy L Thompson #include "newtonian_types.h"
10d1b9ef12SLeila Ghaffari #include "stabilization.h"
11d0cce58aSJeremy L Thompson #include "utils.h"
12bb8a0c61SJames Wright 
13bb8a0c61SJames Wright // *****************************************************************************
143a8779fbSJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
153a8779fbSJames Wright // *****************************************************************************
168fff8293SJames Wright CEED_QFUNCTION_HELPER int ICsNewtonianIG(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
173a8779fbSJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
183a8779fbSJames Wright 
19bb8a0c61SJames Wright   const SetupContext context = (SetupContext)ctx;
20bb8a0c61SJames Wright 
212b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
22a541e550SJames Wright     CeedScalar q[5];
23edcfef1bSKenneth E. Jansen     State      s = StateFromPrimitive(&context->gas, context->reference);
248fff8293SJames Wright     StateToQ(&context->gas, s, q, state_var);
252b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
26b193fadcSJames Wright   }
273a8779fbSJames Wright   return 0;
283a8779fbSJames Wright }
293a8779fbSJames Wright 
309b103f75SJames Wright CEED_QFUNCTION(ICsNewtonianIG_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
319b103f75SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
329b103f75SJames Wright }
339b103f75SJames Wright 
342b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsNewtonianIG_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
358fff8293SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_PRIMITIVE);
36b8fb7609SAdeleke O. Bankole }
379b103f75SJames Wright 
389b103f75SJames Wright CEED_QFUNCTION(ICsNewtonianIG_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
399b103f75SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_ENTROPY);
40cbe60e31SLeila Ghaffari }
41cbe60e31SLeila Ghaffari 
4297cfd714SJames Wright CEED_QFUNCTION_HELPER int MassFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
4365dee3d2SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
4465dee3d2SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[1];
4565dee3d2SJames Wright   const CeedScalar(*q_data)            = in[2];
4665dee3d2SJames Wright   CeedScalar(*v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
4765dee3d2SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]   = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
4865dee3d2SJames Wright 
4965dee3d2SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
5065dee3d2SJames Wright 
5165dee3d2SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
5265dee3d2SJames Wright     const CeedScalar qi[5]     = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
5365dee3d2SJames Wright     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]};
5465dee3d2SJames Wright     const State      s         = StateFromQ(context, qi, state_var);
5565dee3d2SJames Wright     const State      s_dot     = StateFromQ(context, qi_dot, state_var);
5665dee3d2SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
5765dee3d2SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
5865dee3d2SJames Wright 
5965dee3d2SJames Wright     // Standard mass matrix term
6065dee3d2SJames Wright     for (CeedInt f = 0; f < 5; f++) {
6165dee3d2SJames Wright       v[f][i] = wdetJ * qi_dot[f];
6265dee3d2SJames Wright     }
6365dee3d2SJames Wright 
6465dee3d2SJames Wright     // Stabilization method: none (Galerkin), SU, or SUPG
6565dee3d2SJames Wright     State      grad_s[3] = {{{0.}}};
668c85b835SJames Wright     CeedScalar Tau_d[3], stab[5][3], body_force[5] = {0.}, divFdiff[5] = {0.}, U_dot[5];
6765dee3d2SJames Wright     UnpackState_U(s_dot.U, U_dot);
6865dee3d2SJames Wright     Tau_diagPrim(context, s, dXdx, context->dt, Tau_d);
698c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff, stab);
7065dee3d2SJames Wright 
7165dee3d2SJames Wright     // Stabilized mass term
7265dee3d2SJames Wright     for (CeedInt j = 0; j < 5; j++) {
7365dee3d2SJames Wright       for (CeedInt k = 0; k < 3; k++) {
7465dee3d2SJames Wright         Grad_v[k][j][i] = wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]);
7565dee3d2SJames Wright       }
7665dee3d2SJames Wright     }
7765dee3d2SJames Wright   }
7897cfd714SJames Wright   return 0;
7965dee3d2SJames Wright }
8065dee3d2SJames Wright 
8165dee3d2SJames Wright CEED_QFUNCTION(MassFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
8297cfd714SJames Wright   return MassFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
8365dee3d2SJames Wright }
8465dee3d2SJames Wright 
85*30b5892fSJames Wright //TODO: Document function
86*30b5892fSJames Wright CEED_QFUNCTION_HELPER void InternalDampingLayer_Residual(const NewtonianIdealGasContext context, const State s, const CeedScalar sigma,
87*30b5892fSJames Wright                                                          CeedScalar damp_Y[5], CeedScalar damp_residual[5]) {
88*30b5892fSJames Wright   ScaleN(damp_Y, sigma, 5);
89*30b5892fSJames Wright   State damp_s = StateFromY_fwd(context, s, damp_Y);
90*30b5892fSJames Wright 
91*30b5892fSJames Wright   CeedScalar U[5];
92*30b5892fSJames Wright   UnpackState_U(damp_s.U, U);
93*30b5892fSJames Wright   for (int i = 0; i < 5; i++) damp_residual[i] += U[i];
94*30b5892fSJames Wright }
95*30b5892fSJames Wright 
96*30b5892fSJames Wright //TODO: Document function (label v_i and dv_i as [inout])
97*30b5892fSJames Wright CEED_QFUNCTION_HELPER void InternalDampingLayer_IFunction_Integrand(const State s, const NewtonianIdealGasContext context, CeedScalar amplitude,
98*30b5892fSJames Wright                                                                     CeedScalar length, CeedScalar start, CeedScalar location, CeedScalar pressure,
99*30b5892fSJames Wright                                                                     CeedScalar v_i[5], CeedScalar *sigma) {
100*30b5892fSJames Wright   const CeedScalar sigma_                    = LinearRampCoefficient(amplitude, length, start, location);
101*30b5892fSJames Wright   CeedScalar damp_state[5] = {s.Y.pressure - pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
102*30b5892fSJames Wright   InternalDampingLayer_Residual(context, s, sigma_, damp_state, idl_residual);
103*30b5892fSJames Wright   AXPY(1, idl_residual, v_i, 5);
104*30b5892fSJames Wright   *sigma = sigma_;
105*30b5892fSJames Wright }
106*30b5892fSJames Wright 
107cbe60e31SLeila Ghaffari // *****************************************************************************
10804e40bb6SJeremy L Thompson // This QFunction implements the following formulation of Navier-Stokes with explicit time stepping method
1093a8779fbSJames Wright //
11004e40bb6SJeremy L Thompson // This is 3D compressible Navier-Stokes in conservation form with state variables of density, momentum density, and total energy density.
1113a8779fbSJames Wright //
1123a8779fbSJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
1133a8779fbSJames Wright //   rho - Mass Density
1143a8779fbSJames Wright //   Ui  - Momentum Density,      Ui = rho ui
1153a8779fbSJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
1163a8779fbSJames Wright //
1173a8779fbSJames Wright // Navier-Stokes Equations:
1183a8779fbSJames Wright //   drho/dt + div( U )                               = 0
1193a8779fbSJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
1203a8779fbSJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
1213a8779fbSJames Wright //
1223a8779fbSJames Wright // Viscous Stress:
1233a8779fbSJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
1243a8779fbSJames Wright //
1253a8779fbSJames Wright // Thermal Stress:
1263a8779fbSJames Wright //   Fe = u Fu + k grad( T )
127bb8a0c61SJames Wright // Equation of State
1283a8779fbSJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
1293a8779fbSJames Wright //
1303a8779fbSJames Wright // Stabilization:
1313a8779fbSJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
1323a8779fbSJames Wright //     f1 = rho  sqrt(ui uj gij)
1333a8779fbSJames Wright //     gij = dXi/dX * dXi/dX
1343a8779fbSJames Wright //     TauC = Cc f1 / (8 gii)
1353a8779fbSJames Wright //     TauM = min( 1 , 1 / f1 )
1363a8779fbSJames Wright //     TauE = TauM / (Ce cv)
1373a8779fbSJames Wright //
1383a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
1393a8779fbSJames Wright //
1403a8779fbSJames Wright // Constants:
1413a8779fbSJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
1423a8779fbSJames Wright //   mu              ,  Dynamic viscosity
1433a8779fbSJames Wright //   k               ,  Thermal conductivity
1443a8779fbSJames Wright //   cv              ,  Specific heat, constant volume
1453a8779fbSJames Wright //   cp              ,  Specific heat, constant pressure
1463a8779fbSJames Wright //   g               ,  Gravity
1473a8779fbSJames Wright //   gamma  = cp / cv,  Specific heat ratio
1483a8779fbSJames Wright //
14904e40bb6SJeremy L Thompson // 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
15004e40bb6SJeremy L Thompson // gradu )
1513a8779fbSJames Wright // *****************************************************************************
1522b916ea7SJeremy L Thompson CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
153b3b24828SJames Wright   NewtonianIdealGasContext context      = (NewtonianIdealGasContext)ctx;
154b3b24828SJames Wright   const bool               use_divFdiff = context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE;
155b3b24828SJames Wright 
1563d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[0];
15787bd45e7SJames Wright   const CeedScalar(*Grad_q)               = in[1];
158ade49511SJames Wright   const CeedScalar(*q_data)               = in[2];
1590a32a5aaSJames Wright   const CeedScalar(*x)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[3];
160b3b24828SJames Wright   const CeedScalar(*divFdiff)[CEED_Q_VLA] = use_divFdiff ? (const CeedScalar(*)[CEED_Q_VLA])in[4] : NULL;
1613d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]              = (CeedScalar(*)[CEED_Q_VLA])out[0];
1623d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]      = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
1633a8779fbSJames Wright 
164bb8a0c61SJames Wright   const CeedScalar *g            = context->g;
165bb8a0c61SJames Wright   const CeedScalar  dt           = context->dt;
166b3b24828SJames Wright   const CeedScalar  idl_pressure = context->idl_pressure;
1673a8779fbSJames Wright 
1683d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
169ade49511SJames Wright     CeedScalar       U[5], wdetJ, dXdx[3][3];
1700a32a5aaSJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
171c1a52365SJed Brown     for (int j = 0; j < 5; j++) U[j] = q[j][i];
1721be49596SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
173edcfef1bSKenneth E. Jansen     State s = StateFromU(context, U);
174c1a52365SJed Brown 
175c1a52365SJed Brown     State grad_s[3];
176edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, STATEVAR_CONSERVATIVE, Grad_q, dXdx, grad_s);
177c1a52365SJed Brown 
178c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
17940a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
180c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
181c1a52365SJed Brown     KMUnpack(kmstress, stress);
182c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
183c1a52365SJed Brown 
184c1a52365SJed Brown     StateConservative F_inviscid[3];
185c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
186c1a52365SJed Brown 
187c1a52365SJed Brown     // Total flux
188c1a52365SJed Brown     CeedScalar Flux[5][3];
189d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
190c1a52365SJed Brown 
1917523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
1927523f6aaSJames Wright       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]);
1932b916ea7SJeremy L Thompson     }
194c1a52365SJed Brown 
19560dbb574SKenneth E. Jansen     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)};
1962b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
1973a8779fbSJames Wright 
1980a32a5aaSJames Wright     if (context->idl_enable) {
1990a32a5aaSJames Wright       const CeedScalar sigma         = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
200b3b24828SJames Wright       CeedScalar       damp_state[5] = {s.Y.pressure - idl_pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
201*30b5892fSJames Wright       InternalDampingLayer_Residual(context, s, sigma, damp_state, idl_residual);
2020a32a5aaSJames Wright       for (int j = 0; j < 5; j++) v[j][i] -= wdetJ * idl_residual[j];
2030a32a5aaSJames Wright     }
2040a32a5aaSJames Wright 
205b3b24828SJames Wright     CeedScalar divFdiff_i[5] = {0.};
206b3b24828SJames Wright     if (use_divFdiff)
207b3b24828SJames Wright       for (int j = 1; j < 5; j++) divFdiff_i[j] = divFdiff[j - 1][i];
208b3b24828SJames Wright 
209d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
210b3b24828SJames Wright     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0};
211d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
212b3b24828SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff_i, stab);
2133a8779fbSJames Wright 
2142b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
2152b916ea7SJeremy L Thompson       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]);
2162b916ea7SJeremy L Thompson     }
217b193fadcSJames Wright   }
2183a8779fbSJames Wright   return 0;
2193a8779fbSJames Wright }
2203a8779fbSJames Wright 
2213a8779fbSJames Wright // *****************************************************************************
22204e40bb6SJeremy L Thompson // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
2233a8779fbSJames Wright //
2243a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
2253a8779fbSJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
22604e40bb6SJeremy L Thompson //                                       (diffusive terms will be added later)
2273a8779fbSJames Wright // *****************************************************************************
228*30b5892fSJames Wright //TODO: Document function (label v_i and dv_i as [inout])
229*30b5892fSJames Wright CEED_QFUNCTION_HELPER void IFunction_Newtonian_Integrand(const State s, const State grad_s[3], const State s_dot, const CeedScalar divFdiff_i[5],
230*30b5892fSJames Wright                                                          const CeedScalar x_i[3], const NewtonianIdealGasContext context, const CeedScalar dXdx[3][3],
231*30b5892fSJames Wright                                                          CeedScalar v_i[5], CeedScalar dv_i[5][3], CeedScalar kmstress[6], CeedScalar Tau_d[3]) {
232*30b5892fSJames Wright   const CeedScalar *g  = context->g;
233*30b5892fSJames Wright   const CeedScalar  dt = context->dt;
234*30b5892fSJames Wright 
235*30b5892fSJames Wright   CeedScalar strain_rate[6], stress[3][3], Fe[3];
236*30b5892fSJames Wright   KMStrainRate_State(grad_s, strain_rate);
237*30b5892fSJames Wright   NewtonianStress(context, strain_rate, kmstress);
238*30b5892fSJames Wright   KMUnpack(kmstress, stress);
239*30b5892fSJames Wright   ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
240*30b5892fSJames Wright 
241*30b5892fSJames Wright   StateConservative F_inviscid[3];
242*30b5892fSJames Wright   FluxInviscid(context, s, F_inviscid);
243*30b5892fSJames Wright 
244*30b5892fSJames Wright   // Total flux
245*30b5892fSJames Wright   CeedScalar Flux[5][3];
246*30b5892fSJames Wright   FluxTotal(F_inviscid, stress, Fe, Flux);
247*30b5892fSJames Wright 
248*30b5892fSJames Wright   AXPY(-1, (CeedScalar *)Flux, (CeedScalar *)dv_i, 15);
249*30b5892fSJames Wright 
250*30b5892fSJames Wright   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)};
251*30b5892fSJames Wright 
252*30b5892fSJames Wright   // -- Stabilization method: none (Galerkin), SU, or SUPG
253*30b5892fSJames Wright   CeedScalar stab[5][3], U_dot[5] = {0};
254*30b5892fSJames Wright   UnpackState_U(s_dot.U, U_dot);
255*30b5892fSJames Wright 
256*30b5892fSJames Wright   for (CeedInt j = 0; j < 5; j++) v_i[j] += U_dot[j] - body_force[j];
257*30b5892fSJames Wright   Tau_diagPrim(context, s, dXdx, dt, Tau_d);
258*30b5892fSJames Wright   Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff_i, stab);
259*30b5892fSJames Wright   AXPY(1, (CeedScalar *)stab, (CeedScalar *)dv_i, 15);
260*30b5892fSJames Wright }
261*30b5892fSJames Wright 
2628fff8293SJames Wright CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
2638c85b835SJames Wright   NewtonianIdealGasContext context      = (NewtonianIdealGasContext)ctx;
264ff684e42SJames Wright   const bool               use_divFdiff = context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE;
2658c85b835SJames Wright 
2663d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[0];
26787bd45e7SJames Wright   const CeedScalar(*Grad_q)               = in[1];
2683d65b166SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])in[2];
269ade49511SJames Wright   const CeedScalar(*q_data)               = in[3];
2703d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[4];
271ff684e42SJames Wright   const CeedScalar(*divFdiff)[CEED_Q_VLA] = use_divFdiff ? (const CeedScalar(*)[CEED_Q_VLA])in[5] : NULL;
2723d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]              = (CeedScalar(*)[CEED_Q_VLA])out[0];
2733d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]      = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
274ade49511SJames Wright   CeedScalar(*jac_data)                   = out[2];
2753d65b166SJames Wright 
2763d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2773d65b166SJames Wright     const CeedScalar qi[5]     = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
278*30b5892fSJames Wright     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]};
279c1a52365SJed Brown     const CeedScalar x_i[3]    = {x[0][i], x[1][i], x[2][i]};
280edcfef1bSKenneth E. Jansen     const State      s         = StateFromQ(context, qi, state_var);
281*30b5892fSJames Wright     const State      s_dot     = StateFromQ_fwd(context, s, qi_dot, state_var);
282c1a52365SJed Brown 
283ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
284ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
285c1a52365SJed Brown     State grad_s[3];
286edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
2878c85b835SJames Wright     CeedScalar divFdiff_i[5] = {0.};
288*30b5892fSJames Wright     if (use_divFdiff)
2898c85b835SJames Wright       for (int j = 1; j < 5; j++) divFdiff_i[j] = divFdiff[j - 1][i];
2903a8779fbSJames Wright 
291*30b5892fSJames Wright     CeedScalar v_i[5] = {0.}, dv_i[5][3] = {{0.}}, kmstress[6], Tau_d[3], sigma = 0.;
292*30b5892fSJames Wright     IFunction_Newtonian_Integrand(s, grad_s, s_dot, divFdiff_i, x_i, context, dXdx, v_i, dv_i, kmstress, Tau_d);
293*30b5892fSJames Wright     if (context->idl_enable)
294*30b5892fSJames Wright       InternalDampingLayer_IFunction_Integrand(s, context, context->idl_amplitude, context->idl_length, context->idl_start, x_i[0],
295*30b5892fSJames Wright                                                context->idl_pressure, v_i, &sigma);
296*30b5892fSJames Wright 
297*30b5892fSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * v_i[j];
2982b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
2993d65b166SJames Wright       for (CeedInt k = 0; k < 3; k++) {
300*30b5892fSJames Wright         Grad_v[k][j][i] = wdetJ * (dv_i[j][0] * dXdx[k][0] + dv_i[j][1] * dXdx[k][1] + dv_i[j][2] * dXdx[k][2]);
3013d65b166SJames Wright       }
3022b916ea7SJeremy L Thompson     }
303*30b5892fSJames Wright 
304ade49511SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
305ade49511SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
306ade49511SJames Wright     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
307*30b5892fSJames Wright     if (context->idl_enable) StoredValuesPack(Q, i, 14, 1, &sigma, jac_data);
308b193fadcSJames Wright   }
3093a8779fbSJames Wright   return 0;
3103a8779fbSJames Wright }
311f0b65372SJed Brown 
3122b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3138fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
31476555becSJames Wright }
31576555becSJames Wright 
3162b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3178fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
31876555becSJames Wright }
31976555becSJames Wright 
3209b103f75SJames Wright CEED_QFUNCTION(IFunction_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3219b103f75SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
3229b103f75SJames Wright }
3239b103f75SJames Wright 
324cbe60e31SLeila Ghaffari // *****************************************************************************
32504e40bb6SJeremy L Thompson // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
326cbe60e31SLeila Ghaffari // *****************************************************************************
3278fff8293SJames Wright CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3283d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[0];
32987bd45e7SJames Wright   const CeedScalar(*Grad_dq)         = in[1];
330ade49511SJames Wright   const CeedScalar(*q_data)          = in[2];
33194a7b3d2SKenneth E. Jansen   const CeedScalar(*jac_data)        = in[3];
3323d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
3333d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
3343d65b166SJames Wright 
335f0b65372SJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
336f0b65372SJed Brown   const CeedScalar        *g       = context->g;
337f0b65372SJed Brown 
3383d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
339ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
340ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
341f0b65372SJed Brown 
3428789e95fSJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
343ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
344ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
345ade49511SJames Wright     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
346edcfef1bSKenneth E. Jansen     State s = StateFromQ(context, qi, state_var);
347f0b65372SJed Brown 
348edcfef1bSKenneth E. Jansen     CeedScalar dqi[5];
34976555becSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
350edcfef1bSKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
351f0b65372SJed Brown 
352f0b65372SJed Brown     State grad_ds[3];
353edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
354f0b65372SJed Brown 
355f0b65372SJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
35640a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
357f0b65372SJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
358f0b65372SJed Brown     KMUnpack(dkmstress, dstress);
359f0b65372SJed Brown     KMUnpack(kmstress, stress);
360f0b65372SJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
361f0b65372SJed Brown 
362f0b65372SJed Brown     StateConservative dF_inviscid[3];
363f0b65372SJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
364f0b65372SJed Brown 
365f0b65372SJed Brown     // Total flux
366f0b65372SJed Brown     CeedScalar dFlux[5][3];
367d1b9ef12SLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
368f0b65372SJed Brown 
36922387d3aSJames Wright     for (int j = 0; j < 5; j++) {
37022387d3aSJames Wright       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]);
3712b916ea7SJeremy L Thompson     }
372f0b65372SJed Brown 
37360dbb574SKenneth E. Jansen     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)};
37476555becSJames Wright     CeedScalar       dU[5]          = {0.};
37576555becSJames Wright     UnpackState_U(ds.U, dU);
3762b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
377f0b65372SJed Brown 
378e7754af5SKenneth E. Jansen     if (context->idl_enable) {
37994a7b3d2SKenneth E. Jansen       const CeedScalar sigma         = jac_data[14 * Q + i];
380e7754af5SKenneth E. Jansen       CeedScalar       damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
381e7754af5SKenneth E. Jansen       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
382*30b5892fSJames Wright       InternalDampingLayer_Residual(context, s, sigma, damp_state, idl_residual);
383e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
384e7754af5SKenneth E. Jansen     }
385e7754af5SKenneth E. Jansen 
386d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
387d1b9ef12SLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
388d1b9ef12SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
3898c85b835SJames Wright     const CeedScalar zeroFlux[5] = {0.};
3908c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, zeroFlux, dstab);
391d1b9ef12SLeila Ghaffari 
3922b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) {
3932b916ea7SJeremy L Thompson       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]);
3942b916ea7SJeremy L Thompson     }
395b193fadcSJames Wright   }
396f0b65372SJed Brown   return 0;
397f0b65372SJed Brown }
3988085925cSJames Wright 
3992b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4008fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
40176555becSJames Wright }
40276555becSJames Wright 
4032b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4048fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
40576555becSJames Wright }
40676555becSJames Wright 
4079b103f75SJames Wright CEED_QFUNCTION(IJacobian_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4089b103f75SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
4099b103f75SJames Wright }
4109b103f75SJames Wright 
411d1b9ef12SLeila Ghaffari // *****************************************************************************
4128085925cSJames Wright // Compute boundary integral (ie. for strongly set inflows)
413d1b9ef12SLeila Ghaffari // *****************************************************************************
4148fff8293SJames Wright CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
4154b96a86bSJames Wright   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
4163d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]       = (const CeedScalar(*)[CEED_Q_VLA])in[0];
41787bd45e7SJames Wright   const CeedScalar(*Grad_q)              = in[1];
418ade49511SJames Wright   const CeedScalar(*q_data_sur)          = in[2];
4193d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]             = (CeedScalar(*)[CEED_Q_VLA])out[0];
4204b96a86bSJames Wright   CeedScalar(*jac_data_sur)              = context->is_implicit ? out[1] : NULL;
4218085925cSJames Wright 
422d3b25f3aSJames Wright   const bool is_implicit = context->is_implicit;
4238085925cSJames Wright 
4242b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
42541e73928SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
426edcfef1bSKenneth E. Jansen     State            s     = StateFromQ(context, qi, state_var);
4278085925cSJames Wright 
42878e8b7daSJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
42978e8b7daSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
430ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
4318085925cSJames Wright 
432d3b25f3aSJames Wright     State grad_s[3];
433edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
4348085925cSJames Wright 
435d3b25f3aSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
43640a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
437d3b25f3aSJames Wright     NewtonianStress(context, strain_rate, kmstress);
438d3b25f3aSJames Wright     KMUnpack(kmstress, stress);
439d3b25f3aSJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
440d3b25f3aSJames Wright 
441d3b25f3aSJames Wright     StateConservative F_inviscid[3];
442d3b25f3aSJames Wright     FluxInviscid(context, s, F_inviscid);
443d3b25f3aSJames Wright 
444c5740391SJames Wright     CeedScalar Flux[5];
44578e8b7daSJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, normal, Flux);
446d3b25f3aSJames Wright 
447c5740391SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
4488085925cSJames Wright 
4494b96a86bSJames Wright     if (is_implicit) {
450ade49511SJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
451ade49511SJames Wright       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
4528085925cSJames Wright     }
4534b96a86bSJames Wright   }
4548085925cSJames Wright   return 0;
4558085925cSJames Wright }
4568085925cSJames Wright 
4572b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4588fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
459d4559bbeSJames Wright }
460d4559bbeSJames Wright 
4612b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4628fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
463d4559bbeSJames Wright }
464d4559bbeSJames Wright 
4659b103f75SJames Wright CEED_QFUNCTION(BoundaryIntegral_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4669b103f75SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_ENTROPY);
4679b103f75SJames Wright }
4689b103f75SJames Wright 
469d1b9ef12SLeila Ghaffari // *****************************************************************************
47068ae065aSJames Wright // Jacobian for "set nothing" boundary integral
471d1b9ef12SLeila Ghaffari // *****************************************************************************
4722b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
4738fff8293SJames Wright                                                     StateVariable state_var) {
4743d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
47587bd45e7SJames Wright   const CeedScalar(*Grad_dq)        = in[1];
476ade49511SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
477c1484fadSKenneth E. Jansen   const CeedScalar(*jac_data_sur)   = in[4];
47868ae065aSJames Wright   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
47968ae065aSJames Wright 
48068ae065aSJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
481ade49511SJames Wright   const bool                     is_implicit = context->is_implicit;
48268ae065aSJames Wright 
4833d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
48478e8b7daSJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
48578e8b7daSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
486ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
48768ae065aSJames Wright 
488edcfef1bSKenneth E. Jansen     CeedScalar qi[5], kmstress[6], dqi[5];
489ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
490ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
49141e73928SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
4923934e2b1SJames Wright 
493edcfef1bSKenneth E. Jansen     State s  = StateFromQ(context, qi, state_var);
494edcfef1bSKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
49568ae065aSJames Wright 
49668ae065aSJames Wright     State grad_ds[3];
497edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
49868ae065aSJames Wright 
49968ae065aSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
50040a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
50168ae065aSJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
50268ae065aSJames Wright     KMUnpack(dkmstress, dstress);
50368ae065aSJames Wright     KMUnpack(kmstress, stress);
50468ae065aSJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
50568ae065aSJames Wright 
50668ae065aSJames Wright     StateConservative dF_inviscid[3];
50768ae065aSJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
50868ae065aSJames Wright 
509c5740391SJames Wright     CeedScalar dFlux[5];
51078e8b7daSJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, normal, dFlux);
51168ae065aSJames Wright 
512c5740391SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
513512c8ec7SJames Wright   }
51468ae065aSJames Wright   return 0;
51568ae065aSJames Wright }
51668ae065aSJames Wright 
5172b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5188fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
519d4559bbeSJames Wright }
520d4559bbeSJames Wright 
5212b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5228fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
523d4559bbeSJames Wright }
5249b103f75SJames Wright 
5259b103f75SJames Wright CEED_QFUNCTION(BoundaryIntegral_Jacobian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5269b103f75SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_ENTROPY);
5279b103f75SJames Wright }
52836038bbcSJames Wright 
5298561fee2SJames Wright // @brief Volume integral for RHS of divergence of diffusive flux direct projection
53036038bbcSJames Wright CEED_QFUNCTION_HELPER int DivDiffusiveFluxVolumeRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
53136038bbcSJames Wright                                                        StateVariable state_var) {
53236038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[0];
53336038bbcSJames Wright   const CeedScalar(*Grad_q)          = in[1];
53436038bbcSJames Wright   const CeedScalar(*q_data)          = in[2];
53536038bbcSJames Wright   CeedScalar(*Grad_v)[4][CEED_Q_VLA] = (CeedScalar(*)[4][CEED_Q_VLA])out[0];
53636038bbcSJames Wright 
53736038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
53836038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
53936038bbcSJames Wright 
54036038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
54136038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
54236038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
54336038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3];
54436038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
54536038bbcSJames Wright 
54636038bbcSJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
54736038bbcSJames Wright     {  // Get stress and Fe
54836038bbcSJames Wright       State      grad_s[3];
54936038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
55036038bbcSJames Wright 
55136038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
55236038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
55336038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
55436038bbcSJames Wright       KMUnpack(kmstress, stress);
55536038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
55636038bbcSJames Wright     }
55736038bbcSJames Wright 
55836038bbcSJames Wright     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
55936038bbcSJames Wright 
56036038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
56136038bbcSJames Wright       for (CeedInt k = 0; k < 3; k++) {
56236038bbcSJames Wright         Grad_v[k][j - 1][i] = -wdetJ * Dot3(dXdx[k], Fdiff[j]);
56336038bbcSJames Wright       }
56436038bbcSJames Wright     }
56536038bbcSJames Wright   }
56636038bbcSJames Wright   return 0;
56736038bbcSJames Wright }
56836038bbcSJames Wright 
56936038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
57036038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
57136038bbcSJames Wright }
57236038bbcSJames Wright 
57336038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
57436038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
57536038bbcSJames Wright }
57636038bbcSJames Wright 
57736038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
57836038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
57936038bbcSJames Wright }
58036038bbcSJames Wright 
5818561fee2SJames Wright // @brief Boundary integral for RHS of divergence of diffusive flux direct projection
58236038bbcSJames Wright CEED_QFUNCTION_HELPER int DivDiffusiveFluxBoundaryRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
58336038bbcSJames Wright                                                          StateVariable state_var) {
58436038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
58536038bbcSJames Wright   const CeedScalar(*Grad_q)        = in[1];
58636038bbcSJames Wright   const CeedScalar(*q_data)        = in[2];
58736038bbcSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
58836038bbcSJames Wright 
58936038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
59036038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
59136038bbcSJames Wright 
59236038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
59336038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
59436038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
59536038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3], normal[3];
59636038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5];
59736038bbcSJames Wright 
59836038bbcSJames Wright     QdataBoundaryGradientUnpack_3D(Q, i, q_data, &wdetJ, dXdx, normal);
59936038bbcSJames Wright     {  // Get stress and Fe
60036038bbcSJames Wright       State      grad_s[3];
60136038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
60236038bbcSJames Wright 
60336038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
60436038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
60536038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
60636038bbcSJames Wright       KMUnpack(kmstress, stress);
60736038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
60836038bbcSJames Wright     }
60936038bbcSJames Wright 
61036038bbcSJames Wright     FluxTotal_Boundary(ZeroInviscidFluxes, stress, Fe, normal, Fdiff);
61136038bbcSJames Wright 
61236038bbcSJames Wright     // Continuity has no diffusive flux, therefore skip
61336038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) v[j - 1][i] = wdetJ * Fdiff[j];
61436038bbcSJames Wright   }
61536038bbcSJames Wright   return 0;
61636038bbcSJames Wright }
61736038bbcSJames Wright 
61836038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
61936038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
62036038bbcSJames Wright }
62136038bbcSJames Wright 
62236038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
62336038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
62436038bbcSJames Wright }
62536038bbcSJames Wright 
62636038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
62736038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
62836038bbcSJames Wright }
62936038bbcSJames Wright 
6308561fee2SJames Wright // @brief Integral for RHS of diffusive flux indirect projection
63136038bbcSJames Wright CEED_QFUNCTION_HELPER int DiffusiveFluxRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
63236038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
63336038bbcSJames Wright   const CeedScalar(*Grad_q)        = in[1];
63436038bbcSJames Wright   const CeedScalar(*q_data)        = in[2];
63536038bbcSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
63636038bbcSJames Wright 
63736038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
63836038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
63936038bbcSJames Wright 
64036038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
64136038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
64236038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
64336038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3];
64436038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
64536038bbcSJames Wright 
64636038bbcSJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
64736038bbcSJames Wright     {  // Get stress and Fe
64836038bbcSJames Wright       State      grad_s[3];
64936038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
65036038bbcSJames Wright 
65136038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
65236038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
65336038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
65436038bbcSJames Wright       KMUnpack(kmstress, stress);
65536038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
65636038bbcSJames Wright     }
65736038bbcSJames Wright 
65836038bbcSJames Wright     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
65936038bbcSJames Wright 
66036038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
66136038bbcSJames Wright       for (CeedInt k = 0; k < 3; k++) {
66236038bbcSJames Wright         v[(j - 1) * 3 + k][i] = wdetJ * Fdiff[j][k];
66336038bbcSJames Wright       }
66436038bbcSJames Wright     }
66536038bbcSJames Wright   }
66636038bbcSJames Wright   return 0;
66736038bbcSJames Wright }
66836038bbcSJames Wright 
66936038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
67036038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
67136038bbcSJames Wright }
67236038bbcSJames Wright 
67336038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
67436038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
67536038bbcSJames Wright }
67636038bbcSJames Wright 
67736038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
67836038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
67936038bbcSJames Wright }
680