xref: /libCEED/examples/fluids/qfunctions/newtonian.h (revision f0b01153af3a01a9c66c18412b5c017396d8150f)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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 #include <ceed.h>
11c9c2c079SJeremy L Thompson #include <math.h>
12738af36cSAdelekeBankole #include <stdlib.h>
132b730f8bSJeremy L Thompson 
14c6e8c570SJames Wright #include "newtonian_state.h"
15c9c2c079SJeremy L Thompson #include "newtonian_types.h"
162b89d87eSLeila Ghaffari #include "stabilization.h"
17c9c2c079SJeremy L Thompson #include "utils.h"
1888626eedSJames Wright 
191d2a9659SKenneth E. Jansen CEED_QFUNCTION_HELPER void InternalDampingLayer(const NewtonianIdealGasContext context, const State s, const CeedScalar sigma, CeedScalar damp_Y[5],
20530ad8c4SKenneth E. Jansen                                                 CeedScalar damp_residual[5]) {
21530ad8c4SKenneth E. Jansen   ScaleN(damp_Y, sigma, 5);
223bd61617SKenneth E. Jansen   State damp_s = StateFromY_fwd(context, s, damp_Y);
23530ad8c4SKenneth E. Jansen 
24530ad8c4SKenneth E. Jansen   CeedScalar U[5];
25530ad8c4SKenneth E. Jansen   UnpackState_U(damp_s.U, U);
26530ad8c4SKenneth E. Jansen   for (int i = 0; i < 5; i++) damp_residual[i] += U[i];
27530ad8c4SKenneth E. Jansen }
28530ad8c4SKenneth E. Jansen 
2988626eedSJames Wright // *****************************************************************************
3088b783a1SJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
3188b783a1SJames Wright // *****************************************************************************
32be91e165SJames Wright CEED_QFUNCTION_HELPER int ICsNewtonianIG(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3388b783a1SJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
3488b783a1SJames Wright 
3588626eedSJames Wright   const SetupContext context = (SetupContext)ctx;
3688626eedSJames Wright 
372b730f8bSJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
3888b783a1SJames Wright     CeedScalar q[5] = {0.};
393bd61617SKenneth E. Jansen     State      s    = StateFromPrimitive(&context->gas, context->reference);
40be91e165SJames Wright     StateToQ(&context->gas, s, q, state_var);
412b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
42*f0b01153SJames Wright   }
4388b783a1SJames Wright   return 0;
4488b783a1SJames Wright }
4588b783a1SJames Wright 
462b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsNewtonianIG_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
47be91e165SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_PRIMITIVE);
48d310b3d3SAdeleke O. Bankole }
49d310b3d3SAdeleke O. Bankole CEED_QFUNCTION(ICsNewtonianIG_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
50be91e165SJames Wright   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
51dc805cc4SLeila Ghaffari }
52dc805cc4SLeila Ghaffari 
53dc805cc4SLeila Ghaffari // *****************************************************************************
54ea61e9acSJeremy L Thompson // This QFunction implements the following formulation of Navier-Stokes with explicit time stepping method
5588b783a1SJames Wright //
56ea61e9acSJeremy L Thompson // This is 3D compressible Navier-Stokes in conservation form with state variables of density, momentum density, and total energy density.
5788b783a1SJames Wright //
5888b783a1SJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
5988b783a1SJames Wright //   rho - Mass Density
6088b783a1SJames Wright //   Ui  - Momentum Density,      Ui = rho ui
6188b783a1SJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
6288b783a1SJames Wright //
6388b783a1SJames Wright // Navier-Stokes Equations:
6488b783a1SJames Wright //   drho/dt + div( U )                               = 0
6588b783a1SJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
6688b783a1SJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
6788b783a1SJames Wright //
6888b783a1SJames Wright // Viscous Stress:
6988b783a1SJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
7088b783a1SJames Wright //
7188b783a1SJames Wright // Thermal Stress:
7288b783a1SJames Wright //   Fe = u Fu + k grad( T )
7388626eedSJames Wright // Equation of State
7488b783a1SJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
7588b783a1SJames Wright //
7688b783a1SJames Wright // Stabilization:
7788b783a1SJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
7888b783a1SJames Wright //     f1 = rho  sqrt(ui uj gij)
7988b783a1SJames Wright //     gij = dXi/dX * dXi/dX
8088b783a1SJames Wright //     TauC = Cc f1 / (8 gii)
8188b783a1SJames Wright //     TauM = min( 1 , 1 / f1 )
8288b783a1SJames Wright //     TauE = TauM / (Ce cv)
8388b783a1SJames Wright //
8488b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
8588b783a1SJames Wright //
8688b783a1SJames Wright // Constants:
8788b783a1SJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
8888b783a1SJames Wright //   mu              ,  Dynamic viscosity
8988b783a1SJames Wright //   k               ,  Thermal conductivity
9088b783a1SJames Wright //   cv              ,  Specific heat, constant volume
9188b783a1SJames Wright //   cp              ,  Specific heat, constant pressure
9288b783a1SJames Wright //   g               ,  Gravity
9388b783a1SJames Wright //   gamma  = cp / cv,  Specific heat ratio
9488b783a1SJames Wright //
95ea61e9acSJeremy 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
96ea61e9acSJeremy L Thompson // gradu )
9788b783a1SJames Wright // *****************************************************************************
982b730f8bSJeremy L Thompson CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
9946603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1009b6a821dSJames Wright   const CeedScalar(*Grad_q)          = in[1];
101f3e15844SJames Wright   const CeedScalar(*q_data)          = in[2];
10246603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
10346603fc5SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
10488b783a1SJames Wright 
10588b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
10688626eedSJames Wright   const CeedScalar        *g       = context->g;
10788626eedSJames Wright   const CeedScalar         dt      = context->dt;
10888b783a1SJames Wright 
10946603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
110f3e15844SJames Wright     CeedScalar U[5], wdetJ, dXdx[3][3];
1115c677226SJed Brown     for (int j = 0; j < 5; j++) U[j] = q[j][i];
112f3e15844SJames Wright     StoredValuesUnpack(Q, i, 0, 1, q_data, &wdetJ);
113f3e15844SJames Wright     StoredValuesUnpack(Q, i, 1, 9, q_data, (CeedScalar *)dXdx);
1143bd61617SKenneth E. Jansen     State s = StateFromU(context, U);
1155c677226SJed Brown 
1165c677226SJed Brown     State grad_s[3];
1173bd61617SKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, STATEVAR_CONSERVATIVE, Grad_q, dXdx, grad_s);
1185c677226SJed Brown 
1195c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
120d08fcc28SJames Wright     KMStrainRate_State(grad_s, strain_rate);
1215c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
1225c677226SJed Brown     KMUnpack(kmstress, stress);
1235c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
1245c677226SJed Brown 
1255c677226SJed Brown     StateConservative F_inviscid[3];
1265c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
1275c677226SJed Brown 
1285c677226SJed Brown     // Total flux
1295c677226SJed Brown     CeedScalar Flux[5][3];
1302b89d87eSLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
1315c677226SJed Brown 
1327b69c783SJames Wright     for (CeedInt j = 0; j < 5; j++) {
1337b69c783SJames 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]);
1342b730f8bSJeremy L Thompson     }
1355c677226SJed Brown 
136858ec087SKenneth 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)};
1372b730f8bSJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
13888b783a1SJames Wright 
1392b89d87eSLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
1402b89d87eSLeila Ghaffari     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0};
1412b89d87eSLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
1423bd61617SKenneth E. Jansen     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, stab);
14388b783a1SJames Wright 
1442b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
1452b730f8bSJeremy 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]);
1462b730f8bSJeremy L Thompson     }
147*f0b01153SJames Wright   }
14888b783a1SJames Wright   return 0;
14988b783a1SJames Wright }
15088b783a1SJames Wright 
15188b783a1SJames Wright // *****************************************************************************
152ea61e9acSJeremy L Thompson // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
15388b783a1SJames Wright //
15488b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
15588b783a1SJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
156ea61e9acSJeremy L Thompson //                                       (diffusive terms will be added later)
15788b783a1SJames Wright // *****************************************************************************
158be91e165SJames Wright CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
15946603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1609b6a821dSJames Wright   const CeedScalar(*Grad_q)            = in[1];
16146603fc5SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
162f3e15844SJames Wright   const CeedScalar(*q_data)            = in[3];
16346603fc5SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[4];
16446603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
16546603fc5SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA]   = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
166f3e15844SJames Wright   CeedScalar(*jac_data)                = out[2];
16746603fc5SJames Wright 
16888b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
16988626eedSJames Wright   const CeedScalar        *g       = context->g;
17088626eedSJames Wright   const CeedScalar         dt      = context->dt;
171530ad8c4SKenneth E. Jansen   const CeedScalar         P0      = context->P0;
17288b783a1SJames Wright 
17346603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
17446603fc5SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
1755c677226SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
1763bd61617SKenneth E. Jansen     const State      s      = StateFromQ(context, qi, state_var);
1775c677226SJed Brown 
178f3e15844SJames Wright     CeedScalar wdetJ, dXdx[3][3];
179f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
1805c677226SJed Brown     State grad_s[3];
1813bd61617SKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
1825c677226SJed Brown 
1835c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
184d08fcc28SJames Wright     KMStrainRate_State(grad_s, strain_rate);
1855c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
1865c677226SJed Brown     KMUnpack(kmstress, stress);
1875c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
1885c677226SJed Brown 
1895c677226SJed Brown     StateConservative F_inviscid[3];
1905c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
1915c677226SJed Brown 
1925c677226SJed Brown     // Total flux
1935c677226SJed Brown     CeedScalar Flux[5][3];
1942b89d87eSLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
1955c677226SJed Brown 
1967b69c783SJames Wright     for (CeedInt j = 0; j < 5; j++) {
1977b69c783SJames Wright       for (CeedInt k = 0; k < 3; k++) {
1987b69c783SJames 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]);
19946603fc5SJames Wright       }
2002b730f8bSJeremy L Thompson     }
2015c677226SJed Brown 
202858ec087SKenneth 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)};
20388b783a1SJames Wright 
2042b89d87eSLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
2053bd61617SKenneth E. Jansen     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5];
2063d02368aSJames Wright     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
2073bd61617SKenneth E. Jansen     State s_dot = StateFromQ_fwd(context, s, qi_dot, state_var);
2083d02368aSJames Wright     UnpackState_U(s_dot.U, U_dot);
2093d02368aSJames Wright 
2102b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
211530ad8c4SKenneth E. Jansen     if (context->idl_enable) {
2121d2a9659SKenneth E. Jansen       const CeedScalar sigma = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
2131d2a9659SKenneth E. Jansen       StoredValuesPack(Q, i, 14, 1, &sigma, jac_data);
214530ad8c4SKenneth E. Jansen       CeedScalar damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
2151d2a9659SKenneth E. Jansen       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
216530ad8c4SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
217530ad8c4SKenneth E. Jansen     }
218530ad8c4SKenneth E. Jansen 
2192b89d87eSLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
2203bd61617SKenneth E. Jansen     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, stab);
22188b783a1SJames Wright 
2222b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
22346603fc5SJames Wright       for (CeedInt k = 0; k < 3; k++) {
22446603fc5SJames 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]);
22546603fc5SJames Wright       }
2262b730f8bSJeremy L Thompson     }
227f3e15844SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
228f3e15844SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
229f3e15844SJames Wright     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
230*f0b01153SJames Wright   }
23188b783a1SJames Wright   return 0;
23288b783a1SJames Wright }
233e334ad8fSJed Brown 
2342b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
235be91e165SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
2363d02368aSJames Wright }
2373d02368aSJames Wright 
2382b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
239be91e165SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
2403d02368aSJames Wright }
2413d02368aSJames Wright 
242dc805cc4SLeila Ghaffari // *****************************************************************************
243ea61e9acSJeremy L Thompson // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
244dc805cc4SLeila Ghaffari // *****************************************************************************
245be91e165SJames Wright CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
24646603fc5SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2479b6a821dSJames Wright   const CeedScalar(*Grad_dq)         = in[1];
248f3e15844SJames Wright   const CeedScalar(*q_data)          = in[2];
2491d2a9659SKenneth E. Jansen   const CeedScalar(*jac_data)        = in[3];
25046603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
25146603fc5SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
25246603fc5SJames Wright 
253e334ad8fSJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
254e334ad8fSJed Brown   const CeedScalar        *g       = context->g;
255e334ad8fSJed Brown 
25646603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
257f3e15844SJames Wright     CeedScalar wdetJ, dXdx[3][3];
258f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
259e334ad8fSJed Brown 
260c98a0616SJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
261f3e15844SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
262f3e15844SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
263f3e15844SJames Wright     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
2643bd61617SKenneth E. Jansen     State s = StateFromQ(context, qi, state_var);
265e334ad8fSJed Brown 
2663bd61617SKenneth E. Jansen     CeedScalar dqi[5];
2673d02368aSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
2683bd61617SKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
269e334ad8fSJed Brown 
270e334ad8fSJed Brown     State grad_ds[3];
2713bd61617SKenneth E. Jansen     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
272e334ad8fSJed Brown 
273e334ad8fSJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
274d08fcc28SJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
275e334ad8fSJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
276e334ad8fSJed Brown     KMUnpack(dkmstress, dstress);
277e334ad8fSJed Brown     KMUnpack(kmstress, stress);
278e334ad8fSJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
279e334ad8fSJed Brown 
280e334ad8fSJed Brown     StateConservative dF_inviscid[3];
281e334ad8fSJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
282e334ad8fSJed Brown 
283e334ad8fSJed Brown     // Total flux
284e334ad8fSJed Brown     CeedScalar dFlux[5][3];
2852b89d87eSLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
286e334ad8fSJed Brown 
28751b00d91SJames Wright     for (int j = 0; j < 5; j++) {
28851b00d91SJames 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]);
2892b730f8bSJeremy L Thompson     }
290e334ad8fSJed Brown 
291858ec087SKenneth 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)};
2923d02368aSJames Wright     CeedScalar       dU[5]          = {0.};
2933d02368aSJames Wright     UnpackState_U(ds.U, dU);
2942b730f8bSJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
295e334ad8fSJed Brown 
296530ad8c4SKenneth E. Jansen     if (context->idl_enable) {
2971d2a9659SKenneth E. Jansen       const CeedScalar sigma         = jac_data[14 * Q + i];
298530ad8c4SKenneth E. Jansen       CeedScalar       damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
299530ad8c4SKenneth E. Jansen       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
3001d2a9659SKenneth E. Jansen       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
301530ad8c4SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
302530ad8c4SKenneth E. Jansen     }
303530ad8c4SKenneth E. Jansen 
3042b89d87eSLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
3052b89d87eSLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
3062b89d87eSLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
3073bd61617SKenneth E. Jansen     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, dstab);
3082b89d87eSLeila Ghaffari 
3092b730f8bSJeremy L Thompson     for (int j = 0; j < 5; j++) {
3102b730f8bSJeremy 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]);
3112b730f8bSJeremy L Thompson     }
312*f0b01153SJames Wright   }
313e334ad8fSJed Brown   return 0;
314e334ad8fSJed Brown }
31565dd5cafSJames Wright 
3162b730f8bSJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
317be91e165SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
3183d02368aSJames Wright }
3193d02368aSJames Wright 
3202b730f8bSJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
321be91e165SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
3223d02368aSJames Wright }
3233d02368aSJames Wright 
3242b89d87eSLeila Ghaffari // *****************************************************************************
32565dd5cafSJames Wright // Compute boundary integral (ie. for strongly set inflows)
3262b89d87eSLeila Ghaffari // *****************************************************************************
327be91e165SJames Wright CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
328f21e6b1cSJames Wright   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
32946603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]       = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3309b6a821dSJames Wright   const CeedScalar(*Grad_q)              = in[1];
331f3e15844SJames Wright   const CeedScalar(*q_data_sur)          = in[2];
33246603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]             = (CeedScalar(*)[CEED_Q_VLA])out[0];
333f21e6b1cSJames Wright   CeedScalar(*jac_data_sur)              = context->is_implicit ? out[1] : NULL;
33465dd5cafSJames Wright 
3352c4e60d7SJames Wright   const bool is_implicit = context->is_implicit;
33665dd5cafSJames Wright 
3372b730f8bSJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
338efe9d856SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
3393bd61617SKenneth E. Jansen     State            s     = StateFromQ(context, qi, state_var);
34065dd5cafSJames Wright 
341f3e15844SJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
342f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
343f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
34465dd5cafSJames Wright 
3452c4e60d7SJames Wright     State grad_s[3];
3463bd61617SKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
34765dd5cafSJames Wright 
3482c4e60d7SJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
349d08fcc28SJames Wright     KMStrainRate_State(grad_s, strain_rate);
3502c4e60d7SJames Wright     NewtonianStress(context, strain_rate, kmstress);
3512c4e60d7SJames Wright     KMUnpack(kmstress, stress);
3522c4e60d7SJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
3532c4e60d7SJames Wright 
3542c4e60d7SJames Wright     StateConservative F_inviscid[3];
3552c4e60d7SJames Wright     FluxInviscid(context, s, F_inviscid);
3562c4e60d7SJames Wright 
3575bce47c7SJames Wright     CeedScalar Flux[5];
3585bce47c7SJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, norm, Flux);
3592c4e60d7SJames Wright 
3605bce47c7SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
36165dd5cafSJames Wright 
362f21e6b1cSJames Wright     if (is_implicit) {
363f3e15844SJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
364f3e15844SJames Wright       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
36565dd5cafSJames Wright     }
366f21e6b1cSJames Wright   }
36765dd5cafSJames Wright   return 0;
36865dd5cafSJames Wright }
36965dd5cafSJames Wright 
3702b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
371be91e165SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
37220840d50SJames Wright }
37320840d50SJames Wright 
3742b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
375be91e165SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
37620840d50SJames Wright }
37720840d50SJames Wright 
3782b89d87eSLeila Ghaffari // *****************************************************************************
379b55ac660SJames Wright // Jacobian for "set nothing" boundary integral
3802b89d87eSLeila Ghaffari // *****************************************************************************
3812b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
382be91e165SJames Wright                                                     StateVariable state_var) {
38346603fc5SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3849b6a821dSJames Wright   const CeedScalar(*Grad_dq)        = in[1];
385f3e15844SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
386c1d93bc4SKenneth E. Jansen   const CeedScalar(*jac_data_sur)   = in[4];
387b55ac660SJames Wright   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
388b55ac660SJames Wright 
389b55ac660SJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
390f3e15844SJames Wright   const bool                     is_implicit = context->is_implicit;
391b55ac660SJames Wright 
39246603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
393f3e15844SJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
394f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
395f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
396b55ac660SJames Wright 
3973bd61617SKenneth E. Jansen     CeedScalar qi[5], kmstress[6], dqi[5];
398f3e15844SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
399f3e15844SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
400efe9d856SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
40157e55a1cSJames Wright 
4023bd61617SKenneth E. Jansen     State s  = StateFromQ(context, qi, state_var);
4033bd61617SKenneth E. Jansen     State ds = StateFromQ_fwd(context, s, dqi, state_var);
404b55ac660SJames Wright 
405b55ac660SJames Wright     State grad_ds[3];
4063bd61617SKenneth E. Jansen     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
407b55ac660SJames Wright 
408b55ac660SJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
409d08fcc28SJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
410b55ac660SJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
411b55ac660SJames Wright     KMUnpack(dkmstress, dstress);
412b55ac660SJames Wright     KMUnpack(kmstress, stress);
413b55ac660SJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
414b55ac660SJames Wright 
415b55ac660SJames Wright     StateConservative dF_inviscid[3];
416b55ac660SJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
417b55ac660SJames Wright 
4185bce47c7SJames Wright     CeedScalar dFlux[5];
4195bce47c7SJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, norm, dFlux);
420b55ac660SJames Wright 
4215bce47c7SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
4224c0e8230SJames Wright   }
423b55ac660SJames Wright   return 0;
424b55ac660SJames Wright }
425b55ac660SJames Wright 
4262b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
427be91e165SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
42820840d50SJames Wright }
42920840d50SJames Wright 
4302b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
431be91e165SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
43220840d50SJames Wright }
433