xref: /honee/qfunctions/newtonian.h (revision 8561fee26e1c8104caa6d917d7ecea939d705f29)
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 
5465dee3d2SJames Wright CEED_QFUNCTION_HELPER void MassFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
5565dee3d2SJames Wright                                                   StateVariable state_var) {
5665dee3d2SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
5765dee3d2SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[1];
5865dee3d2SJames Wright   const CeedScalar(*q_data)            = in[2];
5965dee3d2SJames Wright   CeedScalar(*v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
6065dee3d2SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]   = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
6165dee3d2SJames Wright 
6265dee3d2SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
6365dee3d2SJames Wright 
6465dee3d2SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
6565dee3d2SJames Wright     const CeedScalar qi[5]     = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
6665dee3d2SJames 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]};
6765dee3d2SJames Wright     const State      s         = StateFromQ(context, qi, state_var);
6865dee3d2SJames Wright     const State      s_dot     = StateFromQ(context, qi_dot, state_var);
6965dee3d2SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
7065dee3d2SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
7165dee3d2SJames Wright 
7265dee3d2SJames Wright     // Standard mass matrix term
7365dee3d2SJames Wright     for (CeedInt f = 0; f < 5; f++) {
7465dee3d2SJames Wright       v[f][i] = wdetJ * qi_dot[f];
7565dee3d2SJames Wright     }
7665dee3d2SJames Wright 
7765dee3d2SJames Wright     // Stabilization method: none (Galerkin), SU, or SUPG
7865dee3d2SJames Wright     State      grad_s[3] = {{{0.}}};
798c85b835SJames Wright     CeedScalar Tau_d[3], stab[5][3], body_force[5] = {0.}, divFdiff[5] = {0.}, U_dot[5];
8065dee3d2SJames Wright     UnpackState_U(s_dot.U, U_dot);
8165dee3d2SJames Wright     Tau_diagPrim(context, s, dXdx, context->dt, Tau_d);
828c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff, stab);
8365dee3d2SJames Wright 
8465dee3d2SJames Wright     // Stabilized mass term
8565dee3d2SJames Wright     for (CeedInt j = 0; j < 5; j++) {
8665dee3d2SJames Wright       for (CeedInt k = 0; k < 3; k++) {
8765dee3d2SJames 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]);
8865dee3d2SJames Wright       }
8965dee3d2SJames Wright     }
9065dee3d2SJames Wright   }
9165dee3d2SJames Wright }
9265dee3d2SJames Wright 
9365dee3d2SJames Wright CEED_QFUNCTION(MassFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
9465dee3d2SJames Wright   MassFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
9565dee3d2SJames Wright   return 0;
9665dee3d2SJames Wright }
9765dee3d2SJames Wright 
98cbe60e31SLeila Ghaffari // *****************************************************************************
9904e40bb6SJeremy L Thompson // This QFunction implements the following formulation of Navier-Stokes with explicit time stepping method
1003a8779fbSJames Wright //
10104e40bb6SJeremy L Thompson // This is 3D compressible Navier-Stokes in conservation form with state variables of density, momentum density, and total energy density.
1023a8779fbSJames Wright //
1033a8779fbSJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
1043a8779fbSJames Wright //   rho - Mass Density
1053a8779fbSJames Wright //   Ui  - Momentum Density,      Ui = rho ui
1063a8779fbSJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
1073a8779fbSJames Wright //
1083a8779fbSJames Wright // Navier-Stokes Equations:
1093a8779fbSJames Wright //   drho/dt + div( U )                               = 0
1103a8779fbSJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
1113a8779fbSJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
1123a8779fbSJames Wright //
1133a8779fbSJames Wright // Viscous Stress:
1143a8779fbSJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
1153a8779fbSJames Wright //
1163a8779fbSJames Wright // Thermal Stress:
1173a8779fbSJames Wright //   Fe = u Fu + k grad( T )
118bb8a0c61SJames Wright // Equation of State
1193a8779fbSJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
1203a8779fbSJames Wright //
1213a8779fbSJames Wright // Stabilization:
1223a8779fbSJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
1233a8779fbSJames Wright //     f1 = rho  sqrt(ui uj gij)
1243a8779fbSJames Wright //     gij = dXi/dX * dXi/dX
1253a8779fbSJames Wright //     TauC = Cc f1 / (8 gii)
1263a8779fbSJames Wright //     TauM = min( 1 , 1 / f1 )
1273a8779fbSJames Wright //     TauE = TauM / (Ce cv)
1283a8779fbSJames Wright //
1293a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
1303a8779fbSJames Wright //
1313a8779fbSJames Wright // Constants:
1323a8779fbSJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
1333a8779fbSJames Wright //   mu              ,  Dynamic viscosity
1343a8779fbSJames Wright //   k               ,  Thermal conductivity
1353a8779fbSJames Wright //   cv              ,  Specific heat, constant volume
1363a8779fbSJames Wright //   cp              ,  Specific heat, constant pressure
1373a8779fbSJames Wright //   g               ,  Gravity
1383a8779fbSJames Wright //   gamma  = cp / cv,  Specific heat ratio
1393a8779fbSJames Wright //
14004e40bb6SJeremy 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
14104e40bb6SJeremy L Thompson // gradu )
1423a8779fbSJames Wright // *****************************************************************************
1432b916ea7SJeremy L Thompson CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
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];
1483d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
1493d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
1503a8779fbSJames Wright 
1513a8779fbSJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
152bb8a0c61SJames Wright   const CeedScalar        *g       = context->g;
153bb8a0c61SJames Wright   const CeedScalar         dt      = context->dt;
1540a32a5aaSJames Wright   const CeedScalar         P0      = 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]);
1880a32a5aaSJames Wright       CeedScalar       damp_state[5] = {s.Y.pressure - P0, 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 
193d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
1948c85b835SJames Wright     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, zeroFlux[5] = {0.};
195d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
1968c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, zeroFlux, stab);
1973a8779fbSJames Wright 
1982b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
1992b916ea7SJeremy 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]);
2002b916ea7SJeremy L Thompson     }
201b193fadcSJames Wright   }
2023a8779fbSJames Wright   return 0;
2033a8779fbSJames Wright }
2043a8779fbSJames Wright 
2053a8779fbSJames Wright // *****************************************************************************
20604e40bb6SJeremy L Thompson // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
2073a8779fbSJames Wright //
2083a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
2093a8779fbSJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
21004e40bb6SJeremy L Thompson //                                       (diffusive terms will be added later)
2113a8779fbSJames Wright // *****************************************************************************
2128fff8293SJames Wright CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
2138c85b835SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
2148c85b835SJames Wright 
2153d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[0];
21687bd45e7SJames Wright   const CeedScalar(*Grad_q)               = in[1];
2173d65b166SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])in[2];
218ade49511SJames Wright   const CeedScalar(*q_data)               = in[3];
2193d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[4];
2208c85b835SJames Wright   const CeedScalar(*divFdiff)[CEED_Q_VLA] = context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE ? (const CeedScalar(*)[CEED_Q_VLA])in[5] : NULL;
2213d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]              = (CeedScalar(*)[CEED_Q_VLA])out[0];
2223d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]      = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
223ade49511SJames Wright   CeedScalar(*jac_data)                   = out[2];
2243d65b166SJames Wright 
225bb8a0c61SJames Wright   const CeedScalar *g  = context->g;
226bb8a0c61SJames Wright   const CeedScalar  dt = context->dt;
227fcb2c22aSJames Wright   const CeedScalar  P0 = context->idl_pressure;
2283a8779fbSJames Wright 
2293d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2303d65b166SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
231c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
232edcfef1bSKenneth E. Jansen     const State      s      = StateFromQ(context, qi, state_var);
233c1a52365SJed Brown 
234ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
235ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
236c1a52365SJed Brown     State grad_s[3];
237edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
238c1a52365SJed Brown 
239c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
24040a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
241c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
242c1a52365SJed Brown     KMUnpack(kmstress, stress);
243c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
244c1a52365SJed Brown 
245c1a52365SJed Brown     StateConservative F_inviscid[3];
246c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
247c1a52365SJed Brown 
248c1a52365SJed Brown     // Total flux
249c1a52365SJed Brown     CeedScalar Flux[5][3];
250d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
251c1a52365SJed Brown 
2527523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
2537523f6aaSJames Wright       for (CeedInt k = 0; k < 3; k++) {
2547523f6aaSJames 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]);
2553d65b166SJames Wright       }
2562b916ea7SJeremy L Thompson     }
257c1a52365SJed Brown 
25860dbb574SKenneth 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)};
2593a8779fbSJames Wright 
260d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
261edcfef1bSKenneth E. Jansen     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5];
26276555becSJames Wright     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
263edcfef1bSKenneth E. Jansen     State s_dot = StateFromQ_fwd(context, s, qi_dot, state_var);
26476555becSJames Wright     UnpackState_U(s_dot.U, U_dot);
26576555becSJames Wright 
2662b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
267e7754af5SKenneth E. Jansen     if (context->idl_enable) {
26894a7b3d2SKenneth E. Jansen       const CeedScalar sigma = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
26994a7b3d2SKenneth E. Jansen       StoredValuesPack(Q, i, 14, 1, &sigma, jac_data);
270e7754af5SKenneth E. Jansen       CeedScalar damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
27194a7b3d2SKenneth E. Jansen       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
272e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
273e7754af5SKenneth E. Jansen     }
274e7754af5SKenneth E. Jansen 
2758c85b835SJames Wright     CeedScalar divFdiff_i[5] = {0.};
2768c85b835SJames Wright     if (context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE) {
2778c85b835SJames Wright       for (int j = 1; j < 5; j++) divFdiff_i[j] = divFdiff[j - 1][i];
2788c85b835SJames Wright     }
279d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
2808c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff_i, stab);
2813a8779fbSJames Wright 
2822b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
2833d65b166SJames Wright       for (CeedInt k = 0; k < 3; k++) {
2843d65b166SJames 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]);
2853d65b166SJames Wright       }
2862b916ea7SJeremy L Thompson     }
287ade49511SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
288ade49511SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
289ade49511SJames Wright     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
290b193fadcSJames Wright   }
2913a8779fbSJames Wright   return 0;
2923a8779fbSJames Wright }
293f0b65372SJed Brown 
2942b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2958fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
29676555becSJames Wright }
29776555becSJames Wright 
2982b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2998fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
30076555becSJames Wright }
30176555becSJames Wright 
3029b103f75SJames Wright CEED_QFUNCTION(IFunction_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3039b103f75SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
3049b103f75SJames Wright }
3059b103f75SJames Wright 
306cbe60e31SLeila Ghaffari // *****************************************************************************
30704e40bb6SJeremy L Thompson // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
308cbe60e31SLeila Ghaffari // *****************************************************************************
3098fff8293SJames Wright CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3103d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[0];
31187bd45e7SJames Wright   const CeedScalar(*Grad_dq)         = in[1];
312ade49511SJames Wright   const CeedScalar(*q_data)          = in[2];
31394a7b3d2SKenneth E. Jansen   const CeedScalar(*jac_data)        = in[3];
3143d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
3153d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
3163d65b166SJames Wright 
317f0b65372SJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
318f0b65372SJed Brown   const CeedScalar        *g       = context->g;
319f0b65372SJed Brown 
3203d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
321ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
322ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
323f0b65372SJed Brown 
3248789e95fSJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
325ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
326ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
327ade49511SJames Wright     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
328edcfef1bSKenneth E. Jansen     State s = StateFromQ(context, qi, state_var);
329f0b65372SJed Brown 
330edcfef1bSKenneth E. Jansen     CeedScalar dqi[5];
33176555becSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
332edcfef1bSKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
333f0b65372SJed Brown 
334f0b65372SJed Brown     State grad_ds[3];
335edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
336f0b65372SJed Brown 
337f0b65372SJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
33840a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
339f0b65372SJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
340f0b65372SJed Brown     KMUnpack(dkmstress, dstress);
341f0b65372SJed Brown     KMUnpack(kmstress, stress);
342f0b65372SJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
343f0b65372SJed Brown 
344f0b65372SJed Brown     StateConservative dF_inviscid[3];
345f0b65372SJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
346f0b65372SJed Brown 
347f0b65372SJed Brown     // Total flux
348f0b65372SJed Brown     CeedScalar dFlux[5][3];
349d1b9ef12SLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
350f0b65372SJed Brown 
35122387d3aSJames Wright     for (int j = 0; j < 5; j++) {
35222387d3aSJames 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]);
3532b916ea7SJeremy L Thompson     }
354f0b65372SJed Brown 
35560dbb574SKenneth 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)};
35676555becSJames Wright     CeedScalar       dU[5]          = {0.};
35776555becSJames Wright     UnpackState_U(ds.U, dU);
3582b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
359f0b65372SJed Brown 
360e7754af5SKenneth E. Jansen     if (context->idl_enable) {
36194a7b3d2SKenneth E. Jansen       const CeedScalar sigma         = jac_data[14 * Q + i];
362e7754af5SKenneth E. Jansen       CeedScalar       damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
363e7754af5SKenneth E. Jansen       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
36494a7b3d2SKenneth E. Jansen       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
365e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
366e7754af5SKenneth E. Jansen     }
367e7754af5SKenneth E. Jansen 
368d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
369d1b9ef12SLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
370d1b9ef12SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
3718c85b835SJames Wright     const CeedScalar zeroFlux[5] = {0.};
3728c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, zeroFlux, dstab);
373d1b9ef12SLeila Ghaffari 
3742b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) {
3752b916ea7SJeremy 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]);
3762b916ea7SJeremy L Thompson     }
377b193fadcSJames Wright   }
378f0b65372SJed Brown   return 0;
379f0b65372SJed Brown }
3808085925cSJames Wright 
3812b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3828fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
38376555becSJames Wright }
38476555becSJames Wright 
3852b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3868fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
38776555becSJames Wright }
38876555becSJames Wright 
3899b103f75SJames Wright CEED_QFUNCTION(IJacobian_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3909b103f75SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
3919b103f75SJames Wright }
3929b103f75SJames Wright 
393d1b9ef12SLeila Ghaffari // *****************************************************************************
3948085925cSJames Wright // Compute boundary integral (ie. for strongly set inflows)
395d1b9ef12SLeila Ghaffari // *****************************************************************************
3968fff8293SJames Wright CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3974b96a86bSJames Wright   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
3983d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]       = (const CeedScalar(*)[CEED_Q_VLA])in[0];
39987bd45e7SJames Wright   const CeedScalar(*Grad_q)              = in[1];
400ade49511SJames Wright   const CeedScalar(*q_data_sur)          = in[2];
4013d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]             = (CeedScalar(*)[CEED_Q_VLA])out[0];
4024b96a86bSJames Wright   CeedScalar(*jac_data_sur)              = context->is_implicit ? out[1] : NULL;
4038085925cSJames Wright 
404d3b25f3aSJames Wright   const bool is_implicit = context->is_implicit;
4058085925cSJames Wright 
4062b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
40741e73928SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
408edcfef1bSKenneth E. Jansen     State            s     = StateFromQ(context, qi, state_var);
4098085925cSJames Wright 
41078e8b7daSJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
41178e8b7daSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
412ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
4138085925cSJames Wright 
414d3b25f3aSJames Wright     State grad_s[3];
415edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
4168085925cSJames Wright 
417d3b25f3aSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
41840a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
419d3b25f3aSJames Wright     NewtonianStress(context, strain_rate, kmstress);
420d3b25f3aSJames Wright     KMUnpack(kmstress, stress);
421d3b25f3aSJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
422d3b25f3aSJames Wright 
423d3b25f3aSJames Wright     StateConservative F_inviscid[3];
424d3b25f3aSJames Wright     FluxInviscid(context, s, F_inviscid);
425d3b25f3aSJames Wright 
426c5740391SJames Wright     CeedScalar Flux[5];
42778e8b7daSJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, normal, Flux);
428d3b25f3aSJames Wright 
429c5740391SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
4308085925cSJames Wright 
4314b96a86bSJames Wright     if (is_implicit) {
432ade49511SJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
433ade49511SJames Wright       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
4348085925cSJames Wright     }
4354b96a86bSJames Wright   }
4368085925cSJames Wright   return 0;
4378085925cSJames Wright }
4388085925cSJames Wright 
4392b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4408fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
441d4559bbeSJames Wright }
442d4559bbeSJames Wright 
4432b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4448fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
445d4559bbeSJames Wright }
446d4559bbeSJames Wright 
4479b103f75SJames Wright CEED_QFUNCTION(BoundaryIntegral_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4489b103f75SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_ENTROPY);
4499b103f75SJames Wright }
4509b103f75SJames Wright 
451d1b9ef12SLeila Ghaffari // *****************************************************************************
45268ae065aSJames Wright // Jacobian for "set nothing" boundary integral
453d1b9ef12SLeila Ghaffari // *****************************************************************************
4542b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
4558fff8293SJames Wright                                                     StateVariable state_var) {
4563d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
45787bd45e7SJames Wright   const CeedScalar(*Grad_dq)        = in[1];
458ade49511SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
459c1484fadSKenneth E. Jansen   const CeedScalar(*jac_data_sur)   = in[4];
46068ae065aSJames Wright   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
46168ae065aSJames Wright 
46268ae065aSJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
463ade49511SJames Wright   const bool                     is_implicit = context->is_implicit;
46468ae065aSJames Wright 
4653d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
46678e8b7daSJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
46778e8b7daSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
468ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
46968ae065aSJames Wright 
470edcfef1bSKenneth E. Jansen     CeedScalar qi[5], kmstress[6], dqi[5];
471ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
472ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
47341e73928SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
4743934e2b1SJames Wright 
475edcfef1bSKenneth E. Jansen     State s  = StateFromQ(context, qi, state_var);
476edcfef1bSKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
47768ae065aSJames Wright 
47868ae065aSJames Wright     State grad_ds[3];
479edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
48068ae065aSJames Wright 
48168ae065aSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
48240a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
48368ae065aSJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
48468ae065aSJames Wright     KMUnpack(dkmstress, dstress);
48568ae065aSJames Wright     KMUnpack(kmstress, stress);
48668ae065aSJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
48768ae065aSJames Wright 
48868ae065aSJames Wright     StateConservative dF_inviscid[3];
48968ae065aSJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
49068ae065aSJames Wright 
491c5740391SJames Wright     CeedScalar dFlux[5];
49278e8b7daSJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, normal, dFlux);
49368ae065aSJames Wright 
494c5740391SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
495512c8ec7SJames Wright   }
49668ae065aSJames Wright   return 0;
49768ae065aSJames Wright }
49868ae065aSJames Wright 
4992b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5008fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
501d4559bbeSJames Wright }
502d4559bbeSJames Wright 
5032b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5048fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
505d4559bbeSJames Wright }
5069b103f75SJames Wright 
5079b103f75SJames Wright CEED_QFUNCTION(BoundaryIntegral_Jacobian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5089b103f75SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_ENTROPY);
5099b103f75SJames Wright }
51036038bbcSJames Wright 
511*8561fee2SJames Wright // @brief Volume integral for RHS of divergence of diffusive flux direct projection
51236038bbcSJames Wright CEED_QFUNCTION_HELPER int DivDiffusiveFluxVolumeRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
51336038bbcSJames Wright                                                        StateVariable state_var) {
51436038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[0];
51536038bbcSJames Wright   const CeedScalar(*Grad_q)          = in[1];
51636038bbcSJames Wright   const CeedScalar(*q_data)          = in[2];
51736038bbcSJames Wright   CeedScalar(*Grad_v)[4][CEED_Q_VLA] = (CeedScalar(*)[4][CEED_Q_VLA])out[0];
51836038bbcSJames Wright 
51936038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
52036038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
52136038bbcSJames Wright 
52236038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
52336038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
52436038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
52536038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3];
52636038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
52736038bbcSJames Wright 
52836038bbcSJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
52936038bbcSJames Wright     {  // Get stress and Fe
53036038bbcSJames Wright       State      grad_s[3];
53136038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
53236038bbcSJames Wright 
53336038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
53436038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
53536038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
53636038bbcSJames Wright       KMUnpack(kmstress, stress);
53736038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
53836038bbcSJames Wright     }
53936038bbcSJames Wright 
54036038bbcSJames Wright     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
54136038bbcSJames Wright 
54236038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
54336038bbcSJames Wright       for (CeedInt k = 0; k < 3; k++) {
54436038bbcSJames Wright         Grad_v[k][j - 1][i] = -wdetJ * Dot3(dXdx[k], Fdiff[j]);
54536038bbcSJames Wright       }
54636038bbcSJames Wright     }
54736038bbcSJames Wright   }
54836038bbcSJames Wright   return 0;
54936038bbcSJames Wright }
55036038bbcSJames Wright 
55136038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
55236038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
55336038bbcSJames Wright }
55436038bbcSJames Wright 
55536038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
55636038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
55736038bbcSJames Wright }
55836038bbcSJames Wright 
55936038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
56036038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
56136038bbcSJames Wright }
56236038bbcSJames Wright 
563*8561fee2SJames Wright // @brief Boundary integral for RHS of divergence of diffusive flux direct projection
56436038bbcSJames Wright CEED_QFUNCTION_HELPER int DivDiffusiveFluxBoundaryRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
56536038bbcSJames Wright                                                          StateVariable state_var) {
56636038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
56736038bbcSJames Wright   const CeedScalar(*Grad_q)        = in[1];
56836038bbcSJames Wright   const CeedScalar(*q_data)        = in[2];
56936038bbcSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
57036038bbcSJames Wright 
57136038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
57236038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
57336038bbcSJames Wright 
57436038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
57536038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
57636038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
57736038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3], normal[3];
57836038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5];
57936038bbcSJames Wright 
58036038bbcSJames Wright     QdataBoundaryGradientUnpack_3D(Q, i, q_data, &wdetJ, dXdx, normal);
58136038bbcSJames Wright     {  // Get stress and Fe
58236038bbcSJames Wright       State      grad_s[3];
58336038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
58436038bbcSJames Wright 
58536038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
58636038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
58736038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
58836038bbcSJames Wright       KMUnpack(kmstress, stress);
58936038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
59036038bbcSJames Wright     }
59136038bbcSJames Wright 
59236038bbcSJames Wright     FluxTotal_Boundary(ZeroInviscidFluxes, stress, Fe, normal, Fdiff);
59336038bbcSJames Wright 
59436038bbcSJames Wright     // Continuity has no diffusive flux, therefore skip
59536038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) v[j - 1][i] = wdetJ * Fdiff[j];
59636038bbcSJames Wright   }
59736038bbcSJames Wright   return 0;
59836038bbcSJames Wright }
59936038bbcSJames Wright 
60036038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60136038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
60236038bbcSJames Wright }
60336038bbcSJames Wright 
60436038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60536038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
60636038bbcSJames Wright }
60736038bbcSJames Wright 
60836038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60936038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
61036038bbcSJames Wright }
61136038bbcSJames Wright 
612*8561fee2SJames Wright // @brief Integral for RHS of diffusive flux indirect projection
61336038bbcSJames Wright CEED_QFUNCTION_HELPER int DiffusiveFluxRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
61436038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
61536038bbcSJames Wright   const CeedScalar(*Grad_q)        = in[1];
61636038bbcSJames Wright   const CeedScalar(*q_data)        = in[2];
61736038bbcSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
61836038bbcSJames Wright 
61936038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
62036038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
62136038bbcSJames Wright 
62236038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
62336038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
62436038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
62536038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3];
62636038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
62736038bbcSJames Wright 
62836038bbcSJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
62936038bbcSJames Wright     {  // Get stress and Fe
63036038bbcSJames Wright       State      grad_s[3];
63136038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
63236038bbcSJames Wright 
63336038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
63436038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
63536038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
63636038bbcSJames Wright       KMUnpack(kmstress, stress);
63736038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
63836038bbcSJames Wright     }
63936038bbcSJames Wright 
64036038bbcSJames Wright     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
64136038bbcSJames Wright 
64236038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
64336038bbcSJames Wright       for (CeedInt k = 0; k < 3; k++) {
64436038bbcSJames Wright         v[(j - 1) * 3 + k][i] = wdetJ * Fdiff[j][k];
64536038bbcSJames Wright       }
64636038bbcSJames Wright     }
64736038bbcSJames Wright   }
64836038bbcSJames Wright   return 0;
64936038bbcSJames Wright }
65036038bbcSJames Wright 
65136038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
65236038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
65336038bbcSJames Wright }
65436038bbcSJames Wright 
65536038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
65636038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
65736038bbcSJames Wright }
65836038bbcSJames Wright 
65936038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
66036038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
66136038bbcSJames Wright }
662