xref: /honee/qfunctions/newtonian.h (revision 97cfd714ca1074786ba0976e643af9cb13ef46ee)
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
53a8779fbSJames Wright /// Operator for Navier-Stokes example using PETSc
63a8779fbSJames Wright #include <ceed.h>
7d0cce58aSJeremy L Thompson #include <math.h>
87b530f2aSAdelekeBankole #include <stdlib.h>
92b916ea7SJeremy L Thompson 
10475b2820SJames Wright #include "newtonian_state.h"
11d0cce58aSJeremy L Thompson #include "newtonian_types.h"
12d1b9ef12SLeila Ghaffari #include "stabilization.h"
13d0cce58aSJeremy L Thompson #include "utils.h"
14bb8a0c61SJames Wright 
1594a7b3d2SKenneth E. Jansen CEED_QFUNCTION_HELPER void InternalDampingLayer(const NewtonianIdealGasContext context, const State s, const CeedScalar sigma, CeedScalar damp_Y[5],
16e7754af5SKenneth E. Jansen                                                 CeedScalar damp_residual[5]) {
17e7754af5SKenneth E. Jansen   ScaleN(damp_Y, sigma, 5);
18edcfef1bSKenneth E. Jansen   State damp_s = StateFromY_fwd(context, s, damp_Y);
19e7754af5SKenneth E. Jansen 
20e7754af5SKenneth E. Jansen   CeedScalar U[5];
21e7754af5SKenneth E. Jansen   UnpackState_U(damp_s.U, U);
22e7754af5SKenneth E. Jansen   for (int i = 0; i < 5; i++) damp_residual[i] += U[i];
23e7754af5SKenneth E. Jansen }
24e7754af5SKenneth E. Jansen 
25bb8a0c61SJames Wright // *****************************************************************************
263a8779fbSJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
273a8779fbSJames Wright // *****************************************************************************
288fff8293SJames Wright CEED_QFUNCTION_HELPER int ICsNewtonianIG(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
293a8779fbSJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
303a8779fbSJames Wright 
31bb8a0c61SJames Wright   const SetupContext context = (SetupContext)ctx;
32bb8a0c61SJames Wright 
332b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
34a541e550SJames Wright     CeedScalar q[5];
35edcfef1bSKenneth E. Jansen     State      s = StateFromPrimitive(&context->gas, context->reference);
368fff8293SJames Wright     StateToQ(&context->gas, s, q, state_var);
372b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
38b193fadcSJames Wright   }
393a8779fbSJames Wright   return 0;
403a8779fbSJames Wright }
413a8779fbSJames Wright 
429b103f75SJames Wright CEED_QFUNCTION(ICsNewtonianIG_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
439b103f75SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
449b103f75SJames Wright }
459b103f75SJames Wright 
462b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsNewtonianIG_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
478fff8293SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_PRIMITIVE);
48b8fb7609SAdeleke O. Bankole }
499b103f75SJames Wright 
509b103f75SJames Wright CEED_QFUNCTION(ICsNewtonianIG_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
519b103f75SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_ENTROPY);
52cbe60e31SLeila Ghaffari }
53cbe60e31SLeila Ghaffari 
54*97cfd714SJames Wright CEED_QFUNCTION_HELPER int MassFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
5565dee3d2SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
5665dee3d2SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[1];
5765dee3d2SJames Wright   const CeedScalar(*q_data)            = in[2];
5865dee3d2SJames Wright   CeedScalar(*v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
5965dee3d2SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]   = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
6065dee3d2SJames Wright 
6165dee3d2SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
6265dee3d2SJames Wright 
6365dee3d2SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
6465dee3d2SJames Wright     const CeedScalar qi[5]     = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
6565dee3d2SJames 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]};
6665dee3d2SJames Wright     const State      s         = StateFromQ(context, qi, state_var);
6765dee3d2SJames Wright     const State      s_dot     = StateFromQ(context, qi_dot, state_var);
6865dee3d2SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
6965dee3d2SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
7065dee3d2SJames Wright 
7165dee3d2SJames Wright     // Standard mass matrix term
7265dee3d2SJames Wright     for (CeedInt f = 0; f < 5; f++) {
7365dee3d2SJames Wright       v[f][i] = wdetJ * qi_dot[f];
7465dee3d2SJames Wright     }
7565dee3d2SJames Wright 
7665dee3d2SJames Wright     // Stabilization method: none (Galerkin), SU, or SUPG
7765dee3d2SJames Wright     State      grad_s[3] = {{{0.}}};
788c85b835SJames Wright     CeedScalar Tau_d[3], stab[5][3], body_force[5] = {0.}, divFdiff[5] = {0.}, U_dot[5];
7965dee3d2SJames Wright     UnpackState_U(s_dot.U, U_dot);
8065dee3d2SJames Wright     Tau_diagPrim(context, s, dXdx, context->dt, Tau_d);
818c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff, stab);
8265dee3d2SJames Wright 
8365dee3d2SJames Wright     // Stabilized mass term
8465dee3d2SJames Wright     for (CeedInt j = 0; j < 5; j++) {
8565dee3d2SJames Wright       for (CeedInt k = 0; k < 3; k++) {
8665dee3d2SJames 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]);
8765dee3d2SJames Wright       }
8865dee3d2SJames Wright     }
8965dee3d2SJames Wright   }
90*97cfd714SJames Wright   return 0;
9165dee3d2SJames Wright }
9265dee3d2SJames Wright 
9365dee3d2SJames Wright CEED_QFUNCTION(MassFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
94*97cfd714SJames Wright   return MassFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
9565dee3d2SJames Wright }
9665dee3d2SJames Wright 
97cbe60e31SLeila Ghaffari // *****************************************************************************
9804e40bb6SJeremy L Thompson // This QFunction implements the following formulation of Navier-Stokes with explicit time stepping method
993a8779fbSJames Wright //
10004e40bb6SJeremy L Thompson // This is 3D compressible Navier-Stokes in conservation form with state variables of density, momentum density, and total energy density.
1013a8779fbSJames Wright //
1023a8779fbSJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
1033a8779fbSJames Wright //   rho - Mass Density
1043a8779fbSJames Wright //   Ui  - Momentum Density,      Ui = rho ui
1053a8779fbSJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
1063a8779fbSJames Wright //
1073a8779fbSJames Wright // Navier-Stokes Equations:
1083a8779fbSJames Wright //   drho/dt + div( U )                               = 0
1093a8779fbSJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
1103a8779fbSJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
1113a8779fbSJames Wright //
1123a8779fbSJames Wright // Viscous Stress:
1133a8779fbSJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
1143a8779fbSJames Wright //
1153a8779fbSJames Wright // Thermal Stress:
1163a8779fbSJames Wright //   Fe = u Fu + k grad( T )
117bb8a0c61SJames Wright // Equation of State
1183a8779fbSJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
1193a8779fbSJames Wright //
1203a8779fbSJames Wright // Stabilization:
1213a8779fbSJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
1223a8779fbSJames Wright //     f1 = rho  sqrt(ui uj gij)
1233a8779fbSJames Wright //     gij = dXi/dX * dXi/dX
1243a8779fbSJames Wright //     TauC = Cc f1 / (8 gii)
1253a8779fbSJames Wright //     TauM = min( 1 , 1 / f1 )
1263a8779fbSJames Wright //     TauE = TauM / (Ce cv)
1273a8779fbSJames Wright //
1283a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
1293a8779fbSJames Wright //
1303a8779fbSJames Wright // Constants:
1313a8779fbSJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
1323a8779fbSJames Wright //   mu              ,  Dynamic viscosity
1333a8779fbSJames Wright //   k               ,  Thermal conductivity
1343a8779fbSJames Wright //   cv              ,  Specific heat, constant volume
1353a8779fbSJames Wright //   cp              ,  Specific heat, constant pressure
1363a8779fbSJames Wright //   g               ,  Gravity
1373a8779fbSJames Wright //   gamma  = cp / cv,  Specific heat ratio
1383a8779fbSJames Wright //
13904e40bb6SJeremy 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
14004e40bb6SJeremy L Thompson // gradu )
1413a8779fbSJames Wright // *****************************************************************************
1422b916ea7SJeremy L Thompson CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1433d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[0];
14487bd45e7SJames Wright   const CeedScalar(*Grad_q)          = in[1];
145ade49511SJames Wright   const CeedScalar(*q_data)          = in[2];
1460a32a5aaSJames Wright   const CeedScalar(*x)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[3];
1473d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
1483d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
1493a8779fbSJames Wright 
1503a8779fbSJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
151bb8a0c61SJames Wright   const CeedScalar        *g       = context->g;
152bb8a0c61SJames Wright   const CeedScalar         dt      = context->dt;
1530a32a5aaSJames Wright   const CeedScalar         P0      = context->idl_pressure;
1543a8779fbSJames Wright 
1553d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
156ade49511SJames Wright     CeedScalar       U[5], wdetJ, dXdx[3][3];
1570a32a5aaSJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
158c1a52365SJed Brown     for (int j = 0; j < 5; j++) U[j] = q[j][i];
1591be49596SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
160edcfef1bSKenneth E. Jansen     State s = StateFromU(context, U);
161c1a52365SJed Brown 
162c1a52365SJed Brown     State grad_s[3];
163edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, STATEVAR_CONSERVATIVE, Grad_q, dXdx, grad_s);
164c1a52365SJed Brown 
165c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
16640a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
167c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
168c1a52365SJed Brown     KMUnpack(kmstress, stress);
169c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
170c1a52365SJed Brown 
171c1a52365SJed Brown     StateConservative F_inviscid[3];
172c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
173c1a52365SJed Brown 
174c1a52365SJed Brown     // Total flux
175c1a52365SJed Brown     CeedScalar Flux[5][3];
176d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
177c1a52365SJed Brown 
1787523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
1797523f6aaSJames 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]);
1802b916ea7SJeremy L Thompson     }
181c1a52365SJed Brown 
18260dbb574SKenneth 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)};
1832b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
1843a8779fbSJames Wright 
1850a32a5aaSJames Wright     if (context->idl_enable) {
1860a32a5aaSJames Wright       const CeedScalar sigma         = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
1870a32a5aaSJames Wright       CeedScalar       damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
1880a32a5aaSJames Wright       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
1890a32a5aaSJames Wright       for (int j = 0; j < 5; j++) v[j][i] -= wdetJ * idl_residual[j];
1900a32a5aaSJames Wright     }
1910a32a5aaSJames Wright 
192d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
1938c85b835SJames Wright     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, zeroFlux[5] = {0.};
194d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
1958c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, zeroFlux, stab);
1963a8779fbSJames Wright 
1972b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
1982b916ea7SJeremy 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]);
1992b916ea7SJeremy L Thompson     }
200b193fadcSJames Wright   }
2013a8779fbSJames Wright   return 0;
2023a8779fbSJames Wright }
2033a8779fbSJames Wright 
2043a8779fbSJames Wright // *****************************************************************************
20504e40bb6SJeremy L Thompson // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
2063a8779fbSJames Wright //
2073a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
2083a8779fbSJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
20904e40bb6SJeremy L Thompson //                                       (diffusive terms will be added later)
2103a8779fbSJames Wright // *****************************************************************************
2118fff8293SJames Wright CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
2128c85b835SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
2138c85b835SJames Wright 
2143d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[0];
21587bd45e7SJames Wright   const CeedScalar(*Grad_q)               = in[1];
2163d65b166SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])in[2];
217ade49511SJames Wright   const CeedScalar(*q_data)               = in[3];
2183d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[4];
2198c85b835SJames Wright   const CeedScalar(*divFdiff)[CEED_Q_VLA] = context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE ? (const CeedScalar(*)[CEED_Q_VLA])in[5] : NULL;
2203d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]              = (CeedScalar(*)[CEED_Q_VLA])out[0];
2213d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]      = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
222ade49511SJames Wright   CeedScalar(*jac_data)                   = out[2];
2233d65b166SJames Wright 
224bb8a0c61SJames Wright   const CeedScalar *g  = context->g;
225bb8a0c61SJames Wright   const CeedScalar  dt = context->dt;
226fcb2c22aSJames Wright   const CeedScalar  P0 = context->idl_pressure;
2273a8779fbSJames Wright 
2283d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2293d65b166SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
230c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
231edcfef1bSKenneth E. Jansen     const State      s      = StateFromQ(context, qi, state_var);
232c1a52365SJed Brown 
233ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
234ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
235c1a52365SJed Brown     State grad_s[3];
236edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
237c1a52365SJed Brown 
238c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
23940a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
240c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
241c1a52365SJed Brown     KMUnpack(kmstress, stress);
242c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
243c1a52365SJed Brown 
244c1a52365SJed Brown     StateConservative F_inviscid[3];
245c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
246c1a52365SJed Brown 
247c1a52365SJed Brown     // Total flux
248c1a52365SJed Brown     CeedScalar Flux[5][3];
249d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
250c1a52365SJed Brown 
2517523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
2527523f6aaSJames Wright       for (CeedInt k = 0; k < 3; k++) {
2537523f6aaSJames 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]);
2543d65b166SJames Wright       }
2552b916ea7SJeremy L Thompson     }
256c1a52365SJed Brown 
25760dbb574SKenneth 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)};
2583a8779fbSJames Wright 
259d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
260edcfef1bSKenneth E. Jansen     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5];
26176555becSJames Wright     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
262edcfef1bSKenneth E. Jansen     State s_dot = StateFromQ_fwd(context, s, qi_dot, state_var);
26376555becSJames Wright     UnpackState_U(s_dot.U, U_dot);
26476555becSJames Wright 
2652b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
266e7754af5SKenneth E. Jansen     if (context->idl_enable) {
26794a7b3d2SKenneth E. Jansen       const CeedScalar sigma = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
26894a7b3d2SKenneth E. Jansen       StoredValuesPack(Q, i, 14, 1, &sigma, jac_data);
269e7754af5SKenneth E. Jansen       CeedScalar damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
27094a7b3d2SKenneth E. Jansen       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
271e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
272e7754af5SKenneth E. Jansen     }
273e7754af5SKenneth E. Jansen 
2748c85b835SJames Wright     CeedScalar divFdiff_i[5] = {0.};
2758c85b835SJames Wright     if (context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE) {
2768c85b835SJames Wright       for (int j = 1; j < 5; j++) divFdiff_i[j] = divFdiff[j - 1][i];
2778c85b835SJames Wright     }
278d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
2798c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff_i, stab);
2803a8779fbSJames Wright 
2812b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
2823d65b166SJames Wright       for (CeedInt k = 0; k < 3; k++) {
2833d65b166SJames 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]);
2843d65b166SJames Wright       }
2852b916ea7SJeremy L Thompson     }
286ade49511SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
287ade49511SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
288ade49511SJames Wright     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
289b193fadcSJames Wright   }
2903a8779fbSJames Wright   return 0;
2913a8779fbSJames Wright }
292f0b65372SJed Brown 
2932b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2948fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
29576555becSJames Wright }
29676555becSJames Wright 
2972b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2988fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
29976555becSJames Wright }
30076555becSJames Wright 
3019b103f75SJames Wright CEED_QFUNCTION(IFunction_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3029b103f75SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
3039b103f75SJames Wright }
3049b103f75SJames Wright 
305cbe60e31SLeila Ghaffari // *****************************************************************************
30604e40bb6SJeremy L Thompson // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
307cbe60e31SLeila Ghaffari // *****************************************************************************
3088fff8293SJames Wright CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3093d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[0];
31087bd45e7SJames Wright   const CeedScalar(*Grad_dq)         = in[1];
311ade49511SJames Wright   const CeedScalar(*q_data)          = in[2];
31294a7b3d2SKenneth E. Jansen   const CeedScalar(*jac_data)        = in[3];
3133d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
3143d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
3153d65b166SJames Wright 
316f0b65372SJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
317f0b65372SJed Brown   const CeedScalar        *g       = context->g;
318f0b65372SJed Brown 
3193d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
320ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
321ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
322f0b65372SJed Brown 
3238789e95fSJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
324ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
325ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
326ade49511SJames Wright     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
327edcfef1bSKenneth E. Jansen     State s = StateFromQ(context, qi, state_var);
328f0b65372SJed Brown 
329edcfef1bSKenneth E. Jansen     CeedScalar dqi[5];
33076555becSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
331edcfef1bSKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
332f0b65372SJed Brown 
333f0b65372SJed Brown     State grad_ds[3];
334edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
335f0b65372SJed Brown 
336f0b65372SJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
33740a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
338f0b65372SJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
339f0b65372SJed Brown     KMUnpack(dkmstress, dstress);
340f0b65372SJed Brown     KMUnpack(kmstress, stress);
341f0b65372SJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
342f0b65372SJed Brown 
343f0b65372SJed Brown     StateConservative dF_inviscid[3];
344f0b65372SJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
345f0b65372SJed Brown 
346f0b65372SJed Brown     // Total flux
347f0b65372SJed Brown     CeedScalar dFlux[5][3];
348d1b9ef12SLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
349f0b65372SJed Brown 
35022387d3aSJames Wright     for (int j = 0; j < 5; j++) {
35122387d3aSJames 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]);
3522b916ea7SJeremy L Thompson     }
353f0b65372SJed Brown 
35460dbb574SKenneth 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)};
35576555becSJames Wright     CeedScalar       dU[5]          = {0.};
35676555becSJames Wright     UnpackState_U(ds.U, dU);
3572b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
358f0b65372SJed Brown 
359e7754af5SKenneth E. Jansen     if (context->idl_enable) {
36094a7b3d2SKenneth E. Jansen       const CeedScalar sigma         = jac_data[14 * Q + i];
361e7754af5SKenneth E. Jansen       CeedScalar       damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
362e7754af5SKenneth E. Jansen       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
36394a7b3d2SKenneth E. Jansen       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
364e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
365e7754af5SKenneth E. Jansen     }
366e7754af5SKenneth E. Jansen 
367d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
368d1b9ef12SLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
369d1b9ef12SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
3708c85b835SJames Wright     const CeedScalar zeroFlux[5] = {0.};
3718c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, zeroFlux, dstab);
372d1b9ef12SLeila Ghaffari 
3732b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) {
3742b916ea7SJeremy 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]);
3752b916ea7SJeremy L Thompson     }
376b193fadcSJames Wright   }
377f0b65372SJed Brown   return 0;
378f0b65372SJed Brown }
3798085925cSJames Wright 
3802b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3818fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
38276555becSJames Wright }
38376555becSJames Wright 
3842b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3858fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
38676555becSJames Wright }
38776555becSJames Wright 
3889b103f75SJames Wright CEED_QFUNCTION(IJacobian_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3899b103f75SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
3909b103f75SJames Wright }
3919b103f75SJames Wright 
392d1b9ef12SLeila Ghaffari // *****************************************************************************
3938085925cSJames Wright // Compute boundary integral (ie. for strongly set inflows)
394d1b9ef12SLeila Ghaffari // *****************************************************************************
3958fff8293SJames Wright CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3964b96a86bSJames Wright   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
3973d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]       = (const CeedScalar(*)[CEED_Q_VLA])in[0];
39887bd45e7SJames Wright   const CeedScalar(*Grad_q)              = in[1];
399ade49511SJames Wright   const CeedScalar(*q_data_sur)          = in[2];
4003d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]             = (CeedScalar(*)[CEED_Q_VLA])out[0];
4014b96a86bSJames Wright   CeedScalar(*jac_data_sur)              = context->is_implicit ? out[1] : NULL;
4028085925cSJames Wright 
403d3b25f3aSJames Wright   const bool is_implicit = context->is_implicit;
4048085925cSJames Wright 
4052b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
40641e73928SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
407edcfef1bSKenneth E. Jansen     State            s     = StateFromQ(context, qi, state_var);
4088085925cSJames Wright 
40978e8b7daSJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
41078e8b7daSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
411ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
4128085925cSJames Wright 
413d3b25f3aSJames Wright     State grad_s[3];
414edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
4158085925cSJames Wright 
416d3b25f3aSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
41740a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
418d3b25f3aSJames Wright     NewtonianStress(context, strain_rate, kmstress);
419d3b25f3aSJames Wright     KMUnpack(kmstress, stress);
420d3b25f3aSJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
421d3b25f3aSJames Wright 
422d3b25f3aSJames Wright     StateConservative F_inviscid[3];
423d3b25f3aSJames Wright     FluxInviscid(context, s, F_inviscid);
424d3b25f3aSJames Wright 
425c5740391SJames Wright     CeedScalar Flux[5];
42678e8b7daSJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, normal, Flux);
427d3b25f3aSJames Wright 
428c5740391SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
4298085925cSJames Wright 
4304b96a86bSJames Wright     if (is_implicit) {
431ade49511SJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
432ade49511SJames Wright       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
4338085925cSJames Wright     }
4344b96a86bSJames Wright   }
4358085925cSJames Wright   return 0;
4368085925cSJames Wright }
4378085925cSJames Wright 
4382b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4398fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
440d4559bbeSJames Wright }
441d4559bbeSJames Wright 
4422b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4438fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
444d4559bbeSJames Wright }
445d4559bbeSJames Wright 
4469b103f75SJames Wright CEED_QFUNCTION(BoundaryIntegral_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4479b103f75SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_ENTROPY);
4489b103f75SJames Wright }
4499b103f75SJames Wright 
450d1b9ef12SLeila Ghaffari // *****************************************************************************
45168ae065aSJames Wright // Jacobian for "set nothing" boundary integral
452d1b9ef12SLeila Ghaffari // *****************************************************************************
4532b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
4548fff8293SJames Wright                                                     StateVariable state_var) {
4553d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
45687bd45e7SJames Wright   const CeedScalar(*Grad_dq)        = in[1];
457ade49511SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
458c1484fadSKenneth E. Jansen   const CeedScalar(*jac_data_sur)   = in[4];
45968ae065aSJames Wright   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
46068ae065aSJames Wright 
46168ae065aSJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
462ade49511SJames Wright   const bool                     is_implicit = context->is_implicit;
46368ae065aSJames Wright 
4643d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
46578e8b7daSJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
46678e8b7daSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
467ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
46868ae065aSJames Wright 
469edcfef1bSKenneth E. Jansen     CeedScalar qi[5], kmstress[6], dqi[5];
470ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
471ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
47241e73928SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
4733934e2b1SJames Wright 
474edcfef1bSKenneth E. Jansen     State s  = StateFromQ(context, qi, state_var);
475edcfef1bSKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
47668ae065aSJames Wright 
47768ae065aSJames Wright     State grad_ds[3];
478edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
47968ae065aSJames Wright 
48068ae065aSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
48140a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
48268ae065aSJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
48368ae065aSJames Wright     KMUnpack(dkmstress, dstress);
48468ae065aSJames Wright     KMUnpack(kmstress, stress);
48568ae065aSJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
48668ae065aSJames Wright 
48768ae065aSJames Wright     StateConservative dF_inviscid[3];
48868ae065aSJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
48968ae065aSJames Wright 
490c5740391SJames Wright     CeedScalar dFlux[5];
49178e8b7daSJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, normal, dFlux);
49268ae065aSJames Wright 
493c5740391SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
494512c8ec7SJames Wright   }
49568ae065aSJames Wright   return 0;
49668ae065aSJames Wright }
49768ae065aSJames Wright 
4982b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4998fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
500d4559bbeSJames Wright }
501d4559bbeSJames Wright 
5022b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5038fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
504d4559bbeSJames Wright }
5059b103f75SJames Wright 
5069b103f75SJames Wright CEED_QFUNCTION(BoundaryIntegral_Jacobian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5079b103f75SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_ENTROPY);
5089b103f75SJames Wright }
50936038bbcSJames Wright 
5108561fee2SJames Wright // @brief Volume integral for RHS of divergence of diffusive flux direct projection
51136038bbcSJames Wright CEED_QFUNCTION_HELPER int DivDiffusiveFluxVolumeRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
51236038bbcSJames Wright                                                        StateVariable state_var) {
51336038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[0];
51436038bbcSJames Wright   const CeedScalar(*Grad_q)          = in[1];
51536038bbcSJames Wright   const CeedScalar(*q_data)          = in[2];
51636038bbcSJames Wright   CeedScalar(*Grad_v)[4][CEED_Q_VLA] = (CeedScalar(*)[4][CEED_Q_VLA])out[0];
51736038bbcSJames Wright 
51836038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
51936038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
52036038bbcSJames Wright 
52136038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
52236038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
52336038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
52436038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3];
52536038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
52636038bbcSJames Wright 
52736038bbcSJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
52836038bbcSJames Wright     {  // Get stress and Fe
52936038bbcSJames Wright       State      grad_s[3];
53036038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
53136038bbcSJames Wright 
53236038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
53336038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
53436038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
53536038bbcSJames Wright       KMUnpack(kmstress, stress);
53636038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
53736038bbcSJames Wright     }
53836038bbcSJames Wright 
53936038bbcSJames Wright     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
54036038bbcSJames Wright 
54136038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
54236038bbcSJames Wright       for (CeedInt k = 0; k < 3; k++) {
54336038bbcSJames Wright         Grad_v[k][j - 1][i] = -wdetJ * Dot3(dXdx[k], Fdiff[j]);
54436038bbcSJames Wright       }
54536038bbcSJames Wright     }
54636038bbcSJames Wright   }
54736038bbcSJames Wright   return 0;
54836038bbcSJames Wright }
54936038bbcSJames Wright 
55036038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
55136038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
55236038bbcSJames Wright }
55336038bbcSJames Wright 
55436038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
55536038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
55636038bbcSJames Wright }
55736038bbcSJames Wright 
55836038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
55936038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
56036038bbcSJames Wright }
56136038bbcSJames Wright 
5628561fee2SJames Wright // @brief Boundary integral for RHS of divergence of diffusive flux direct projection
56336038bbcSJames Wright CEED_QFUNCTION_HELPER int DivDiffusiveFluxBoundaryRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
56436038bbcSJames Wright                                                          StateVariable state_var) {
56536038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
56636038bbcSJames Wright   const CeedScalar(*Grad_q)        = in[1];
56736038bbcSJames Wright   const CeedScalar(*q_data)        = in[2];
56836038bbcSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
56936038bbcSJames Wright 
57036038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
57136038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
57236038bbcSJames Wright 
57336038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
57436038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
57536038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
57636038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3], normal[3];
57736038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5];
57836038bbcSJames Wright 
57936038bbcSJames Wright     QdataBoundaryGradientUnpack_3D(Q, i, q_data, &wdetJ, dXdx, normal);
58036038bbcSJames Wright     {  // Get stress and Fe
58136038bbcSJames Wright       State      grad_s[3];
58236038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
58336038bbcSJames Wright 
58436038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
58536038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
58636038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
58736038bbcSJames Wright       KMUnpack(kmstress, stress);
58836038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
58936038bbcSJames Wright     }
59036038bbcSJames Wright 
59136038bbcSJames Wright     FluxTotal_Boundary(ZeroInviscidFluxes, stress, Fe, normal, Fdiff);
59236038bbcSJames Wright 
59336038bbcSJames Wright     // Continuity has no diffusive flux, therefore skip
59436038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) v[j - 1][i] = wdetJ * Fdiff[j];
59536038bbcSJames Wright   }
59636038bbcSJames Wright   return 0;
59736038bbcSJames Wright }
59836038bbcSJames Wright 
59936038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60036038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
60136038bbcSJames Wright }
60236038bbcSJames Wright 
60336038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60436038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
60536038bbcSJames Wright }
60636038bbcSJames Wright 
60736038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60836038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
60936038bbcSJames Wright }
61036038bbcSJames Wright 
6118561fee2SJames Wright // @brief Integral for RHS of diffusive flux indirect projection
61236038bbcSJames Wright CEED_QFUNCTION_HELPER int DiffusiveFluxRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
61336038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
61436038bbcSJames Wright   const CeedScalar(*Grad_q)        = in[1];
61536038bbcSJames Wright   const CeedScalar(*q_data)        = in[2];
61636038bbcSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
61736038bbcSJames Wright 
61836038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
61936038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
62036038bbcSJames Wright 
62136038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
62236038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
62336038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
62436038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3];
62536038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
62636038bbcSJames Wright 
62736038bbcSJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
62836038bbcSJames Wright     {  // Get stress and Fe
62936038bbcSJames Wright       State      grad_s[3];
63036038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
63136038bbcSJames Wright 
63236038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
63336038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
63436038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
63536038bbcSJames Wright       KMUnpack(kmstress, stress);
63636038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
63736038bbcSJames Wright     }
63836038bbcSJames Wright 
63936038bbcSJames Wright     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
64036038bbcSJames Wright 
64136038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
64236038bbcSJames Wright       for (CeedInt k = 0; k < 3; k++) {
64336038bbcSJames Wright         v[(j - 1) * 3 + k][i] = wdetJ * Fdiff[j][k];
64436038bbcSJames Wright       }
64536038bbcSJames Wright     }
64636038bbcSJames Wright   }
64736038bbcSJames Wright   return 0;
64836038bbcSJames Wright }
64936038bbcSJames Wright 
65036038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
65136038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
65236038bbcSJames Wright }
65336038bbcSJames Wright 
65436038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
65536038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
65636038bbcSJames Wright }
65736038bbcSJames Wright 
65836038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
65936038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
66036038bbcSJames Wright }
661