xref: /honee/qfunctions/newtonian.h (revision e57b52db703e09f6f8573122ef6053adcc581a73)
1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
33a8779fbSJames Wright 
43a8779fbSJames Wright /// @file
5ea615d4cSJames Wright /// Newtonian fluids operator for HONEE
63e17a7a1SJames Wright #include <ceed/types.h>
72b916ea7SJeremy L Thompson 
8475b2820SJames Wright #include "newtonian_state.h"
9d0cce58aSJeremy L Thompson #include "newtonian_types.h"
10d1b9ef12SLeila Ghaffari #include "stabilization.h"
11d0cce58aSJeremy L Thompson #include "utils.h"
12bb8a0c61SJames Wright 
13bb8a0c61SJames Wright // *****************************************************************************
143a8779fbSJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
153a8779fbSJames Wright // *****************************************************************************
168fff8293SJames Wright CEED_QFUNCTION_HELPER int ICsNewtonianIG(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
173a8779fbSJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
183a8779fbSJames Wright 
19bb8a0c61SJames Wright   const SetupContext context = (SetupContext)ctx;
20bb8a0c61SJames Wright 
212b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
22a541e550SJames Wright     CeedScalar q[5];
23edcfef1bSKenneth E. Jansen     State      s = StateFromPrimitive(&context->gas, context->reference);
248fff8293SJames Wright     StateToQ(&context->gas, s, q, state_var);
252b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
26b193fadcSJames Wright   }
273a8779fbSJames Wright   return 0;
283a8779fbSJames Wright }
293a8779fbSJames Wright 
309b103f75SJames Wright CEED_QFUNCTION(ICsNewtonianIG_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
319b103f75SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
329b103f75SJames Wright }
339b103f75SJames Wright 
342b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsNewtonianIG_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
358fff8293SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_PRIMITIVE);
36b8fb7609SAdeleke O. Bankole }
379b103f75SJames Wright 
389b103f75SJames Wright CEED_QFUNCTION(ICsNewtonianIG_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
399b103f75SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_ENTROPY);
40cbe60e31SLeila Ghaffari }
41cbe60e31SLeila Ghaffari 
4297cfd714SJames Wright CEED_QFUNCTION_HELPER int MassFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
4365dee3d2SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
4465dee3d2SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[1];
4565dee3d2SJames Wright   const CeedScalar(*q_data)            = in[2];
4665dee3d2SJames Wright   CeedScalar(*v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
4765dee3d2SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]   = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
4865dee3d2SJames Wright 
4965dee3d2SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
5065dee3d2SJames Wright 
5165dee3d2SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
5265dee3d2SJames Wright     const CeedScalar qi[5]     = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
5365dee3d2SJames Wright     const CeedScalar qi_dot[5] = {q_dot[0][i], q_dot[1][i], q_dot[2][i], q_dot[3][i], q_dot[4][i]};
5465dee3d2SJames Wright     const State      s         = StateFromQ(context, qi, state_var);
5565dee3d2SJames Wright     const State      s_dot     = StateFromQ(context, qi_dot, state_var);
5665dee3d2SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
5765dee3d2SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
5865dee3d2SJames Wright 
5965dee3d2SJames Wright     // Standard mass matrix term
6065dee3d2SJames Wright     for (CeedInt f = 0; f < 5; f++) {
6165dee3d2SJames Wright       v[f][i] = wdetJ * qi_dot[f];
6265dee3d2SJames Wright     }
6365dee3d2SJames Wright 
6465dee3d2SJames Wright     // Stabilization method: none (Galerkin), SU, or SUPG
6565dee3d2SJames Wright     State      grad_s[3] = {{{0.}}};
668c85b835SJames Wright     CeedScalar Tau_d[3], stab[5][3], body_force[5] = {0.}, divFdiff[5] = {0.}, U_dot[5];
6765dee3d2SJames Wright     UnpackState_U(s_dot.U, U_dot);
6865dee3d2SJames Wright     Tau_diagPrim(context, s, dXdx, context->dt, Tau_d);
698c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff, stab);
7065dee3d2SJames Wright 
7165dee3d2SJames Wright     // Stabilized mass term
7265dee3d2SJames Wright     for (CeedInt j = 0; j < 5; j++) {
7365dee3d2SJames Wright       for (CeedInt k = 0; k < 3; k++) {
7465dee3d2SJames Wright         Grad_v[k][j][i] = wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]);
7565dee3d2SJames Wright       }
7665dee3d2SJames Wright     }
7765dee3d2SJames Wright   }
7897cfd714SJames Wright   return 0;
7965dee3d2SJames Wright }
8065dee3d2SJames Wright 
8165dee3d2SJames Wright CEED_QFUNCTION(MassFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
8297cfd714SJames Wright   return MassFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
8365dee3d2SJames Wright }
8465dee3d2SJames Wright 
85*e57b52dbSJames Wright // @brief Computes the residual created by IDL
8630b5892fSJames Wright CEED_QFUNCTION_HELPER void InternalDampingLayer_Residual(const NewtonianIdealGasContext context, const State s, const CeedScalar sigma,
8730b5892fSJames Wright                                                          CeedScalar damp_Y[5], CeedScalar damp_residual[5]) {
8830b5892fSJames Wright   ScaleN(damp_Y, sigma, 5);
8930b5892fSJames Wright   State damp_s = StateFromY_fwd(context, s, damp_Y);
9030b5892fSJames Wright 
9130b5892fSJames Wright   CeedScalar U[5];
9230b5892fSJames Wright   UnpackState_U(damp_s.U, U);
9330b5892fSJames Wright   for (int i = 0; i < 5; i++) damp_residual[i] += U[i];
9430b5892fSJames Wright }
9530b5892fSJames Wright 
96*e57b52dbSJames Wright /**
97*e57b52dbSJames Wright   @brief IFunction integrand for Internal Damping Layer
98*e57b52dbSJames Wright 
99*e57b52dbSJames Wright   `location` refers to whatever scalar distance is desired for IDL to ramp from.
100*e57b52dbSJames Wright   See `LinearRampCoefficient()` for details on the `amplitude`, `length`, `start`, and `location` arguments.
101*e57b52dbSJames Wright 
102*e57b52dbSJames Wright   @param[in]    s         Solution `State`
103*e57b52dbSJames Wright   @param[in]    context   Newtonian context
104*e57b52dbSJames Wright   @param[in]    amplitude Amplitude of the IDL ramp
105*e57b52dbSJames Wright   @param[in]    length    Length of the IDL ramp
106*e57b52dbSJames Wright   @param[in]    start     Start of the IDL ramp
107*e57b52dbSJames Wright   @param[in]    location  Quadrature point location (relative to IDL ramp specification)
108*e57b52dbSJames Wright   @param[in]    pressure  Pressure used to damp to
109*e57b52dbSJames Wright   @param[inout] v_i       Output to be multiplied by weight function, summed into
110*e57b52dbSJames Wright   @param[out]   sigma     IDL ramp coefficient
111*e57b52dbSJames Wright **/
11230b5892fSJames Wright CEED_QFUNCTION_HELPER void InternalDampingLayer_IFunction_Integrand(const State s, const NewtonianIdealGasContext context, CeedScalar amplitude,
11330b5892fSJames Wright                                                                     CeedScalar length, CeedScalar start, CeedScalar location, CeedScalar pressure,
11430b5892fSJames Wright                                                                     CeedScalar v_i[5], CeedScalar *sigma) {
11530b5892fSJames Wright   const CeedScalar sigma_        = LinearRampCoefficient(amplitude, length, start, location);
11630b5892fSJames Wright   CeedScalar       damp_state[5] = {s.Y.pressure - pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
11730b5892fSJames Wright   InternalDampingLayer_Residual(context, s, sigma_, damp_state, idl_residual);
11830b5892fSJames Wright   AXPY(1, idl_residual, v_i, 5);
11930b5892fSJames Wright   *sigma = sigma_;
12030b5892fSJames Wright }
12130b5892fSJames Wright 
122cbe60e31SLeila Ghaffari // *****************************************************************************
12304e40bb6SJeremy L Thompson // This QFunction implements the following formulation of Navier-Stokes with explicit time stepping method
1243a8779fbSJames Wright //
12504e40bb6SJeremy L Thompson // This is 3D compressible Navier-Stokes in conservation form with state variables of density, momentum density, and total energy density.
1263a8779fbSJames Wright //
1273a8779fbSJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
1283a8779fbSJames Wright //   rho - Mass Density
1293a8779fbSJames Wright //   Ui  - Momentum Density,      Ui = rho ui
1303a8779fbSJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
1313a8779fbSJames Wright //
1323a8779fbSJames Wright // Navier-Stokes Equations:
1333a8779fbSJames Wright //   drho/dt + div( U )                               = 0
1343a8779fbSJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
1353a8779fbSJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
1363a8779fbSJames Wright //
1373a8779fbSJames Wright // Viscous Stress:
1383a8779fbSJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
1393a8779fbSJames Wright //
1403a8779fbSJames Wright // Thermal Stress:
1413a8779fbSJames Wright //   Fe = u Fu + k grad( T )
142bb8a0c61SJames Wright // Equation of State
1433a8779fbSJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
1443a8779fbSJames Wright //
1453a8779fbSJames Wright // Stabilization:
1463a8779fbSJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
1473a8779fbSJames Wright //     f1 = rho  sqrt(ui uj gij)
1483a8779fbSJames Wright //     gij = dXi/dX * dXi/dX
1493a8779fbSJames Wright //     TauC = Cc f1 / (8 gii)
1503a8779fbSJames Wright //     TauM = min( 1 , 1 / f1 )
1513a8779fbSJames Wright //     TauE = TauM / (Ce cv)
1523a8779fbSJames Wright //
1533a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
1543a8779fbSJames Wright //
1553a8779fbSJames Wright // Constants:
1563a8779fbSJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
1573a8779fbSJames Wright //   mu              ,  Dynamic viscosity
1583a8779fbSJames Wright //   k               ,  Thermal conductivity
1593a8779fbSJames Wright //   cv              ,  Specific heat, constant volume
1603a8779fbSJames Wright //   cp              ,  Specific heat, constant pressure
1613a8779fbSJames Wright //   g               ,  Gravity
1623a8779fbSJames Wright //   gamma  = cp / cv,  Specific heat ratio
1633a8779fbSJames Wright //
16404e40bb6SJeremy 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
16504e40bb6SJeremy L Thompson // gradu )
1663a8779fbSJames Wright // *****************************************************************************
1672b916ea7SJeremy L Thompson CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
168b3b24828SJames Wright   NewtonianIdealGasContext context      = (NewtonianIdealGasContext)ctx;
169b3b24828SJames Wright   const bool               use_divFdiff = context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE;
170b3b24828SJames Wright 
1713d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[0];
17287bd45e7SJames Wright   const CeedScalar(*Grad_q)               = in[1];
173ade49511SJames Wright   const CeedScalar(*q_data)               = in[2];
1740a32a5aaSJames Wright   const CeedScalar(*x)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[3];
175b3b24828SJames Wright   const CeedScalar(*divFdiff)[CEED_Q_VLA] = use_divFdiff ? (const CeedScalar(*)[CEED_Q_VLA])in[4] : NULL;
1763d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]              = (CeedScalar(*)[CEED_Q_VLA])out[0];
1773d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]      = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
1783a8779fbSJames Wright 
179bb8a0c61SJames Wright   const CeedScalar *g            = context->g;
180bb8a0c61SJames Wright   const CeedScalar  dt           = context->dt;
181b3b24828SJames Wright   const CeedScalar  idl_pressure = context->idl_pressure;
1823a8779fbSJames Wright 
1833d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
184ade49511SJames Wright     CeedScalar       U[5], wdetJ, dXdx[3][3];
1850a32a5aaSJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
186c1a52365SJed Brown     for (int j = 0; j < 5; j++) U[j] = q[j][i];
1871be49596SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
188edcfef1bSKenneth E. Jansen     State s = StateFromU(context, U);
189c1a52365SJed Brown 
190c1a52365SJed Brown     State grad_s[3];
191edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, STATEVAR_CONSERVATIVE, Grad_q, dXdx, grad_s);
192c1a52365SJed Brown 
193c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
19440a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
195c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
196c1a52365SJed Brown     KMUnpack(kmstress, stress);
197c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
198c1a52365SJed Brown 
199c1a52365SJed Brown     StateConservative F_inviscid[3];
200c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
201c1a52365SJed Brown 
202c1a52365SJed Brown     // Total flux
203c1a52365SJed Brown     CeedScalar Flux[5][3];
204d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
205c1a52365SJed Brown 
2067523f6aaSJames Wright     for (CeedInt j = 0; j < 5; j++) {
2077523f6aaSJames 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]);
2082b916ea7SJeremy L Thompson     }
209c1a52365SJed Brown 
21060dbb574SKenneth 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)};
2112b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
2123a8779fbSJames Wright 
2130a32a5aaSJames Wright     if (context->idl_enable) {
2140a32a5aaSJames Wright       const CeedScalar sigma         = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
215b3b24828SJames Wright       CeedScalar       damp_state[5] = {s.Y.pressure - idl_pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
21630b5892fSJames Wright       InternalDampingLayer_Residual(context, s, sigma, damp_state, idl_residual);
2170a32a5aaSJames Wright       for (int j = 0; j < 5; j++) v[j][i] -= wdetJ * idl_residual[j];
2180a32a5aaSJames Wright     }
2190a32a5aaSJames Wright 
220b3b24828SJames Wright     CeedScalar divFdiff_i[5] = {0.};
221b3b24828SJames Wright     if (use_divFdiff)
222b3b24828SJames Wright       for (int j = 1; j < 5; j++) divFdiff_i[j] = divFdiff[j - 1][i];
223b3b24828SJames Wright 
224d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
225b3b24828SJames Wright     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0};
226d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
227b3b24828SJames Wright     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff_i, stab);
2283a8779fbSJames Wright 
2292b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
2302b916ea7SJeremy 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]);
2312b916ea7SJeremy L Thompson     }
232b193fadcSJames Wright   }
2333a8779fbSJames Wright   return 0;
2343a8779fbSJames Wright }
2353a8779fbSJames Wright 
236*e57b52dbSJames Wright /**
237*e57b52dbSJames Wright   @brief IFunction integrand of Navier-Stokes for Newtonian ideal gas
238*e57b52dbSJames Wright 
239*e57b52dbSJames Wright   This is used in the quadrature point loop within a larger QFunction.
240*e57b52dbSJames Wright   `v_i` and `dv_i` are summed into (meaning they must be some initialized value).
241*e57b52dbSJames Wright   `kmstress` and `Tau_d` are given to be included as Jacobian data.
242*e57b52dbSJames Wright 
243*e57b52dbSJames Wright   @param[in]    s          `State` of solution
244*e57b52dbSJames Wright   @param[in]    grad_s     Physical gradient of solution
245*e57b52dbSJames Wright   @param[in]    s_dot      Time derivative of solution
246*e57b52dbSJames Wright   @param[in]    divFdiff_i Divergence of diffusive flux
247*e57b52dbSJames Wright   @param[in]    x_i        Coordinate location of quadrature point
248*e57b52dbSJames Wright   @param[in]    context    Newtonian context
249*e57b52dbSJames Wright   @param[in]    dXdx       Inverse of element mapping Jacobian (d\xi / dx)
250*e57b52dbSJames Wright   @param[inout] v_i        Output to be multiplied by weight function, summed into
251*e57b52dbSJames Wright   @param[inout] grad_v_i   Output to be multiplied by gradient of weight function, summed into
252*e57b52dbSJames Wright   @param[out]   kmstress   Viscous stress, in Kelvin-Mandel ordering
253*e57b52dbSJames Wright   @param[out]   Tau_d      Diagonal Tau coefficients
254*e57b52dbSJames Wright **/
25530b5892fSJames Wright CEED_QFUNCTION_HELPER void IFunction_Newtonian_Integrand(const State s, const State grad_s[3], const State s_dot, const CeedScalar divFdiff_i[5],
25630b5892fSJames Wright                                                          const CeedScalar x_i[3], const NewtonianIdealGasContext context, const CeedScalar dXdx[3][3],
257*e57b52dbSJames Wright                                                          CeedScalar v_i[5], CeedScalar grad_v_i[5][3], CeedScalar kmstress[6], CeedScalar Tau_d[3]) {
258*e57b52dbSJames Wright   CeedScalar        strain_rate[6], stress[3][3], F_visc_energy[3], F_total[5][3];
259*e57b52dbSJames Wright   StateConservative F_inviscid[3];
260*e57b52dbSJames Wright   const CeedScalar *g = context->g, dt = context->dt;
26130b5892fSJames Wright 
262*e57b52dbSJames Wright   // Advective and viscous fluxes
26330b5892fSJames Wright   KMStrainRate_State(grad_s, strain_rate);
26430b5892fSJames Wright   NewtonianStress(context, strain_rate, kmstress);
26530b5892fSJames Wright   KMUnpack(kmstress, stress);
266*e57b52dbSJames Wright   ViscousEnergyFlux(context, s.Y, grad_s, stress, F_visc_energy);
26730b5892fSJames Wright   FluxInviscid(context, s, F_inviscid);
268*e57b52dbSJames Wright   FluxTotal(F_inviscid, stress, F_visc_energy, F_total);
269*e57b52dbSJames Wright   AXPY(-1, (CeedScalar *)F_total, (CeedScalar *)grad_v_i, 15);
27030b5892fSJames Wright 
271*e57b52dbSJames Wright   // Body force and time derivative
27230b5892fSJames Wright   const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], Dot3(s.U.momentum, g)};
273*e57b52dbSJames Wright   CeedScalar       U_dot[5];
27430b5892fSJames Wright   UnpackState_U(s_dot.U, U_dot);
27530b5892fSJames Wright   for (CeedInt j = 0; j < 5; j++) v_i[j] += U_dot[j] - body_force[j];
276*e57b52dbSJames Wright 
277*e57b52dbSJames Wright   // Stabilization
278*e57b52dbSJames Wright   CeedScalar stab[5][3];
27930b5892fSJames Wright   Tau_diagPrim(context, s, dXdx, dt, Tau_d);
28030b5892fSJames Wright   Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, divFdiff_i, stab);
281*e57b52dbSJames Wright   AXPY(1, (CeedScalar *)stab, (CeedScalar *)grad_v_i, 15);
28230b5892fSJames Wright }
28330b5892fSJames Wright 
284*e57b52dbSJames Wright // @brief State-independent IFunction of Navier-Stokes for Newtonian ideal gas
2858fff8293SJames Wright CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
2868c85b835SJames Wright   NewtonianIdealGasContext context      = (NewtonianIdealGasContext)ctx;
287ff684e42SJames Wright   const bool               use_divFdiff = context->divFdiff_method != DIV_DIFF_FLUX_PROJ_NONE;
2888c85b835SJames Wright 
2893d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[0];
290*e57b52dbSJames Wright   const CeedScalar(*grad_q)               = in[1];
2913d65b166SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])in[2];
292ade49511SJames Wright   const CeedScalar(*q_data)               = in[3];
2933d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]        = (const CeedScalar(*)[CEED_Q_VLA])in[4];
294ff684e42SJames Wright   const CeedScalar(*divFdiff)[CEED_Q_VLA] = use_divFdiff ? (const CeedScalar(*)[CEED_Q_VLA])in[5] : NULL;
2953d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]              = (CeedScalar(*)[CEED_Q_VLA])out[0];
296*e57b52dbSJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA]      = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
297ade49511SJames Wright   CeedScalar(*jac_data)                   = out[2];
2983d65b166SJames Wright 
2993d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
300*e57b52dbSJames Wright     const CeedScalar q_i[5]     = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
301*e57b52dbSJames Wright     const CeedScalar q_i_dot[5] = {q_dot[0][i], q_dot[1][i], q_dot[2][i], q_dot[3][i], q_dot[4][i]};
302c1a52365SJed Brown     const CeedScalar x_i[3]     = {x[0][i], x[1][i], x[2][i]};
303*e57b52dbSJames Wright     const State      s          = StateFromQ(context, q_i, state_var);
304*e57b52dbSJames Wright     const State      s_dot      = StateFromQ_fwd(context, s, q_i_dot, state_var);
305c1a52365SJed Brown 
306ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
307ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
308c1a52365SJed Brown     State grad_s[3];
309*e57b52dbSJames Wright     StatePhysicalGradientFromReference(Q, i, context, s, state_var, grad_q, dXdx, grad_s);
3108c85b835SJames Wright     CeedScalar divFdiff_i[5] = {0.};
31130b5892fSJames Wright     if (use_divFdiff)
3128c85b835SJames Wright       for (int j = 1; j < 5; j++) divFdiff_i[j] = divFdiff[j - 1][i];
3133a8779fbSJames Wright 
314*e57b52dbSJames Wright     CeedScalar v_i[5] = {0.}, grad_v_i[5][3] = {{0.}}, kmstress[6], Tau_d[3], sigma;
315*e57b52dbSJames Wright     IFunction_Newtonian_Integrand(s, grad_s, s_dot, divFdiff_i, x_i, context, dXdx, v_i, grad_v_i, kmstress, Tau_d);
31630b5892fSJames Wright     if (context->idl_enable)
31730b5892fSJames Wright       InternalDampingLayer_IFunction_Integrand(s, context, context->idl_amplitude, context->idl_length, context->idl_start, x_i[0],
31830b5892fSJames Wright                                                context->idl_pressure, v_i, &sigma);
31930b5892fSJames Wright 
32030b5892fSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * v_i[j];
3212b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
3223d65b166SJames Wright       for (CeedInt k = 0; k < 3; k++) {
323*e57b52dbSJames Wright         grad_v[k][j][i] = wdetJ * (grad_v_i[j][0] * dXdx[k][0] + grad_v_i[j][1] * dXdx[k][1] + grad_v_i[j][2] * dXdx[k][2]);
3243d65b166SJames Wright       }
3252b916ea7SJeremy L Thompson     }
32630b5892fSJames Wright 
327*e57b52dbSJames Wright     StoredValuesPack(Q, i, 0, 5, q_i, jac_data);
328ade49511SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
329ade49511SJames Wright     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
33030b5892fSJames Wright     if (context->idl_enable) StoredValuesPack(Q, i, 14, 1, &sigma, jac_data);
331b193fadcSJames Wright   }
3323a8779fbSJames Wright   return 0;
3333a8779fbSJames Wright }
334f0b65372SJed Brown 
3352b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3368fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
33776555becSJames Wright }
33876555becSJames Wright 
3392b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3408fff8293SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
34176555becSJames Wright }
34276555becSJames Wright 
3439b103f75SJames Wright CEED_QFUNCTION(IFunction_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3449b103f75SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
3459b103f75SJames Wright }
3469b103f75SJames Wright 
347cbe60e31SLeila Ghaffari // *****************************************************************************
34804e40bb6SJeremy L Thompson // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
349cbe60e31SLeila Ghaffari // *****************************************************************************
3508fff8293SJames Wright CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3513d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[0];
35287bd45e7SJames Wright   const CeedScalar(*Grad_dq)         = in[1];
353ade49511SJames Wright   const CeedScalar(*q_data)          = in[2];
35494a7b3d2SKenneth E. Jansen   const CeedScalar(*jac_data)        = in[3];
3553d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
3563d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
3573d65b166SJames Wright 
358f0b65372SJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
359f0b65372SJed Brown   const CeedScalar        *g       = context->g;
360f0b65372SJed Brown 
3613d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
362ade49511SJames Wright     CeedScalar wdetJ, dXdx[3][3];
363ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
364f0b65372SJed Brown 
3658789e95fSJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
366ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
367ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
368ade49511SJames Wright     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
369edcfef1bSKenneth E. Jansen     State s = StateFromQ(context, qi, state_var);
370f0b65372SJed Brown 
371edcfef1bSKenneth E. Jansen     CeedScalar dqi[5];
37276555becSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
373edcfef1bSKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
374f0b65372SJed Brown 
375f0b65372SJed Brown     State grad_ds[3];
376edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
377f0b65372SJed Brown 
378f0b65372SJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
37940a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
380f0b65372SJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
381f0b65372SJed Brown     KMUnpack(dkmstress, dstress);
382f0b65372SJed Brown     KMUnpack(kmstress, stress);
383f0b65372SJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
384f0b65372SJed Brown 
385f0b65372SJed Brown     StateConservative dF_inviscid[3];
386f0b65372SJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
387f0b65372SJed Brown 
388f0b65372SJed Brown     // Total flux
389f0b65372SJed Brown     CeedScalar dFlux[5][3];
390d1b9ef12SLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
391f0b65372SJed Brown 
39222387d3aSJames Wright     for (int j = 0; j < 5; j++) {
39322387d3aSJames 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]);
3942b916ea7SJeremy L Thompson     }
395f0b65372SJed Brown 
39660dbb574SKenneth 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)};
39776555becSJames Wright     CeedScalar       dU[5]          = {0.};
39876555becSJames Wright     UnpackState_U(ds.U, dU);
3992b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
400f0b65372SJed Brown 
401e7754af5SKenneth E. Jansen     if (context->idl_enable) {
40294a7b3d2SKenneth E. Jansen       const CeedScalar sigma         = jac_data[14 * Q + i];
403e7754af5SKenneth E. Jansen       CeedScalar       damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
404e7754af5SKenneth E. Jansen       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
40530b5892fSJames Wright       InternalDampingLayer_Residual(context, s, sigma, damp_state, idl_residual);
406e7754af5SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
407e7754af5SKenneth E. Jansen     }
408e7754af5SKenneth E. Jansen 
409d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
410d1b9ef12SLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
411d1b9ef12SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
4128c85b835SJames Wright     const CeedScalar zeroFlux[5] = {0.};
4138c85b835SJames Wright     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, zeroFlux, dstab);
414d1b9ef12SLeila Ghaffari 
4152b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) {
4162b916ea7SJeremy 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]);
4172b916ea7SJeremy L Thompson     }
418b193fadcSJames Wright   }
419f0b65372SJed Brown   return 0;
420f0b65372SJed Brown }
4218085925cSJames Wright 
4222b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4238fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
42476555becSJames Wright }
42576555becSJames Wright 
4262b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4278fff8293SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
42876555becSJames Wright }
42976555becSJames Wright 
4309b103f75SJames Wright CEED_QFUNCTION(IJacobian_Newtonian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4319b103f75SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_ENTROPY);
4329b103f75SJames Wright }
4339b103f75SJames Wright 
434d1b9ef12SLeila Ghaffari // *****************************************************************************
4358085925cSJames Wright // Compute boundary integral (ie. for strongly set inflows)
436d1b9ef12SLeila Ghaffari // *****************************************************************************
4378fff8293SJames Wright CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
4384b96a86bSJames Wright   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
4393d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]       = (const CeedScalar(*)[CEED_Q_VLA])in[0];
44087bd45e7SJames Wright   const CeedScalar(*Grad_q)              = in[1];
441ade49511SJames Wright   const CeedScalar(*q_data_sur)          = in[2];
4423d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]             = (CeedScalar(*)[CEED_Q_VLA])out[0];
4434b96a86bSJames Wright   CeedScalar(*jac_data_sur)              = context->is_implicit ? out[1] : NULL;
4448085925cSJames Wright 
445d3b25f3aSJames Wright   const bool is_implicit = context->is_implicit;
4468085925cSJames Wright 
4472b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
44841e73928SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
449edcfef1bSKenneth E. Jansen     State            s     = StateFromQ(context, qi, state_var);
4508085925cSJames Wright 
45178e8b7daSJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
45278e8b7daSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
453ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
4548085925cSJames Wright 
455d3b25f3aSJames Wright     State grad_s[3];
456edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
4578085925cSJames Wright 
458d3b25f3aSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
45940a33f2dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
460d3b25f3aSJames Wright     NewtonianStress(context, strain_rate, kmstress);
461d3b25f3aSJames Wright     KMUnpack(kmstress, stress);
462d3b25f3aSJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
463d3b25f3aSJames Wright 
464d3b25f3aSJames Wright     StateConservative F_inviscid[3];
465d3b25f3aSJames Wright     FluxInviscid(context, s, F_inviscid);
466d3b25f3aSJames Wright 
467c5740391SJames Wright     CeedScalar Flux[5];
46878e8b7daSJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, normal, Flux);
469d3b25f3aSJames Wright 
470c5740391SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
4718085925cSJames Wright 
4724b96a86bSJames Wright     if (is_implicit) {
473ade49511SJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
474ade49511SJames Wright       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
4758085925cSJames Wright     }
4764b96a86bSJames Wright   }
4778085925cSJames Wright   return 0;
4788085925cSJames Wright }
4798085925cSJames Wright 
4802b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4818fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
482d4559bbeSJames Wright }
483d4559bbeSJames Wright 
4842b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4858fff8293SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
486d4559bbeSJames Wright }
487d4559bbeSJames Wright 
4889b103f75SJames Wright CEED_QFUNCTION(BoundaryIntegral_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4899b103f75SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_ENTROPY);
4909b103f75SJames Wright }
4919b103f75SJames Wright 
492d1b9ef12SLeila Ghaffari // *****************************************************************************
49368ae065aSJames Wright // Jacobian for "set nothing" boundary integral
494d1b9ef12SLeila Ghaffari // *****************************************************************************
4952b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
4968fff8293SJames Wright                                                     StateVariable state_var) {
4973d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
49887bd45e7SJames Wright   const CeedScalar(*Grad_dq)        = in[1];
499ade49511SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
500c1484fadSKenneth E. Jansen   const CeedScalar(*jac_data_sur)   = in[4];
50168ae065aSJames Wright   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
50268ae065aSJames Wright 
50368ae065aSJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
504ade49511SJames Wright   const bool                     is_implicit = context->is_implicit;
50568ae065aSJames Wright 
5063d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
50778e8b7daSJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
50878e8b7daSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
509ade49511SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
51068ae065aSJames Wright 
511edcfef1bSKenneth E. Jansen     CeedScalar qi[5], kmstress[6], dqi[5];
512ade49511SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
513ade49511SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
51441e73928SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
5153934e2b1SJames Wright 
516edcfef1bSKenneth E. Jansen     State s  = StateFromQ(context, qi, state_var);
517edcfef1bSKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
51868ae065aSJames Wright 
51968ae065aSJames Wright     State grad_ds[3];
520edcfef1bSKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
52168ae065aSJames Wright 
52268ae065aSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
52340a33f2dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
52468ae065aSJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
52568ae065aSJames Wright     KMUnpack(dkmstress, dstress);
52668ae065aSJames Wright     KMUnpack(kmstress, stress);
52768ae065aSJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
52868ae065aSJames Wright 
52968ae065aSJames Wright     StateConservative dF_inviscid[3];
53068ae065aSJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
53168ae065aSJames Wright 
532c5740391SJames Wright     CeedScalar dFlux[5];
53378e8b7daSJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, normal, dFlux);
53468ae065aSJames Wright 
535c5740391SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
536512c8ec7SJames Wright   }
53768ae065aSJames Wright   return 0;
53868ae065aSJames Wright }
53968ae065aSJames Wright 
5402b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5418fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
542d4559bbeSJames Wright }
543d4559bbeSJames Wright 
5442b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5458fff8293SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
546d4559bbeSJames Wright }
5479b103f75SJames Wright 
5489b103f75SJames Wright CEED_QFUNCTION(BoundaryIntegral_Jacobian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5499b103f75SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_ENTROPY);
5509b103f75SJames Wright }
55136038bbcSJames Wright 
5528561fee2SJames Wright // @brief Volume integral for RHS of divergence of diffusive flux direct projection
55336038bbcSJames Wright CEED_QFUNCTION_HELPER int DivDiffusiveFluxVolumeRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
55436038bbcSJames Wright                                                        StateVariable state_var) {
55536038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[0];
55636038bbcSJames Wright   const CeedScalar(*Grad_q)          = in[1];
55736038bbcSJames Wright   const CeedScalar(*q_data)          = in[2];
55836038bbcSJames Wright   CeedScalar(*Grad_v)[4][CEED_Q_VLA] = (CeedScalar(*)[4][CEED_Q_VLA])out[0];
55936038bbcSJames Wright 
56036038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
56136038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
56236038bbcSJames Wright 
56336038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
56436038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
56536038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
56636038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3];
56736038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
56836038bbcSJames Wright 
56936038bbcSJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
57036038bbcSJames Wright     {  // Get stress and Fe
57136038bbcSJames Wright       State      grad_s[3];
57236038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
57336038bbcSJames Wright 
57436038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
57536038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
57636038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
57736038bbcSJames Wright       KMUnpack(kmstress, stress);
57836038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
57936038bbcSJames Wright     }
58036038bbcSJames Wright 
58136038bbcSJames Wright     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
58236038bbcSJames Wright 
58336038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
58436038bbcSJames Wright       for (CeedInt k = 0; k < 3; k++) {
58536038bbcSJames Wright         Grad_v[k][j - 1][i] = -wdetJ * Dot3(dXdx[k], Fdiff[j]);
58636038bbcSJames Wright       }
58736038bbcSJames Wright     }
58836038bbcSJames Wright   }
58936038bbcSJames Wright   return 0;
59036038bbcSJames Wright }
59136038bbcSJames Wright 
59236038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
59336038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
59436038bbcSJames Wright }
59536038bbcSJames Wright 
59636038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
59736038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
59836038bbcSJames Wright }
59936038bbcSJames Wright 
60036038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxVolumeRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60136038bbcSJames Wright   return DivDiffusiveFluxVolumeRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
60236038bbcSJames Wright }
60336038bbcSJames Wright 
6048561fee2SJames Wright // @brief Boundary integral for RHS of divergence of diffusive flux direct projection
60536038bbcSJames Wright CEED_QFUNCTION_HELPER int DivDiffusiveFluxBoundaryRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
60636038bbcSJames Wright                                                          StateVariable state_var) {
60736038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
60836038bbcSJames Wright   const CeedScalar(*Grad_q)        = in[1];
60936038bbcSJames Wright   const CeedScalar(*q_data)        = in[2];
61036038bbcSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
61136038bbcSJames Wright 
61236038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
61336038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
61436038bbcSJames Wright 
61536038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
61636038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
61736038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
61836038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3], normal[3];
61936038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5];
62036038bbcSJames Wright 
62136038bbcSJames Wright     QdataBoundaryGradientUnpack_3D(Q, i, q_data, &wdetJ, dXdx, normal);
62236038bbcSJames Wright     {  // Get stress and Fe
62336038bbcSJames Wright       State      grad_s[3];
62436038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
62536038bbcSJames Wright 
62636038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
62736038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
62836038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
62936038bbcSJames Wright       KMUnpack(kmstress, stress);
63036038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
63136038bbcSJames Wright     }
63236038bbcSJames Wright 
63336038bbcSJames Wright     FluxTotal_Boundary(ZeroInviscidFluxes, stress, Fe, normal, Fdiff);
63436038bbcSJames Wright 
63536038bbcSJames Wright     // Continuity has no diffusive flux, therefore skip
63636038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) v[j - 1][i] = wdetJ * Fdiff[j];
63736038bbcSJames Wright   }
63836038bbcSJames Wright   return 0;
63936038bbcSJames Wright }
64036038bbcSJames Wright 
64136038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
64236038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
64336038bbcSJames Wright }
64436038bbcSJames Wright 
64536038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
64636038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
64736038bbcSJames Wright }
64836038bbcSJames Wright 
64936038bbcSJames Wright CEED_QFUNCTION(DivDiffusiveFluxBoundaryRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
65036038bbcSJames Wright   return DivDiffusiveFluxBoundaryRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
65136038bbcSJames Wright }
65236038bbcSJames Wright 
6538561fee2SJames Wright // @brief Integral for RHS of diffusive flux indirect projection
65436038bbcSJames Wright CEED_QFUNCTION_HELPER int DiffusiveFluxRHS_NS(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
65536038bbcSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
65636038bbcSJames Wright   const CeedScalar(*Grad_q)        = in[1];
65736038bbcSJames Wright   const CeedScalar(*q_data)        = in[2];
65836038bbcSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
65936038bbcSJames Wright 
66036038bbcSJames Wright   const NewtonianIdealGasContext context               = (NewtonianIdealGasContext)ctx;
66136038bbcSJames Wright   const StateConservative        ZeroInviscidFluxes[3] = {{0}};
66236038bbcSJames Wright 
66336038bbcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
66436038bbcSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
66536038bbcSJames Wright     const State      s     = StateFromQ(context, qi, state_var);
66636038bbcSJames Wright     CeedScalar       wdetJ, dXdx[3][3];
66736038bbcSJames Wright     CeedScalar       stress[3][3], Fe[3], Fdiff[5][3];
66836038bbcSJames Wright 
66936038bbcSJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
67036038bbcSJames Wright     {  // Get stress and Fe
67136038bbcSJames Wright       State      grad_s[3];
67236038bbcSJames Wright       CeedScalar strain_rate[6], kmstress[6];
67336038bbcSJames Wright 
67436038bbcSJames Wright       StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
67536038bbcSJames Wright       KMStrainRate_State(grad_s, strain_rate);
67636038bbcSJames Wright       NewtonianStress(context, strain_rate, kmstress);
67736038bbcSJames Wright       KMUnpack(kmstress, stress);
67836038bbcSJames Wright       ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
67936038bbcSJames Wright     }
68036038bbcSJames Wright 
68136038bbcSJames Wright     FluxTotal(ZeroInviscidFluxes, stress, Fe, Fdiff);
68236038bbcSJames Wright 
68336038bbcSJames Wright     for (CeedInt j = 1; j < 5; j++) {  // Continuity has no diffusive flux, therefore skip
68436038bbcSJames Wright       for (CeedInt k = 0; k < 3; k++) {
68536038bbcSJames Wright         v[(j - 1) * 3 + k][i] = wdetJ * Fdiff[j][k];
68636038bbcSJames Wright       }
68736038bbcSJames Wright     }
68836038bbcSJames Wright   }
68936038bbcSJames Wright   return 0;
69036038bbcSJames Wright }
69136038bbcSJames Wright 
69236038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
69336038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
69436038bbcSJames Wright }
69536038bbcSJames Wright 
69636038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
69736038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_PRIMITIVE);
69836038bbcSJames Wright }
69936038bbcSJames Wright 
70036038bbcSJames Wright CEED_QFUNCTION(DiffusiveFluxRHS_NS_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
70136038bbcSJames Wright   return DiffusiveFluxRHS_NS(ctx, Q, in, out, STATEVAR_ENTROPY);
70236038bbcSJames Wright }
703