xref: /honee/qfunctions/newtonian.h (revision ea615d4cc464aa6ad650c06fae6d120cc2465bc4)
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
5*ea615d4cSJames 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 
1394a7b3d2SKenneth E. Jansen CEED_QFUNCTION_HELPER void InternalDampingLayer(const NewtonianIdealGasContext context, const State s, const CeedScalar sigma, CeedScalar damp_Y[5],
14e7754af5SKenneth E. Jansen                                                 CeedScalar damp_residual[5]) {
15e7754af5SKenneth E. Jansen   ScaleN(damp_Y, sigma, 5);
16edcfef1bSKenneth E. Jansen   State damp_s = StateFromY_fwd(context, s, damp_Y);
17e7754af5SKenneth E. Jansen 
18e7754af5SKenneth E. Jansen   CeedScalar U[5];
19e7754af5SKenneth E. Jansen   UnpackState_U(damp_s.U, U);
20e7754af5SKenneth E. Jansen   for (int i = 0; i < 5; i++) damp_residual[i] += U[i];
21e7754af5SKenneth E. Jansen }
22e7754af5SKenneth E. Jansen 
23bb8a0c61SJames Wright // *****************************************************************************
243a8779fbSJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
253a8779fbSJames Wright // *****************************************************************************
268fff8293SJames Wright CEED_QFUNCTION_HELPER int ICsNewtonianIG(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
273a8779fbSJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
283a8779fbSJames Wright 
29bb8a0c61SJames Wright   const SetupContext context = (SetupContext)ctx;
30bb8a0c61SJames Wright 
312b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
32a541e550SJames Wright     CeedScalar q[5];
33edcfef1bSKenneth E. Jansen     State      s = StateFromPrimitive(&context->gas, context->reference);
348fff8293SJames Wright     StateToQ(&context->gas, s, q, state_var);
352b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
36b193fadcSJames Wright   }
373a8779fbSJames Wright   return 0;
383a8779fbSJames Wright }
393a8779fbSJames Wright 
409b103f75SJames Wright CEED_QFUNCTION(ICsNewtonianIG_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
419b103f75SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
429b103f75SJames Wright }
439b103f75SJames Wright 
442b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsNewtonianIG_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
458fff8293SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_PRIMITIVE);
46b8fb7609SAdeleke O. Bankole }
479b103f75SJames Wright 
489b103f75SJames Wright CEED_QFUNCTION(ICsNewtonianIG_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
499b103f75SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_ENTROPY);
50cbe60e31SLeila Ghaffari }
51cbe60e31SLeila Ghaffari 
5297cfd714SJames Wright CEED_QFUNCTION_HELPER int MassFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
5365dee3d2SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
5465dee3d2SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[1];
5565dee3d2SJames Wright   const CeedScalar(*q_data)            = in[2];
5665dee3d2SJames Wright   CeedScalar(*v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
5765dee3d2SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]   = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
5865dee3d2SJames Wright 
5965dee3d2SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
6065dee3d2SJames Wright 
6165dee3d2SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
6265dee3d2SJames Wright     const CeedScalar qi[5]     = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
6365dee3d2SJames 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]};
6465dee3d2SJames Wright     const State      s         = StateFromQ(context, qi, state_var);
6565dee3d2SJames Wright     const State      s_dot     = StateFromQ(context, qi_dot, state_var);
6665dee3d2SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
6765dee3d2SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
6865dee3d2SJames Wright 
6965dee3d2SJames Wright     // Standard mass matrix term
7065dee3d2SJames Wright     for (CeedInt f = 0; f < 5; f++) {
7165dee3d2SJames Wright       v[f][i] = wdetJ * qi_dot[f];
7265dee3d2SJames Wright     }
7365dee3d2SJames Wright 
7465dee3d2SJames Wright     // Stabilization method: none (Galerkin), SU, or SUPG
7565dee3d2SJames Wright     State      grad_s[3] = {{{0.}}};
768c85b835SJames Wright     CeedScalar Tau_d[3], stab[5][3], body_force[5] = {0.}, divFdiff[5] = {0.}, U_dot[5];
7765dee3d2SJames Wright     UnpackState_U(s_dot.U, U_dot);
7865dee3d2SJames Wright     Tau_diagPrim(context, s, dXdx, context->dt, Tau_d);
798c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff, stab);
8065dee3d2SJames Wright 
8165dee3d2SJames Wright     // Stabilized mass term
8265dee3d2SJames Wright     for (CeedInt j = 0; j < 5; j++) {
8365dee3d2SJames Wright       for (CeedInt k = 0; k < 3; k++) {
8465dee3d2SJames 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]);
8565dee3d2SJames Wright       }
8665dee3d2SJames Wright     }
8765dee3d2SJames Wright   }
8897cfd714SJames Wright   return 0;
8965dee3d2SJames Wright }
9065dee3d2SJames Wright 
9165dee3d2SJames Wright CEED_QFUNCTION(MassFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
9297cfd714SJames Wright   return MassFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
9365dee3d2SJames Wright }
9465dee3d2SJames Wright 
95cbe60e31SLeila Ghaffari // *****************************************************************************
9604e40bb6SJeremy L Thompson // This QFunction implements the following formulation of Navier-Stokes with explicit time stepping method
973a8779fbSJames Wright //
9804e40bb6SJeremy L Thompson // This is 3D compressible Navier-Stokes in conservation form with state variables of density, momentum density, and total energy density.
993a8779fbSJames Wright //
1003a8779fbSJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
1013a8779fbSJames Wright //   rho - Mass Density
1023a8779fbSJames Wright //   Ui  - Momentum Density,      Ui = rho ui
1033a8779fbSJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
1043a8779fbSJames Wright //
1053a8779fbSJames Wright // Navier-Stokes Equations:
1063a8779fbSJames Wright //   drho/dt + div( U )                               = 0
1073a8779fbSJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
1083a8779fbSJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
1093a8779fbSJames Wright //
1103a8779fbSJames Wright // Viscous Stress:
1113a8779fbSJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
1123a8779fbSJames Wright //
1133a8779fbSJames Wright // Thermal Stress:
1143a8779fbSJames Wright //   Fe = u Fu + k grad( T )
115bb8a0c61SJames Wright // Equation of State
1163a8779fbSJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
1173a8779fbSJames Wright //
1183a8779fbSJames Wright // Stabilization:
1193a8779fbSJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
1203a8779fbSJames Wright //     f1 = rho  sqrt(ui uj gij)
1213a8779fbSJames Wright //     gij = dXi/dX * dXi/dX
1223a8779fbSJames Wright //     TauC = Cc f1 / (8 gii)
1233a8779fbSJames Wright //     TauM = min( 1 , 1 / f1 )
1243a8779fbSJames Wright //     TauE = TauM / (Ce cv)
1253a8779fbSJames Wright //
1263a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
1273a8779fbSJames Wright //
1283a8779fbSJames Wright // Constants:
1293a8779fbSJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
1303a8779fbSJames Wright //   mu              ,  Dynamic viscosity
1313a8779fbSJames Wright //   k               ,  Thermal conductivity
1323a8779fbSJames Wright //   cv              ,  Specific heat, constant volume
1333a8779fbSJames Wright //   cp              ,  Specific heat, constant pressure
1343a8779fbSJames Wright //   g               ,  Gravity
1353a8779fbSJames Wright //   gamma  = cp / cv,  Specific heat ratio
1363a8779fbSJames Wright //
13704e40bb6SJeremy 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
13804e40bb6SJeremy L Thompson // gradu )
1393a8779fbSJames Wright // *****************************************************************************
1402b916ea7SJeremy L Thompson CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1413d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[0];
14287bd45e7SJames Wright   const CeedScalar(*Grad_q)          = in[1];
143ade49511SJames Wright   const CeedScalar(*q_data)          = in[2];
1440a32a5aaSJames Wright   const CeedScalar(*x)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[3];
1453d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
1463d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
1473a8779fbSJames Wright 
1483a8779fbSJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
149bb8a0c61SJames Wright   const CeedScalar        *g       = context->g;
150bb8a0c61SJames Wright   const CeedScalar         dt      = context->dt;
1510a32a5aaSJames Wright   const CeedScalar         P0      = context->idl_pressure;
1523a8779fbSJames Wright 
1533d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
154ade49511SJames Wright     CeedScalar       U[5], wdetJ, dXdx[3][3];
1550a32a5aaSJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
156c1a52365SJed Brown     for (int j = 0; j < 5; j++) U[j] = q[j][i];
1571be49596SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
158edcfef1bSKenneth E. Jansen     State s = StateFromU(context, U);
159c1a52365SJed Brown 
160c1a52365SJed Brown     State grad_s[3];
161edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, STATEVAR_CONSERVATIVE, Grad_q, dXdx, grad_s);
162c1a52365SJed Brown 
163c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
16440a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
165c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
166c1a52365SJed Brown     KMUnpack(kmstress, stress);
167c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
168c1a52365SJed Brown 
169c1a52365SJed Brown     StateConservative F_inviscid[3];
170c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
171c1a52365SJed Brown 
172c1a52365SJed Brown     // Total flux
173c1a52365SJed Brown     CeedScalar Flux[5][3];
174d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
175c1a52365SJed Brown 
1767523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
1777523f6aaSJames 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]);
1782b916ea7SJeremy L Thompson     }
179c1a52365SJed Brown 
18060dbb574SKenneth 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)};
1812b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
1823a8779fbSJames Wright 
1830a32a5aaSJames Wright     if (context->idl_enable) {
1840a32a5aaSJames Wright       const CeedScalar sigma         = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
1850a32a5aaSJames Wright       CeedScalar       damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
1860a32a5aaSJames Wright       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
1870a32a5aaSJames Wright       for (int j = 0; j < 5; j++) v[j][i] -= wdetJ * idl_residual[j];
1880a32a5aaSJames Wright     }
1890a32a5aaSJames Wright 
190d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
1918c85b835SJames Wright     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, zeroFlux[5] = {0.};
192d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
1938c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, zeroFlux, stab);
1943a8779fbSJames Wright 
1952b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
1962b916ea7SJeremy 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]);
1972b916ea7SJeremy L Thompson     }
198b193fadcSJames Wright   }
1993a8779fbSJames Wright   return 0;
2003a8779fbSJames Wright }
2013a8779fbSJames Wright 
2023a8779fbSJames Wright // *****************************************************************************
20304e40bb6SJeremy L Thompson // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
2043a8779fbSJames Wright //
2053a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
2063a8779fbSJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
20704e40bb6SJeremy L Thompson //                                       (diffusive terms will be added later)
2083a8779fbSJames Wright // *****************************************************************************
2098fff8293SJames Wright CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
2108c85b835SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
2118c85b835SJames Wright 
2123d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[0];
21387bd45e7SJames Wright   const CeedScalar(*Grad_q)               = in[1];
2143d65b166SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])in[2];
215ade49511SJames Wright   const CeedScalar(*q_data)               = in[3];
2163d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[4];
2178c85b835SJames Wright   const CeedScalar(*divFdiff)[CEED_Q_VLA] = context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE ? (const CeedScalar(*)[CEED_Q_VLA])in[5] : NULL;
2183d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]              = (CeedScalar(*)[CEED_Q_VLA])out[0];
2193d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]      = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
220ade49511SJames Wright   CeedScalar(*jac_data)                   = out[2];
2213d65b166SJames Wright 
222bb8a0c61SJames Wright   const CeedScalar *g  = context->g;
223bb8a0c61SJames Wright   const CeedScalar  dt = context->dt;
224fcb2c22aSJames Wright   const CeedScalar  P0 = context->idl_pressure;
2253a8779fbSJames Wright 
2263d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2273d65b166SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
228c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
229edcfef1bSKenneth E. Jansen     const State      s      = StateFromQ(context, qi, state_var);
230c1a52365SJed Brown 
231ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
232ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
233c1a52365SJed Brown     State grad_s[3];
234edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
235c1a52365SJed Brown 
236c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
23740a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
238c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
239c1a52365SJed Brown     KMUnpack(kmstress, stress);
240c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
241c1a52365SJed Brown 
242c1a52365SJed Brown     StateConservative F_inviscid[3];
243c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
244c1a52365SJed Brown 
245c1a52365SJed Brown     // Total flux
246c1a52365SJed Brown     CeedScalar Flux[5][3];
247d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
248c1a52365SJed Brown 
2497523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
2507523f6aaSJames Wright       for (CeedInt k = 0; k < 3; k++) {
2517523f6aaSJames Wright         Grad_v[k][j][i] = -wdetJ * (dXdx[k][0] * Flux[j][0] + dXdx[k][1] * Flux[j][1] + dXdx[k][2] * Flux[j][2]);
2523d65b166SJames Wright       }
2532b916ea7SJeremy L Thompson     }
254c1a52365SJed Brown 
25560dbb574SKenneth 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)};
2563a8779fbSJames Wright 
257d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
258edcfef1bSKenneth E. Jansen     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5];
25976555becSJames Wright     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
260edcfef1bSKenneth E. Jansen     State s_dot = StateFromQ_fwd(context, s, qi_dot, state_var);
26176555becSJames Wright     UnpackState_U(s_dot.U, U_dot);
26276555becSJames Wright 
2632b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
264e7754af5SKenneth E. Jansen     if (context->idl_enable) {
26594a7b3d2SKenneth E. Jansen       const CeedScalar sigma = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
26694a7b3d2SKenneth E. Jansen       StoredValuesPack(Q, i, 14, 1, &sigma, jac_data);
267e7754af5SKenneth E. Jansen       CeedScalar damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
26894a7b3d2SKenneth E. Jansen       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
269e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
270e7754af5SKenneth E. Jansen     }
271e7754af5SKenneth E. Jansen 
2728c85b835SJames Wright     CeedScalar divFdiff_i[5] = {0.};
2738c85b835SJames Wright     if (context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE) {
2748c85b835SJames Wright       for (int j = 1; j < 5; j++) divFdiff_i[j] = divFdiff[j - 1][i];
2758c85b835SJames Wright     }
276d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
2778c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff_i, stab);
2783a8779fbSJames Wright 
2792b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
2803d65b166SJames Wright       for (CeedInt k = 0; k < 3; k++) {
2813d65b166SJames 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]);
2823d65b166SJames Wright       }
2832b916ea7SJeremy L Thompson     }
284ade49511SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
285ade49511SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
286ade49511SJames Wright     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
287b193fadcSJames Wright   }
2883a8779fbSJames Wright   return 0;
2893a8779fbSJames Wright }
290f0b65372SJed Brown 
2912b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2928fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
29376555becSJames Wright }
29476555becSJames Wright 
2952b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2968fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
29776555becSJames Wright }
29876555becSJames Wright 
2999b103f75SJames Wright CEED_QFUNCTION(IFunction_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3009b103f75SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
3019b103f75SJames Wright }
3029b103f75SJames Wright 
303cbe60e31SLeila Ghaffari // *****************************************************************************
30404e40bb6SJeremy L Thompson // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
305cbe60e31SLeila Ghaffari // *****************************************************************************
3068fff8293SJames Wright CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3073d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[0];
30887bd45e7SJames Wright   const CeedScalar(*Grad_dq)         = in[1];
309ade49511SJames Wright   const CeedScalar(*q_data)          = in[2];
31094a7b3d2SKenneth E. Jansen   const CeedScalar(*jac_data)        = in[3];
3113d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
3123d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
3133d65b166SJames Wright 
314f0b65372SJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
315f0b65372SJed Brown   const CeedScalar        *g       = context->g;
316f0b65372SJed Brown 
3173d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
318ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
319ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
320f0b65372SJed Brown 
3218789e95fSJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
322ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
323ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
324ade49511SJames Wright     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
325edcfef1bSKenneth E. Jansen     State s = StateFromQ(context, qi, state_var);
326f0b65372SJed Brown 
327edcfef1bSKenneth E. Jansen     CeedScalar dqi[5];
32876555becSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
329edcfef1bSKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
330f0b65372SJed Brown 
331f0b65372SJed Brown     State grad_ds[3];
332edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
333f0b65372SJed Brown 
334f0b65372SJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
33540a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
336f0b65372SJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
337f0b65372SJed Brown     KMUnpack(dkmstress, dstress);
338f0b65372SJed Brown     KMUnpack(kmstress, stress);
339f0b65372SJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
340f0b65372SJed Brown 
341f0b65372SJed Brown     StateConservative dF_inviscid[3];
342f0b65372SJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
343f0b65372SJed Brown 
344f0b65372SJed Brown     // Total flux
345f0b65372SJed Brown     CeedScalar dFlux[5][3];
346d1b9ef12SLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
347f0b65372SJed Brown 
34822387d3aSJames Wright     for (int j = 0; j < 5; j++) {
34922387d3aSJames 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]);
3502b916ea7SJeremy L Thompson     }
351f0b65372SJed Brown 
35260dbb574SKenneth 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)};
35376555becSJames Wright     CeedScalar       dU[5]          = {0.};
35476555becSJames Wright     UnpackState_U(ds.U, dU);
3552b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
356f0b65372SJed Brown 
357e7754af5SKenneth E. Jansen     if (context->idl_enable) {
35894a7b3d2SKenneth E. Jansen       const CeedScalar sigma         = jac_data[14 * Q + i];
359e7754af5SKenneth E. Jansen       CeedScalar       damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
360e7754af5SKenneth E. Jansen       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
36194a7b3d2SKenneth E. Jansen       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
362e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
363e7754af5SKenneth E. Jansen     }
364e7754af5SKenneth E. Jansen 
365d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
366d1b9ef12SLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
367d1b9ef12SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
3688c85b835SJames Wright     const CeedScalar zeroFlux[5] = {0.};
3698c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, zeroFlux, dstab);
370d1b9ef12SLeila Ghaffari 
3712b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) {
3722b916ea7SJeremy 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]);
3732b916ea7SJeremy L Thompson     }
374b193fadcSJames Wright   }
375f0b65372SJed Brown   return 0;
376f0b65372SJed Brown }
3778085925cSJames Wright 
3782b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3798fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
38076555becSJames Wright }
38176555becSJames Wright 
3822b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3838fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
38476555becSJames Wright }
38576555becSJames Wright 
3869b103f75SJames Wright CEED_QFUNCTION(IJacobian_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3879b103f75SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
3889b103f75SJames Wright }
3899b103f75SJames Wright 
390d1b9ef12SLeila Ghaffari // *****************************************************************************
3918085925cSJames Wright // Compute boundary integral (ie. for strongly set inflows)
392d1b9ef12SLeila Ghaffari // *****************************************************************************
3938fff8293SJames Wright CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3944b96a86bSJames Wright   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
3953d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]       = (const CeedScalar(*)[CEED_Q_VLA])in[0];
39687bd45e7SJames Wright   const CeedScalar(*Grad_q)              = in[1];
397ade49511SJames Wright   const CeedScalar(*q_data_sur)          = in[2];
3983d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]             = (CeedScalar(*)[CEED_Q_VLA])out[0];
3994b96a86bSJames Wright   CeedScalar(*jac_data_sur)              = context->is_implicit ? out[1] : NULL;
4008085925cSJames Wright 
401d3b25f3aSJames Wright   const bool is_implicit = context->is_implicit;
4028085925cSJames Wright 
4032b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
40441e73928SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
405edcfef1bSKenneth E. Jansen     State            s     = StateFromQ(context, qi, state_var);
4068085925cSJames Wright 
40778e8b7daSJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
40878e8b7daSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
409ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
4108085925cSJames Wright 
411d3b25f3aSJames Wright     State grad_s[3];
412edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
4138085925cSJames Wright 
414d3b25f3aSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
41540a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
416d3b25f3aSJames Wright     NewtonianStress(context, strain_rate, kmstress);
417d3b25f3aSJames Wright     KMUnpack(kmstress, stress);
418d3b25f3aSJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
419d3b25f3aSJames Wright 
420d3b25f3aSJames Wright     StateConservative F_inviscid[3];
421d3b25f3aSJames Wright     FluxInviscid(context, s, F_inviscid);
422d3b25f3aSJames Wright 
423c5740391SJames Wright     CeedScalar Flux[5];
42478e8b7daSJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, normal, Flux);
425d3b25f3aSJames Wright 
426c5740391SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
4278085925cSJames Wright 
4284b96a86bSJames Wright     if (is_implicit) {
429ade49511SJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
430ade49511SJames Wright       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
4318085925cSJames Wright     }
4324b96a86bSJames Wright   }
4338085925cSJames Wright   return 0;
4348085925cSJames Wright }
4358085925cSJames Wright 
4362b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4378fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
438d4559bbeSJames Wright }
439d4559bbeSJames Wright 
4402b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4418fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
442d4559bbeSJames Wright }
443d4559bbeSJames Wright 
4449b103f75SJames Wright CEED_QFUNCTION(BoundaryIntegral_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4459b103f75SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_ENTROPY);
4469b103f75SJames Wright }
4479b103f75SJames Wright 
448d1b9ef12SLeila Ghaffari // *****************************************************************************
44968ae065aSJames Wright // Jacobian for "set nothing" boundary integral
450d1b9ef12SLeila Ghaffari // *****************************************************************************
4512b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
4528fff8293SJames Wright                                                     StateVariable state_var) {
4533d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
45487bd45e7SJames Wright   const CeedScalar(*Grad_dq)        = in[1];
455ade49511SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
456c1484fadSKenneth E. Jansen   const CeedScalar(*jac_data_sur)   = in[4];
45768ae065aSJames Wright   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
45868ae065aSJames Wright 
45968ae065aSJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
460ade49511SJames Wright   const bool                     is_implicit = context->is_implicit;
46168ae065aSJames Wright 
4623d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
46378e8b7daSJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
46478e8b7daSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
465ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
46668ae065aSJames Wright 
467edcfef1bSKenneth E. Jansen     CeedScalar qi[5], kmstress[6], dqi[5];
468ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
469ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
47041e73928SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
4713934e2b1SJames Wright 
472edcfef1bSKenneth E. Jansen     State s  = StateFromQ(context, qi, state_var);
473edcfef1bSKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
47468ae065aSJames Wright 
47568ae065aSJames Wright     State grad_ds[3];
476edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
47768ae065aSJames Wright 
47868ae065aSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
47940a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
48068ae065aSJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
48168ae065aSJames Wright     KMUnpack(dkmstress, dstress);
48268ae065aSJames Wright     KMUnpack(kmstress, stress);
48368ae065aSJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
48468ae065aSJames Wright 
48568ae065aSJames Wright     StateConservative dF_inviscid[3];
48668ae065aSJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
48768ae065aSJames Wright 
488c5740391SJames Wright     CeedScalar dFlux[5];
48978e8b7daSJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, normal, dFlux);
49068ae065aSJames Wright 
491c5740391SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
492512c8ec7SJames Wright   }
49368ae065aSJames Wright   return 0;
49468ae065aSJames Wright }
49568ae065aSJames Wright 
4962b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4978fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
498d4559bbeSJames Wright }
499d4559bbeSJames Wright 
5002b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5018fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
502d4559bbeSJames Wright }
5039b103f75SJames Wright 
5049b103f75SJames Wright CEED_QFUNCTION(BoundaryIntegral_Jacobian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5059b103f75SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_ENTROPY);
5069b103f75SJames Wright }
50736038bbcSJames Wright 
5088561fee2SJames Wright // @brief Volume integral for RHS of divergence of diffusive flux direct projection
50936038bbcSJames Wright CEED_QFUNCTION_HELPER int DivDiffusiveFluxVolumeRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
51036038bbcSJames Wright                                                        StateVariable state_var) {
51136038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[0];
51236038bbcSJames Wright   const CeedScalar(*Grad_q)          = in[1];
51336038bbcSJames Wright   const CeedScalar(*q_data)          = in[2];
51436038bbcSJames Wright   CeedScalar(*Grad_v)[4][CEED_Q_VLA] = (CeedScalar(*)[4][CEED_Q_VLA])out[0];
51536038bbcSJames Wright 
51636038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
51736038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
51836038bbcSJames Wright 
51936038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
52036038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
52136038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
52236038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3];
52336038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
52436038bbcSJames Wright 
52536038bbcSJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
52636038bbcSJames Wright     {  // Get stress and Fe
52736038bbcSJames Wright       State      grad_s[3];
52836038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
52936038bbcSJames Wright 
53036038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
53136038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
53236038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
53336038bbcSJames Wright       KMUnpack(kmstress, stress);
53436038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
53536038bbcSJames Wright     }
53636038bbcSJames Wright 
53736038bbcSJames Wright     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
53836038bbcSJames Wright 
53936038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
54036038bbcSJames Wright       for (CeedInt k = 0; k < 3; k++) {
54136038bbcSJames Wright         Grad_v[k][j - 1][i] = -wdetJ * Dot3(dXdx[k], Fdiff[j]);
54236038bbcSJames Wright       }
54336038bbcSJames Wright     }
54436038bbcSJames Wright   }
54536038bbcSJames Wright   return 0;
54636038bbcSJames Wright }
54736038bbcSJames Wright 
54836038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
54936038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
55036038bbcSJames Wright }
55136038bbcSJames Wright 
55236038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
55336038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
55436038bbcSJames Wright }
55536038bbcSJames Wright 
55636038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
55736038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
55836038bbcSJames Wright }
55936038bbcSJames Wright 
5608561fee2SJames Wright // @brief Boundary integral for RHS of divergence of diffusive flux direct projection
56136038bbcSJames Wright CEED_QFUNCTION_HELPER int DivDiffusiveFluxBoundaryRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
56236038bbcSJames Wright                                                          StateVariable state_var) {
56336038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
56436038bbcSJames Wright   const CeedScalar(*Grad_q)        = in[1];
56536038bbcSJames Wright   const CeedScalar(*q_data)        = in[2];
56636038bbcSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
56736038bbcSJames Wright 
56836038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
56936038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
57036038bbcSJames Wright 
57136038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
57236038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
57336038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
57436038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3], normal[3];
57536038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5];
57636038bbcSJames Wright 
57736038bbcSJames Wright     QdataBoundaryGradientUnpack_3D(Q, i, q_data, &wdetJ, dXdx, normal);
57836038bbcSJames Wright     {  // Get stress and Fe
57936038bbcSJames Wright       State      grad_s[3];
58036038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
58136038bbcSJames Wright 
58236038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
58336038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
58436038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
58536038bbcSJames Wright       KMUnpack(kmstress, stress);
58636038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
58736038bbcSJames Wright     }
58836038bbcSJames Wright 
58936038bbcSJames Wright     FluxTotal_Boundary(ZeroInviscidFluxes, stress, Fe, normal, Fdiff);
59036038bbcSJames Wright 
59136038bbcSJames Wright     // Continuity has no diffusive flux, therefore skip
59236038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) v[j - 1][i] = wdetJ * Fdiff[j];
59336038bbcSJames Wright   }
59436038bbcSJames Wright   return 0;
59536038bbcSJames Wright }
59636038bbcSJames Wright 
59736038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
59836038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
59936038bbcSJames Wright }
60036038bbcSJames Wright 
60136038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60236038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
60336038bbcSJames Wright }
60436038bbcSJames Wright 
60536038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60636038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
60736038bbcSJames Wright }
60836038bbcSJames Wright 
6098561fee2SJames Wright // @brief Integral for RHS of diffusive flux indirect projection
61036038bbcSJames Wright CEED_QFUNCTION_HELPER int DiffusiveFluxRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
61136038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
61236038bbcSJames Wright   const CeedScalar(*Grad_q)        = in[1];
61336038bbcSJames Wright   const CeedScalar(*q_data)        = in[2];
61436038bbcSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
61536038bbcSJames Wright 
61636038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
61736038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
61836038bbcSJames Wright 
61936038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
62036038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
62136038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
62236038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3];
62336038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
62436038bbcSJames Wright 
62536038bbcSJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
62636038bbcSJames Wright     {  // Get stress and Fe
62736038bbcSJames Wright       State      grad_s[3];
62836038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
62936038bbcSJames Wright 
63036038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
63136038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
63236038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
63336038bbcSJames Wright       KMUnpack(kmstress, stress);
63436038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
63536038bbcSJames Wright     }
63636038bbcSJames Wright 
63736038bbcSJames Wright     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
63836038bbcSJames Wright 
63936038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
64036038bbcSJames Wright       for (CeedInt k = 0; k < 3; k++) {
64136038bbcSJames Wright         v[(j - 1) * 3 + k][i] = wdetJ * Fdiff[j][k];
64236038bbcSJames Wright       }
64336038bbcSJames Wright     }
64436038bbcSJames Wright   }
64536038bbcSJames Wright   return 0;
64636038bbcSJames Wright }
64736038bbcSJames Wright 
64836038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
64936038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
65036038bbcSJames Wright }
65136038bbcSJames Wright 
65236038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
65336038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
65436038bbcSJames Wright }
65536038bbcSJames Wright 
65636038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
65736038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
65836038bbcSJames Wright }
659