xref: /libCEED/examples/fluids/qfunctions/newtonian.h (revision 9b6a821d0db442995f57d6faa6453ce3c921134b)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
388b783a1SJames Wright //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
588b783a1SJames Wright //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
788b783a1SJames Wright 
888b783a1SJames Wright /// @file
988b783a1SJames Wright /// Operator for Navier-Stokes example using PETSc
1088b783a1SJames Wright 
1188b783a1SJames Wright #ifndef newtonian_h
1288b783a1SJames Wright #define newtonian_h
1388b783a1SJames Wright 
1488b783a1SJames Wright #include <ceed.h>
15c9c2c079SJeremy L Thompson #include <math.h>
16738af36cSAdelekeBankole #include <stdlib.h>
172b730f8bSJeremy L Thompson 
18c6e8c570SJames Wright #include "newtonian_state.h"
19c9c2c079SJeremy L Thompson #include "newtonian_types.h"
202b89d87eSLeila Ghaffari #include "stabilization.h"
21c9c2c079SJeremy L Thompson #include "utils.h"
2288626eedSJames Wright 
23530ad8c4SKenneth E. Jansen CEED_QFUNCTION_HELPER void InternalDampingLayer(const NewtonianIdealGasContext context, const State s, const CeedScalar x_i[3], CeedScalar damp_Y[5],
24530ad8c4SKenneth E. Jansen                                                 CeedScalar damp_residual[5]) {
25530ad8c4SKenneth E. Jansen   const CeedScalar sigma = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
26530ad8c4SKenneth E. Jansen   ScaleN(damp_Y, sigma, 5);
27530ad8c4SKenneth E. Jansen   CeedScalar dx_i[3] = {0};
28530ad8c4SKenneth E. Jansen   State      damp_s  = StateFromY_fwd(context, s, damp_Y, x_i, dx_i);
29530ad8c4SKenneth E. Jansen 
30530ad8c4SKenneth E. Jansen   CeedScalar U[5];
31530ad8c4SKenneth E. Jansen   UnpackState_U(damp_s.U, U);
32530ad8c4SKenneth E. Jansen   for (int i = 0; i < 5; i++) damp_residual[i] += U[i];
33530ad8c4SKenneth E. Jansen }
34530ad8c4SKenneth E. Jansen 
3588626eedSJames Wright // *****************************************************************************
3688b783a1SJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
3788b783a1SJames Wright // *****************************************************************************
38be91e165SJames Wright CEED_QFUNCTION_HELPER int ICsNewtonianIG(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3988b783a1SJames Wright   // Inputs
4088b783a1SJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
4188b783a1SJames Wright 
4288b783a1SJames Wright   // Outputs
4388b783a1SJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
4488b783a1SJames Wright 
4588626eedSJames Wright   // Context
4688626eedSJames Wright   const SetupContext context = (SetupContext)ctx;
4788626eedSJames Wright 
4888b783a1SJames Wright   // Quadrature Point Loop
492b730f8bSJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
50d310b3d3SAdeleke O. Bankole     CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
5188b783a1SJames Wright     CeedScalar q[5] = {0.};
52d310b3d3SAdeleke O. Bankole     State      s    = StateFromPrimitive(&context->gas, context->reference, x);
53be91e165SJames Wright     StateToQ(&context->gas, s, q, state_var);
542b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
5588b783a1SJames Wright   }  // End of Quadrature Point Loop
5688b783a1SJames Wright   return 0;
5788b783a1SJames Wright }
5888b783a1SJames Wright 
592b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsNewtonianIG_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60be91e165SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_PRIMITIVE);
61d310b3d3SAdeleke O. Bankole }
62d310b3d3SAdeleke O. Bankole CEED_QFUNCTION(ICsNewtonianIG_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
63be91e165SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
64dc805cc4SLeila Ghaffari }
65dc805cc4SLeila Ghaffari 
66dc805cc4SLeila Ghaffari // *****************************************************************************
67ea61e9acSJeremy L Thompson // This QFunction implements the following formulation of Navier-Stokes with explicit time stepping method
6888b783a1SJames Wright //
69ea61e9acSJeremy L Thompson // This is 3D compressible Navier-Stokes in conservation form with state variables of density, momentum density, and total energy density.
7088b783a1SJames Wright //
7188b783a1SJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
7288b783a1SJames Wright //   rho - Mass Density
7388b783a1SJames Wright //   Ui  - Momentum Density,      Ui = rho ui
7488b783a1SJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
7588b783a1SJames Wright //
7688b783a1SJames Wright // Navier-Stokes Equations:
7788b783a1SJames Wright //   drho/dt + div( U )                               = 0
7888b783a1SJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
7988b783a1SJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
8088b783a1SJames Wright //
8188b783a1SJames Wright // Viscous Stress:
8288b783a1SJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
8388b783a1SJames Wright //
8488b783a1SJames Wright // Thermal Stress:
8588b783a1SJames Wright //   Fe = u Fu + k grad( T )
8688626eedSJames Wright // Equation of State
8788b783a1SJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
8888b783a1SJames Wright //
8988b783a1SJames Wright // Stabilization:
9088b783a1SJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
9188b783a1SJames Wright //     f1 = rho  sqrt(ui uj gij)
9288b783a1SJames Wright //     gij = dXi/dX * dXi/dX
9388b783a1SJames Wright //     TauC = Cc f1 / (8 gii)
9488b783a1SJames Wright //     TauM = min( 1 , 1 / f1 )
9588b783a1SJames Wright //     TauE = TauM / (Ce cv)
9688b783a1SJames Wright //
9788b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
9888b783a1SJames Wright //
9988b783a1SJames Wright // Constants:
10088b783a1SJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
10188b783a1SJames Wright //   mu              ,  Dynamic viscosity
10288b783a1SJames Wright //   k               ,  Thermal conductivity
10388b783a1SJames Wright //   cv              ,  Specific heat, constant volume
10488b783a1SJames Wright //   cp              ,  Specific heat, constant pressure
10588b783a1SJames Wright //   g               ,  Gravity
10688b783a1SJames Wright //   gamma  = cp / cv,  Specific heat ratio
10788b783a1SJames Wright //
108ea61e9acSJeremy 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
109ea61e9acSJeremy L Thompson // gradu )
11088b783a1SJames Wright // *****************************************************************************
1112b730f8bSJeremy L Thompson CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
11288b783a1SJames Wright   // Inputs
11346603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
114*9b6a821dSJames Wright   const CeedScalar(*Grad_q)        = in[1];
115f3e15844SJames Wright   const CeedScalar(*q_data)        = in[2];
11646603fc5SJames Wright   const CeedScalar(*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
11746603fc5SJames Wright 
11888b783a1SJames Wright   // Outputs
11946603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
12046603fc5SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
12188b783a1SJames Wright 
12288b783a1SJames Wright   // Context
12388b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
12488626eedSJames Wright   const CeedScalar        *g       = context->g;
12588626eedSJames Wright   const CeedScalar         dt      = context->dt;
12688b783a1SJames Wright 
12788b783a1SJames Wright   // Quadrature Point Loop
12846603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
129f3e15844SJames Wright     CeedScalar U[5], wdetJ, dXdx[3][3];
1305c677226SJed Brown     for (int j = 0; j < 5; j++) U[j] = q[j][i];
131f3e15844SJames Wright     StoredValuesUnpack(Q, i, 0, 1, q_data, &wdetJ);
132f3e15844SJames Wright     StoredValuesUnpack(Q, i, 1, 9, q_data, (CeedScalar *)dXdx);
1335c677226SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
1345c677226SJed Brown     State            s      = StateFromU(context, U, x_i);
1355c677226SJed Brown 
1365c677226SJed Brown     State grad_s[3];
137*9b6a821dSJames Wright     StatePhysicalGradientFromReference(Q, i, context, s, x_i, STATEVAR_CONSERVATIVE, Grad_q, dXdx, false, grad_s);
1385c677226SJed Brown 
1395c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
140d08fcc28SJames Wright     KMStrainRate_State(grad_s, strain_rate);
1415c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
1425c677226SJed Brown     KMUnpack(kmstress, stress);
1435c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
1445c677226SJed Brown 
1455c677226SJed Brown     StateConservative F_inviscid[3];
1465c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
1475c677226SJed Brown 
1485c677226SJed Brown     // Total flux
1495c677226SJed Brown     CeedScalar Flux[5][3];
1502b89d87eSLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
1515c677226SJed Brown 
1527b69c783SJames Wright     for (CeedInt j = 0; j < 5; j++) {
1537b69c783SJames 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]);
1542b730f8bSJeremy L Thompson     }
1555c677226SJed Brown 
1565c677226SJed Brown     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], 0};
1572b730f8bSJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
15888b783a1SJames Wright 
1592b89d87eSLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
1602b89d87eSLeila Ghaffari     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0};
1612b89d87eSLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
1622b89d87eSLeila Ghaffari     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, x_i, stab);
16388b783a1SJames Wright 
1642b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
1652b730f8bSJeremy 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]);
1662b730f8bSJeremy L Thompson     }
16788b783a1SJames Wright   }  // End Quadrature Point Loop
16888b783a1SJames Wright 
16988b783a1SJames Wright   // Return
17088b783a1SJames Wright   return 0;
17188b783a1SJames Wright }
17288b783a1SJames Wright 
17388b783a1SJames Wright // *****************************************************************************
174ea61e9acSJeremy L Thompson // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
17588b783a1SJames Wright //
17688b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
17788b783a1SJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
178ea61e9acSJeremy L Thompson //                                       (diffusive terms will be added later)
17988b783a1SJames Wright // *****************************************************************************
180be91e165SJames Wright CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
18188b783a1SJames Wright   // Inputs
18246603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
183*9b6a821dSJames Wright   const CeedScalar(*Grad_q)            = in[1];
18446603fc5SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
185f3e15844SJames Wright   const CeedScalar(*q_data)            = in[3];
18646603fc5SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[4];
18746603fc5SJames Wright 
18888b783a1SJames Wright   // Outputs
18946603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
19046603fc5SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
191f3e15844SJames Wright   CeedScalar(*jac_data)              = out[2];
19246603fc5SJames Wright 
19388b783a1SJames Wright   // Context
19488b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
19588626eedSJames Wright   const CeedScalar        *g       = context->g;
19688626eedSJames Wright   const CeedScalar         dt      = context->dt;
197530ad8c4SKenneth E. Jansen   const CeedScalar         P0      = context->P0;
19888b783a1SJames Wright 
19988b783a1SJames Wright   // Quadrature Point Loop
20046603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
20146603fc5SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
2025c677226SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
203be91e165SJames Wright     const State      s      = StateFromQ(context, qi, x_i, state_var);
2045c677226SJed Brown 
205f3e15844SJames Wright     CeedScalar wdetJ, dXdx[3][3];
206f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
2075c677226SJed Brown     State grad_s[3];
208*9b6a821dSJames Wright     StatePhysicalGradientFromReference(Q, i, context, s, x_i, state_var, Grad_q, dXdx, false, grad_s);
2095c677226SJed Brown 
2105c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
211d08fcc28SJames Wright     KMStrainRate_State(grad_s, strain_rate);
2125c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
2135c677226SJed Brown     KMUnpack(kmstress, stress);
2145c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
2155c677226SJed Brown 
2165c677226SJed Brown     StateConservative F_inviscid[3];
2175c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
2185c677226SJed Brown 
2195c677226SJed Brown     // Total flux
2205c677226SJed Brown     CeedScalar Flux[5][3];
2212b89d87eSLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
2225c677226SJed Brown 
2237b69c783SJames Wright     for (CeedInt j = 0; j < 5; j++) {
2247b69c783SJames Wright       for (CeedInt k = 0; k < 3; k++) {
2257b69c783SJames 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]);
22646603fc5SJames Wright       }
2272b730f8bSJeremy L Thompson     }
2285c677226SJed Brown 
2295c677226SJed Brown     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], 0};
23088b783a1SJames Wright 
2312b89d87eSLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
2323d02368aSJames Wright     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5], dx0[3] = {0};
2333d02368aSJames Wright     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
234be91e165SJames Wright     State s_dot = StateFromQ_fwd(context, s, qi_dot, x_i, dx0, state_var);
2353d02368aSJames Wright     UnpackState_U(s_dot.U, U_dot);
2363d02368aSJames Wright 
2372b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
238530ad8c4SKenneth E. Jansen     if (context->idl_enable) {
239530ad8c4SKenneth E. Jansen       CeedScalar damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
240530ad8c4SKenneth E. Jansen       InternalDampingLayer(context, s, x_i, damp_state, idl_residual);
241530ad8c4SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
242530ad8c4SKenneth E. Jansen     }
243530ad8c4SKenneth E. Jansen 
2442b89d87eSLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
2452b89d87eSLeila Ghaffari     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, x_i, stab);
24688b783a1SJames Wright 
2472b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
24846603fc5SJames Wright       for (CeedInt k = 0; k < 3; k++) {
24946603fc5SJames 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]);
25046603fc5SJames Wright       }
2512b730f8bSJeremy L Thompson     }
252f3e15844SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
253f3e15844SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
254f3e15844SJames Wright     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
25588b783a1SJames Wright 
25688b783a1SJames Wright   }  // End Quadrature Point Loop
25788b783a1SJames Wright 
25888b783a1SJames Wright   // Return
25988b783a1SJames Wright   return 0;
26088b783a1SJames Wright }
261e334ad8fSJed Brown 
2622b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
263be91e165SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
2643d02368aSJames Wright }
2653d02368aSJames Wright 
2662b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
267be91e165SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
2683d02368aSJames Wright }
2693d02368aSJames Wright 
270dc805cc4SLeila Ghaffari // *****************************************************************************
271ea61e9acSJeremy L Thompson // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
272dc805cc4SLeila Ghaffari // *****************************************************************************
273be91e165SJames Wright CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
274e334ad8fSJed Brown   // Inputs
27546603fc5SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
276*9b6a821dSJames Wright   const CeedScalar(*Grad_dq)        = in[1];
277f3e15844SJames Wright   const CeedScalar(*q_data)         = in[2];
27846603fc5SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[3];
279f3e15844SJames Wright   const CeedScalar(*jac_data)       = in[4];
28046603fc5SJames Wright 
281e334ad8fSJed Brown   // Outputs
28246603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
28346603fc5SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
28446603fc5SJames Wright 
285e334ad8fSJed Brown   // Context
286e334ad8fSJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
287e334ad8fSJed Brown   const CeedScalar        *g       = context->g;
288e334ad8fSJed Brown 
289e334ad8fSJed Brown   // Quadrature Point Loop
29046603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
291f3e15844SJames Wright     CeedScalar wdetJ, dXdx[3][3];
292f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
293e334ad8fSJed Brown 
294c98a0616SJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
295f3e15844SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
296f3e15844SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
297f3e15844SJames Wright     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
298e334ad8fSJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
299be91e165SJames Wright     State            s      = StateFromQ(context, qi, x_i, state_var);
300e334ad8fSJed Brown 
3013d02368aSJames Wright     CeedScalar dqi[5], dx0[3] = {0};
3023d02368aSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
303be91e165SJames Wright     State ds = StateFromQ_fwd(context, s, dqi, x_i, dx0, state_var);
304e334ad8fSJed Brown 
305e334ad8fSJed Brown     State grad_ds[3];
306*9b6a821dSJames Wright     StatePhysicalGradientFromReference(Q, i, context, s, x_i, state_var, Grad_dq, dXdx, true, grad_ds);
307e334ad8fSJed Brown 
308e334ad8fSJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
309d08fcc28SJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
310e334ad8fSJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
311e334ad8fSJed Brown     KMUnpack(dkmstress, dstress);
312e334ad8fSJed Brown     KMUnpack(kmstress, stress);
313e334ad8fSJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
314e334ad8fSJed Brown 
315e334ad8fSJed Brown     StateConservative dF_inviscid[3];
316e334ad8fSJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
317e334ad8fSJed Brown 
318e334ad8fSJed Brown     // Total flux
319e334ad8fSJed Brown     CeedScalar dFlux[5][3];
3202b89d87eSLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
321e334ad8fSJed Brown 
32251b00d91SJames Wright     for (int j = 0; j < 5; j++) {
32351b00d91SJames 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]);
3242b730f8bSJeremy L Thompson     }
325e334ad8fSJed Brown 
326e334ad8fSJed Brown     const CeedScalar dbody_force[5] = {0, ds.U.density * g[0], ds.U.density * g[1], ds.U.density * g[2], 0};
3273d02368aSJames Wright     CeedScalar       dU[5]          = {0.};
3283d02368aSJames Wright     UnpackState_U(ds.U, dU);
3292b730f8bSJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
330e334ad8fSJed Brown 
331530ad8c4SKenneth E. Jansen     if (context->idl_enable) {
332530ad8c4SKenneth E. Jansen       CeedScalar damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
333530ad8c4SKenneth E. Jansen       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
334530ad8c4SKenneth E. Jansen       InternalDampingLayer(context, s, x_i, damp_state, idl_residual);
335530ad8c4SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
336530ad8c4SKenneth E. Jansen     }
337530ad8c4SKenneth E. Jansen 
3382b89d87eSLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
3392b89d87eSLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
3402b89d87eSLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
3412b89d87eSLeila Ghaffari     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, x_i, dstab);
3422b89d87eSLeila Ghaffari 
3432b730f8bSJeremy L Thompson     for (int j = 0; j < 5; j++) {
3442b730f8bSJeremy 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]);
3452b730f8bSJeremy L Thompson     }
346e334ad8fSJed Brown   }  // End Quadrature Point Loop
347e334ad8fSJed Brown   return 0;
348e334ad8fSJed Brown }
34965dd5cafSJames Wright 
3502b730f8bSJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
351be91e165SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
3523d02368aSJames Wright }
3533d02368aSJames Wright 
3542b730f8bSJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
355be91e165SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
3563d02368aSJames Wright }
3573d02368aSJames Wright 
3582b89d87eSLeila Ghaffari // *****************************************************************************
35965dd5cafSJames Wright // Compute boundary integral (ie. for strongly set inflows)
3602b89d87eSLeila Ghaffari // *****************************************************************************
361be91e165SJames Wright CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
36246603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
363*9b6a821dSJames Wright   const CeedScalar(*Grad_q)        = in[1];
364f3e15844SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
36546603fc5SJames Wright   const CeedScalar(*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
36665dd5cafSJames Wright 
36746603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
368f3e15844SJames Wright   CeedScalar(*jac_data_sur)  = out[1];
36965dd5cafSJames Wright 
3702c4e60d7SJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
3712c4e60d7SJames Wright   const bool                     is_implicit = context->is_implicit;
37265dd5cafSJames Wright 
3732b730f8bSJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
3742c4e60d7SJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
375efe9d856SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
376be91e165SJames Wright     State            s      = StateFromQ(context, qi, x_i, state_var);
37765dd5cafSJames Wright 
378f3e15844SJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
379f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
380f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
38165dd5cafSJames Wright 
3822c4e60d7SJames Wright     State grad_s[3];
383*9b6a821dSJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, x_i, state_var, Grad_q, dXdx, false, grad_s);
38465dd5cafSJames Wright 
3852c4e60d7SJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
386d08fcc28SJames Wright     KMStrainRate_State(grad_s, strain_rate);
3872c4e60d7SJames Wright     NewtonianStress(context, strain_rate, kmstress);
3882c4e60d7SJames Wright     KMUnpack(kmstress, stress);
3892c4e60d7SJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
3902c4e60d7SJames Wright 
3912c4e60d7SJames Wright     StateConservative F_inviscid[3];
3922c4e60d7SJames Wright     FluxInviscid(context, s, F_inviscid);
3932c4e60d7SJames Wright 
3945bce47c7SJames Wright     CeedScalar Flux[5];
3955bce47c7SJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, norm, Flux);
3962c4e60d7SJames Wright 
3975bce47c7SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
39865dd5cafSJames Wright 
399f3e15844SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
400f3e15844SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
40165dd5cafSJames Wright   }
40265dd5cafSJames Wright   return 0;
40365dd5cafSJames Wright }
40465dd5cafSJames Wright 
4052b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
406be91e165SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
40720840d50SJames Wright }
40820840d50SJames Wright 
4092b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
410be91e165SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
41120840d50SJames Wright }
41220840d50SJames Wright 
4132b89d87eSLeila Ghaffari // *****************************************************************************
414b55ac660SJames Wright // Jacobian for "set nothing" boundary integral
4152b89d87eSLeila Ghaffari // *****************************************************************************
4162b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
417be91e165SJames Wright                                                     StateVariable state_var) {
418b55ac660SJames Wright   // Inputs
41946603fc5SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
420*9b6a821dSJames Wright   const CeedScalar(*Grad_dq)        = in[1];
421f3e15844SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
42246603fc5SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[3];
423f3e15844SJames Wright   const CeedScalar(*jac_data_sur)   = in[4];
42446603fc5SJames Wright 
425b55ac660SJames Wright   // Outputs
426b55ac660SJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
427b55ac660SJames Wright 
428b55ac660SJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
429f3e15844SJames Wright   const bool                     is_implicit = context->is_implicit;
430b55ac660SJames Wright 
431b55ac660SJames Wright   // Quadrature Point Loop
43246603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
433b55ac660SJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
434f3e15844SJames Wright     CeedScalar       wdetJb, dXdx[2][3], norm[3];
435f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
436f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
437b55ac660SJames Wright 
438efe9d856SJames Wright     CeedScalar qi[5], kmstress[6], dqi[5], dx_i[3] = {0.};
439f3e15844SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
440f3e15844SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
441efe9d856SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
44257e55a1cSJames Wright 
443be91e165SJames Wright     State s  = StateFromQ(context, qi, x_i, state_var);
444be91e165SJames Wright     State ds = StateFromQ_fwd(context, s, dqi, x_i, dx_i, state_var);
445b55ac660SJames Wright 
446b55ac660SJames Wright     State grad_ds[3];
447*9b6a821dSJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, x_i, state_var, Grad_dq, dXdx, false, grad_ds);
448b55ac660SJames Wright 
449b55ac660SJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
450d08fcc28SJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
451b55ac660SJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
452b55ac660SJames Wright     KMUnpack(dkmstress, dstress);
453b55ac660SJames Wright     KMUnpack(kmstress, stress);
454b55ac660SJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
455b55ac660SJames Wright 
456b55ac660SJames Wright     StateConservative dF_inviscid[3];
457b55ac660SJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
458b55ac660SJames Wright 
4595bce47c7SJames Wright     CeedScalar dFlux[5];
4605bce47c7SJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, norm, dFlux);
461b55ac660SJames Wright 
4625bce47c7SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
463b55ac660SJames Wright   }  // End Quadrature Point Loop
464b55ac660SJames Wright   return 0;
465b55ac660SJames Wright }
466b55ac660SJames Wright 
4672b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
468be91e165SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
46920840d50SJames Wright }
47020840d50SJames Wright 
4712b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
472be91e165SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
47320840d50SJames Wright }
47420840d50SJames Wright 
47588b783a1SJames Wright #endif  // newtonian_h
476