xref: /honee/qfunctions/newtonian.h (revision b3b248287fd0560e63edc72c42da511f4b359741)
1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
33a8779fbSJames Wright 
43a8779fbSJames Wright /// @file
5ea615d4cSJames Wright /// Newtonian fluids operator for HONEE
63e17a7a1SJames Wright #include <ceed/types.h>
72b916ea7SJeremy L Thompson 
8475b2820SJames Wright #include "newtonian_state.h"
9d0cce58aSJeremy L Thompson #include "newtonian_types.h"
10d1b9ef12SLeila Ghaffari #include "stabilization.h"
11d0cce58aSJeremy L Thompson #include "utils.h"
12bb8a0c61SJames Wright 
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) {
141*b3b24828SJames Wright   NewtonianIdealGasContext context      = (NewtonianIdealGasContext)ctx;
142*b3b24828SJames Wright   const bool               use_divFdiff = context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE;
143*b3b24828SJames Wright 
1443d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[0];
14587bd45e7SJames Wright   const CeedScalar(*Grad_q)               = in[1];
146ade49511SJames Wright   const CeedScalar(*q_data)               = in[2];
1470a32a5aaSJames Wright   const CeedScalar(*x)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[3];
148*b3b24828SJames Wright   const CeedScalar(*divFdiff)[CEED_Q_VLA] = use_divFdiff ? (const CeedScalar(*)[CEED_Q_VLA])in[4] : NULL;
1493d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]              = (CeedScalar(*)[CEED_Q_VLA])out[0];
1503d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]      = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
1513a8779fbSJames Wright 
152bb8a0c61SJames Wright   const CeedScalar *g            = context->g;
153bb8a0c61SJames Wright   const CeedScalar  dt           = context->dt;
154*b3b24828SJames Wright   const CeedScalar  idl_pressure = context->idl_pressure;
1553a8779fbSJames Wright 
1563d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
157ade49511SJames Wright     CeedScalar       U[5], wdetJ, dXdx[3][3];
1580a32a5aaSJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
159c1a52365SJed Brown     for (int j = 0; j < 5; j++) U[j] = q[j][i];
1601be49596SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
161edcfef1bSKenneth E. Jansen     State s = StateFromU(context, U);
162c1a52365SJed Brown 
163c1a52365SJed Brown     State grad_s[3];
164edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, STATEVAR_CONSERVATIVE, Grad_q, dXdx, grad_s);
165c1a52365SJed Brown 
166c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
16740a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
168c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
169c1a52365SJed Brown     KMUnpack(kmstress, stress);
170c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
171c1a52365SJed Brown 
172c1a52365SJed Brown     StateConservative F_inviscid[3];
173c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
174c1a52365SJed Brown 
175c1a52365SJed Brown     // Total flux
176c1a52365SJed Brown     CeedScalar Flux[5][3];
177d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
178c1a52365SJed Brown 
1797523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
1807523f6aaSJames 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]);
1812b916ea7SJeremy L Thompson     }
182c1a52365SJed Brown 
18360dbb574SKenneth 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)};
1842b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
1853a8779fbSJames Wright 
1860a32a5aaSJames Wright     if (context->idl_enable) {
1870a32a5aaSJames Wright       const CeedScalar sigma         = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
188*b3b24828SJames Wright       CeedScalar       damp_state[5] = {s.Y.pressure - idl_pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
1890a32a5aaSJames Wright       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
1900a32a5aaSJames Wright       for (int j = 0; j < 5; j++) v[j][i] -= wdetJ * idl_residual[j];
1910a32a5aaSJames Wright     }
1920a32a5aaSJames Wright 
193*b3b24828SJames Wright     CeedScalar divFdiff_i[5] = {0.};
194*b3b24828SJames Wright     if (use_divFdiff)
195*b3b24828SJames Wright       for (int j = 1; j < 5; j++) divFdiff_i[j] = divFdiff[j - 1][i];
196*b3b24828SJames Wright 
197d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
198*b3b24828SJames Wright     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0};
199d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
200*b3b24828SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff_i, stab);
2013a8779fbSJames Wright 
2022b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
2032b916ea7SJeremy 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]);
2042b916ea7SJeremy L Thompson     }
205b193fadcSJames Wright   }
2063a8779fbSJames Wright   return 0;
2073a8779fbSJames Wright }
2083a8779fbSJames Wright 
2093a8779fbSJames Wright // *****************************************************************************
21004e40bb6SJeremy L Thompson // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
2113a8779fbSJames Wright //
2123a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
2133a8779fbSJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
21404e40bb6SJeremy L Thompson //                                       (diffusive terms will be added later)
2153a8779fbSJames Wright // *****************************************************************************
2168fff8293SJames Wright CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
2178c85b835SJames Wright   NewtonianIdealGasContext context      = (NewtonianIdealGasContext)ctx;
218ff684e42SJames Wright   const bool               use_divFdiff = context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE;
2198c85b835SJames Wright 
2203d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[0];
22187bd45e7SJames Wright   const CeedScalar(*Grad_q)               = in[1];
2223d65b166SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])in[2];
223ade49511SJames Wright   const CeedScalar(*q_data)               = in[3];
2243d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[4];
225ff684e42SJames Wright   const CeedScalar(*divFdiff)[CEED_Q_VLA] = use_divFdiff ? (const CeedScalar(*)[CEED_Q_VLA])in[5] : NULL;
2263d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]              = (CeedScalar(*)[CEED_Q_VLA])out[0];
2273d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]      = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
228ade49511SJames Wright   CeedScalar(*jac_data)                   = out[2];
2293d65b166SJames Wright 
230bb8a0c61SJames Wright   const CeedScalar *g            = context->g;
231bb8a0c61SJames Wright   const CeedScalar  dt           = context->dt;
232ff684e42SJames Wright   const CeedScalar  idl_pressure = context->idl_pressure;
2333a8779fbSJames Wright 
2343d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2353d65b166SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
236c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
237edcfef1bSKenneth E. Jansen     const State      s      = StateFromQ(context, qi, state_var);
238c1a52365SJed Brown 
239ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
240ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
241c1a52365SJed Brown     State grad_s[3];
242edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
243c1a52365SJed Brown 
244c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
24540a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
246c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
247c1a52365SJed Brown     KMUnpack(kmstress, stress);
248c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
249c1a52365SJed Brown 
250c1a52365SJed Brown     StateConservative F_inviscid[3];
251c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
252c1a52365SJed Brown 
253c1a52365SJed Brown     // Total flux
254c1a52365SJed Brown     CeedScalar Flux[5][3];
255d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
256c1a52365SJed Brown 
2577523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
2587523f6aaSJames Wright       for (CeedInt k = 0; k < 3; k++) {
2597523f6aaSJames 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]);
2603d65b166SJames Wright       }
2612b916ea7SJeremy L Thompson     }
262c1a52365SJed Brown 
26360dbb574SKenneth 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)};
2643a8779fbSJames Wright 
265d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
266edcfef1bSKenneth E. Jansen     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5];
26776555becSJames Wright     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
268edcfef1bSKenneth E. Jansen     State s_dot = StateFromQ_fwd(context, s, qi_dot, state_var);
26976555becSJames Wright     UnpackState_U(s_dot.U, U_dot);
27076555becSJames Wright 
2712b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
272e7754af5SKenneth E. Jansen     if (context->idl_enable) {
27394a7b3d2SKenneth E. Jansen       const CeedScalar sigma = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
27494a7b3d2SKenneth E. Jansen       StoredValuesPack(Q, i, 14, 1, &sigma, jac_data);
275ff684e42SJames Wright       CeedScalar damp_state[5] = {s.Y.pressure - idl_pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
27694a7b3d2SKenneth E. Jansen       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
277e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
278e7754af5SKenneth E. Jansen     }
279e7754af5SKenneth E. Jansen 
2808c85b835SJames Wright     CeedScalar divFdiff_i[5] = {0.};
281ff684e42SJames Wright     if (use_divFdiff) {
2828c85b835SJames Wright       for (int j = 1; j < 5; j++) divFdiff_i[j] = divFdiff[j - 1][i];
2838c85b835SJames Wright     }
284d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
2858c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff_i, stab);
2863a8779fbSJames Wright 
2872b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
2883d65b166SJames Wright       for (CeedInt k = 0; k < 3; k++) {
2893d65b166SJames 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]);
2903d65b166SJames Wright       }
2912b916ea7SJeremy L Thompson     }
292ade49511SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
293ade49511SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
294ade49511SJames Wright     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
295b193fadcSJames Wright   }
2963a8779fbSJames Wright   return 0;
2973a8779fbSJames Wright }
298f0b65372SJed Brown 
2992b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3008fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
30176555becSJames Wright }
30276555becSJames Wright 
3032b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3048fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
30576555becSJames Wright }
30676555becSJames Wright 
3079b103f75SJames Wright CEED_QFUNCTION(IFunction_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3089b103f75SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
3099b103f75SJames Wright }
3109b103f75SJames Wright 
311cbe60e31SLeila Ghaffari // *****************************************************************************
31204e40bb6SJeremy L Thompson // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
313cbe60e31SLeila Ghaffari // *****************************************************************************
3148fff8293SJames Wright CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3153d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[0];
31687bd45e7SJames Wright   const CeedScalar(*Grad_dq)         = in[1];
317ade49511SJames Wright   const CeedScalar(*q_data)          = in[2];
31894a7b3d2SKenneth E. Jansen   const CeedScalar(*jac_data)        = in[3];
3193d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
3203d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
3213d65b166SJames Wright 
322f0b65372SJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
323f0b65372SJed Brown   const CeedScalar        *g       = context->g;
324f0b65372SJed Brown 
3253d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
326ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
327ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
328f0b65372SJed Brown 
3298789e95fSJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
330ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
331ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
332ade49511SJames Wright     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
333edcfef1bSKenneth E. Jansen     State s = StateFromQ(context, qi, state_var);
334f0b65372SJed Brown 
335edcfef1bSKenneth E. Jansen     CeedScalar dqi[5];
33676555becSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
337edcfef1bSKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
338f0b65372SJed Brown 
339f0b65372SJed Brown     State grad_ds[3];
340edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
341f0b65372SJed Brown 
342f0b65372SJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
34340a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
344f0b65372SJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
345f0b65372SJed Brown     KMUnpack(dkmstress, dstress);
346f0b65372SJed Brown     KMUnpack(kmstress, stress);
347f0b65372SJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
348f0b65372SJed Brown 
349f0b65372SJed Brown     StateConservative dF_inviscid[3];
350f0b65372SJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
351f0b65372SJed Brown 
352f0b65372SJed Brown     // Total flux
353f0b65372SJed Brown     CeedScalar dFlux[5][3];
354d1b9ef12SLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
355f0b65372SJed Brown 
35622387d3aSJames Wright     for (int j = 0; j < 5; j++) {
35722387d3aSJames 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]);
3582b916ea7SJeremy L Thompson     }
359f0b65372SJed Brown 
36060dbb574SKenneth 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)};
36176555becSJames Wright     CeedScalar       dU[5]          = {0.};
36276555becSJames Wright     UnpackState_U(ds.U, dU);
3632b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
364f0b65372SJed Brown 
365e7754af5SKenneth E. Jansen     if (context->idl_enable) {
36694a7b3d2SKenneth E. Jansen       const CeedScalar sigma         = jac_data[14 * Q + i];
367e7754af5SKenneth E. Jansen       CeedScalar       damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
368e7754af5SKenneth E. Jansen       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
36994a7b3d2SKenneth E. Jansen       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
370e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
371e7754af5SKenneth E. Jansen     }
372e7754af5SKenneth E. Jansen 
373d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
374d1b9ef12SLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
375d1b9ef12SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
3768c85b835SJames Wright     const CeedScalar zeroFlux[5] = {0.};
3778c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, zeroFlux, dstab);
378d1b9ef12SLeila Ghaffari 
3792b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) {
3802b916ea7SJeremy 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]);
3812b916ea7SJeremy L Thompson     }
382b193fadcSJames Wright   }
383f0b65372SJed Brown   return 0;
384f0b65372SJed Brown }
3858085925cSJames Wright 
3862b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3878fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
38876555becSJames Wright }
38976555becSJames Wright 
3902b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3918fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
39276555becSJames Wright }
39376555becSJames Wright 
3949b103f75SJames Wright CEED_QFUNCTION(IJacobian_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3959b103f75SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
3969b103f75SJames Wright }
3979b103f75SJames Wright 
398d1b9ef12SLeila Ghaffari // *****************************************************************************
3998085925cSJames Wright // Compute boundary integral (ie. for strongly set inflows)
400d1b9ef12SLeila Ghaffari // *****************************************************************************
4018fff8293SJames Wright CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
4024b96a86bSJames Wright   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
4033d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]       = (const CeedScalar(*)[CEED_Q_VLA])in[0];
40487bd45e7SJames Wright   const CeedScalar(*Grad_q)              = in[1];
405ade49511SJames Wright   const CeedScalar(*q_data_sur)          = in[2];
4063d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]             = (CeedScalar(*)[CEED_Q_VLA])out[0];
4074b96a86bSJames Wright   CeedScalar(*jac_data_sur)              = context->is_implicit ? out[1] : NULL;
4088085925cSJames Wright 
409d3b25f3aSJames Wright   const bool is_implicit = context->is_implicit;
4108085925cSJames Wright 
4112b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
41241e73928SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
413edcfef1bSKenneth E. Jansen     State            s     = StateFromQ(context, qi, state_var);
4148085925cSJames Wright 
41578e8b7daSJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
41678e8b7daSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
417ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
4188085925cSJames Wright 
419d3b25f3aSJames Wright     State grad_s[3];
420edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
4218085925cSJames Wright 
422d3b25f3aSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
42340a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
424d3b25f3aSJames Wright     NewtonianStress(context, strain_rate, kmstress);
425d3b25f3aSJames Wright     KMUnpack(kmstress, stress);
426d3b25f3aSJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
427d3b25f3aSJames Wright 
428d3b25f3aSJames Wright     StateConservative F_inviscid[3];
429d3b25f3aSJames Wright     FluxInviscid(context, s, F_inviscid);
430d3b25f3aSJames Wright 
431c5740391SJames Wright     CeedScalar Flux[5];
43278e8b7daSJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, normal, Flux);
433d3b25f3aSJames Wright 
434c5740391SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
4358085925cSJames Wright 
4364b96a86bSJames Wright     if (is_implicit) {
437ade49511SJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
438ade49511SJames Wright       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
4398085925cSJames Wright     }
4404b96a86bSJames Wright   }
4418085925cSJames Wright   return 0;
4428085925cSJames Wright }
4438085925cSJames Wright 
4442b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4458fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
446d4559bbeSJames Wright }
447d4559bbeSJames Wright 
4482b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4498fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
450d4559bbeSJames Wright }
451d4559bbeSJames Wright 
4529b103f75SJames Wright CEED_QFUNCTION(BoundaryIntegral_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4539b103f75SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_ENTROPY);
4549b103f75SJames Wright }
4559b103f75SJames Wright 
456d1b9ef12SLeila Ghaffari // *****************************************************************************
45768ae065aSJames Wright // Jacobian for "set nothing" boundary integral
458d1b9ef12SLeila Ghaffari // *****************************************************************************
4592b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
4608fff8293SJames Wright                                                     StateVariable state_var) {
4613d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
46287bd45e7SJames Wright   const CeedScalar(*Grad_dq)        = in[1];
463ade49511SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
464c1484fadSKenneth E. Jansen   const CeedScalar(*jac_data_sur)   = in[4];
46568ae065aSJames Wright   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
46668ae065aSJames Wright 
46768ae065aSJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
468ade49511SJames Wright   const bool                     is_implicit = context->is_implicit;
46968ae065aSJames Wright 
4703d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
47178e8b7daSJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
47278e8b7daSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
473ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
47468ae065aSJames Wright 
475edcfef1bSKenneth E. Jansen     CeedScalar qi[5], kmstress[6], dqi[5];
476ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
477ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
47841e73928SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
4793934e2b1SJames Wright 
480edcfef1bSKenneth E. Jansen     State s  = StateFromQ(context, qi, state_var);
481edcfef1bSKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
48268ae065aSJames Wright 
48368ae065aSJames Wright     State grad_ds[3];
484edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
48568ae065aSJames Wright 
48668ae065aSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
48740a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
48868ae065aSJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
48968ae065aSJames Wright     KMUnpack(dkmstress, dstress);
49068ae065aSJames Wright     KMUnpack(kmstress, stress);
49168ae065aSJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
49268ae065aSJames Wright 
49368ae065aSJames Wright     StateConservative dF_inviscid[3];
49468ae065aSJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
49568ae065aSJames Wright 
496c5740391SJames Wright     CeedScalar dFlux[5];
49778e8b7daSJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, normal, dFlux);
49868ae065aSJames Wright 
499c5740391SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
500512c8ec7SJames Wright   }
50168ae065aSJames Wright   return 0;
50268ae065aSJames Wright }
50368ae065aSJames Wright 
5042b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5058fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
506d4559bbeSJames Wright }
507d4559bbeSJames Wright 
5082b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5098fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
510d4559bbeSJames Wright }
5119b103f75SJames Wright 
5129b103f75SJames Wright CEED_QFUNCTION(BoundaryIntegral_Jacobian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5139b103f75SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_ENTROPY);
5149b103f75SJames Wright }
51536038bbcSJames Wright 
5168561fee2SJames Wright // @brief Volume integral for RHS of divergence of diffusive flux direct projection
51736038bbcSJames Wright CEED_QFUNCTION_HELPER int DivDiffusiveFluxVolumeRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
51836038bbcSJames Wright                                                        StateVariable state_var) {
51936038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[0];
52036038bbcSJames Wright   const CeedScalar(*Grad_q)          = in[1];
52136038bbcSJames Wright   const CeedScalar(*q_data)          = in[2];
52236038bbcSJames Wright   CeedScalar(*Grad_v)[4][CEED_Q_VLA] = (CeedScalar(*)[4][CEED_Q_VLA])out[0];
52336038bbcSJames Wright 
52436038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
52536038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
52636038bbcSJames Wright 
52736038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
52836038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
52936038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
53036038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3];
53136038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
53236038bbcSJames Wright 
53336038bbcSJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
53436038bbcSJames Wright     {  // Get stress and Fe
53536038bbcSJames Wright       State      grad_s[3];
53636038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
53736038bbcSJames Wright 
53836038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
53936038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
54036038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
54136038bbcSJames Wright       KMUnpack(kmstress, stress);
54236038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
54336038bbcSJames Wright     }
54436038bbcSJames Wright 
54536038bbcSJames Wright     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
54636038bbcSJames Wright 
54736038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
54836038bbcSJames Wright       for (CeedInt k = 0; k < 3; k++) {
54936038bbcSJames Wright         Grad_v[k][j - 1][i] = -wdetJ * Dot3(dXdx[k], Fdiff[j]);
55036038bbcSJames Wright       }
55136038bbcSJames Wright     }
55236038bbcSJames Wright   }
55336038bbcSJames Wright   return 0;
55436038bbcSJames Wright }
55536038bbcSJames Wright 
55636038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
55736038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
55836038bbcSJames Wright }
55936038bbcSJames Wright 
56036038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
56136038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
56236038bbcSJames Wright }
56336038bbcSJames Wright 
56436038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
56536038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
56636038bbcSJames Wright }
56736038bbcSJames Wright 
5688561fee2SJames Wright // @brief Boundary integral for RHS of divergence of diffusive flux direct projection
56936038bbcSJames Wright CEED_QFUNCTION_HELPER int DivDiffusiveFluxBoundaryRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
57036038bbcSJames Wright                                                          StateVariable state_var) {
57136038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
57236038bbcSJames Wright   const CeedScalar(*Grad_q)        = in[1];
57336038bbcSJames Wright   const CeedScalar(*q_data)        = in[2];
57436038bbcSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
57536038bbcSJames Wright 
57636038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
57736038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
57836038bbcSJames Wright 
57936038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
58036038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
58136038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
58236038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3], normal[3];
58336038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5];
58436038bbcSJames Wright 
58536038bbcSJames Wright     QdataBoundaryGradientUnpack_3D(Q, i, q_data, &wdetJ, dXdx, normal);
58636038bbcSJames Wright     {  // Get stress and Fe
58736038bbcSJames Wright       State      grad_s[3];
58836038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
58936038bbcSJames Wright 
59036038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
59136038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
59236038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
59336038bbcSJames Wright       KMUnpack(kmstress, stress);
59436038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
59536038bbcSJames Wright     }
59636038bbcSJames Wright 
59736038bbcSJames Wright     FluxTotal_Boundary(ZeroInviscidFluxes, stress, Fe, normal, Fdiff);
59836038bbcSJames Wright 
59936038bbcSJames Wright     // Continuity has no diffusive flux, therefore skip
60036038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) v[j - 1][i] = wdetJ * Fdiff[j];
60136038bbcSJames Wright   }
60236038bbcSJames Wright   return 0;
60336038bbcSJames Wright }
60436038bbcSJames Wright 
60536038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60636038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
60736038bbcSJames Wright }
60836038bbcSJames Wright 
60936038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
61036038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
61136038bbcSJames Wright }
61236038bbcSJames Wright 
61336038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
61436038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
61536038bbcSJames Wright }
61636038bbcSJames Wright 
6178561fee2SJames Wright // @brief Integral for RHS of diffusive flux indirect projection
61836038bbcSJames Wright CEED_QFUNCTION_HELPER int DiffusiveFluxRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
61936038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
62036038bbcSJames Wright   const CeedScalar(*Grad_q)        = in[1];
62136038bbcSJames Wright   const CeedScalar(*q_data)        = in[2];
62236038bbcSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
62336038bbcSJames Wright 
62436038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
62536038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
62636038bbcSJames Wright 
62736038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
62836038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
62936038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
63036038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3];
63136038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
63236038bbcSJames Wright 
63336038bbcSJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
63436038bbcSJames Wright     {  // Get stress and Fe
63536038bbcSJames Wright       State      grad_s[3];
63636038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
63736038bbcSJames Wright 
63836038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
63936038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
64036038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
64136038bbcSJames Wright       KMUnpack(kmstress, stress);
64236038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
64336038bbcSJames Wright     }
64436038bbcSJames Wright 
64536038bbcSJames Wright     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
64636038bbcSJames Wright 
64736038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
64836038bbcSJames Wright       for (CeedInt k = 0; k < 3; k++) {
64936038bbcSJames Wright         v[(j - 1) * 3 + k][i] = wdetJ * Fdiff[j][k];
65036038bbcSJames Wright       }
65136038bbcSJames Wright     }
65236038bbcSJames Wright   }
65336038bbcSJames Wright   return 0;
65436038bbcSJames Wright }
65536038bbcSJames Wright 
65636038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
65736038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
65836038bbcSJames Wright }
65936038bbcSJames Wright 
66036038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
66136038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
66236038bbcSJames Wright }
66336038bbcSJames Wright 
66436038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
66536038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
66636038bbcSJames Wright }
667