xref: /libCEED/examples/fluids/qfunctions/newtonian.h (revision f3e15844553774b7483e8b688026466147268b6c)
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];
11446603fc5SJames Wright   const CeedScalar(*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
115*f3e15844SJames 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++) {
129*f3e15844SJames Wright     CeedScalar U[5], wdetJ, dXdx[3][3];
1305c677226SJed Brown     for (int j = 0; j < 5; j++) U[j] = q[j][i];
131*f3e15844SJames Wright     StoredValuesUnpack(Q, i, 0, 1, q_data, &wdetJ);
132*f3e15844SJames 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];
1377b69c783SJames Wright     for (CeedInt k = 0; k < 3; k++) {
1386f00d0e6SJed Brown       CeedScalar dx_i[3] = {0}, dU[5];
1397b69c783SJames Wright       for (CeedInt j = 0; j < 5; j++) dU[j] = Grad_q[0][j][i] * dXdx[0][k] + Grad_q[1][j][i] * dXdx[1][k] + Grad_q[2][j][i] * dXdx[2][k];
1407b69c783SJames Wright       dx_i[k]   = 1.;
1417b69c783SJames Wright       grad_s[k] = StateFromU_fwd(context, s, dU, x_i, dx_i);
1425c677226SJed Brown     }
1435c677226SJed Brown 
1445c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
145d08fcc28SJames Wright     KMStrainRate_State(grad_s, strain_rate);
1465c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
1475c677226SJed Brown     KMUnpack(kmstress, stress);
1485c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
1495c677226SJed Brown 
1505c677226SJed Brown     StateConservative F_inviscid[3];
1515c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
1525c677226SJed Brown 
1535c677226SJed Brown     // Total flux
1545c677226SJed Brown     CeedScalar Flux[5][3];
1552b89d87eSLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
1565c677226SJed Brown 
1577b69c783SJames Wright     for (CeedInt j = 0; j < 5; j++) {
1587b69c783SJames 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]);
1592b730f8bSJeremy L Thompson     }
1605c677226SJed Brown 
1615c677226SJed Brown     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], 0};
1622b730f8bSJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
16388b783a1SJames Wright 
1642b89d87eSLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
1652b89d87eSLeila Ghaffari     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0};
1662b89d87eSLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
1672b89d87eSLeila Ghaffari     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, x_i, stab);
16888b783a1SJames Wright 
1692b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
1702b730f8bSJeremy 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]);
1712b730f8bSJeremy L Thompson     }
17288b783a1SJames Wright   }  // End Quadrature Point Loop
17388b783a1SJames Wright 
17488b783a1SJames Wright   // Return
17588b783a1SJames Wright   return 0;
17688b783a1SJames Wright }
17788b783a1SJames Wright 
17888b783a1SJames Wright // *****************************************************************************
179ea61e9acSJeremy L Thompson // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
18088b783a1SJames Wright //
18188b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
18288b783a1SJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
183ea61e9acSJeremy L Thompson //                                       (diffusive terms will be added later)
18488b783a1SJames Wright // *****************************************************************************
185be91e165SJames Wright CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
18688b783a1SJames Wright   // Inputs
18746603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
18846603fc5SJames Wright   const CeedScalar(*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
18946603fc5SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[2];
190*f3e15844SJames Wright   const CeedScalar(*q_data)                = in[3];
19146603fc5SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[4];
19246603fc5SJames Wright 
19388b783a1SJames Wright   // Outputs
19446603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
19546603fc5SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
196*f3e15844SJames Wright   CeedScalar(*jac_data)              = out[2];
19746603fc5SJames Wright 
19888b783a1SJames Wright   // Context
19988b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
20088626eedSJames Wright   const CeedScalar        *g       = context->g;
20188626eedSJames Wright   const CeedScalar         dt      = context->dt;
202530ad8c4SKenneth E. Jansen   const CeedScalar         P0      = context->P0;
20388b783a1SJames Wright 
20488b783a1SJames Wright   // Quadrature Point Loop
20546603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
20646603fc5SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
2075c677226SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
208be91e165SJames Wright     const State      s      = StateFromQ(context, qi, x_i, state_var);
2095c677226SJed Brown 
210*f3e15844SJames Wright     CeedScalar wdetJ, dXdx[3][3];
211*f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
2125c677226SJed Brown     State grad_s[3];
2137b69c783SJames Wright     for (CeedInt k = 0; k < 3; k++) {
2143d02368aSJames Wright       CeedScalar dx_i[3] = {0}, dqi[5];
2157b69c783SJames Wright       for (CeedInt j = 0; j < 5; j++) {
2167b69c783SJames Wright         dqi[j] = Grad_q[0][j][i] * dXdx[0][k] + Grad_q[1][j][i] * dXdx[1][k] + Grad_q[2][j][i] * dXdx[2][k];
21746603fc5SJames Wright       }
2187b69c783SJames Wright       dx_i[k]   = 1.;
2197b69c783SJames Wright       grad_s[k] = StateFromQ_fwd(context, s, dqi, x_i, dx_i, state_var);
22088b783a1SJames Wright     }
2215c677226SJed Brown 
2225c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
223d08fcc28SJames Wright     KMStrainRate_State(grad_s, strain_rate);
2245c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
2255c677226SJed Brown     KMUnpack(kmstress, stress);
2265c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
2275c677226SJed Brown 
2285c677226SJed Brown     StateConservative F_inviscid[3];
2295c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
2305c677226SJed Brown 
2315c677226SJed Brown     // Total flux
2325c677226SJed Brown     CeedScalar Flux[5][3];
2332b89d87eSLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
2345c677226SJed Brown 
2357b69c783SJames Wright     for (CeedInt j = 0; j < 5; j++) {
2367b69c783SJames Wright       for (CeedInt k = 0; k < 3; k++) {
2377b69c783SJames 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]);
23846603fc5SJames Wright       }
2392b730f8bSJeremy L Thompson     }
2405c677226SJed Brown 
2415c677226SJed Brown     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], 0};
24288b783a1SJames Wright 
2432b89d87eSLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
2443d02368aSJames Wright     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5], dx0[3] = {0};
2453d02368aSJames Wright     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
246be91e165SJames Wright     State s_dot = StateFromQ_fwd(context, s, qi_dot, x_i, dx0, state_var);
2473d02368aSJames Wright     UnpackState_U(s_dot.U, U_dot);
2483d02368aSJames Wright 
2492b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
250530ad8c4SKenneth E. Jansen     if (context->idl_enable) {
251530ad8c4SKenneth E. Jansen       CeedScalar damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
252530ad8c4SKenneth E. Jansen       InternalDampingLayer(context, s, x_i, damp_state, idl_residual);
253530ad8c4SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
254530ad8c4SKenneth E. Jansen     }
255530ad8c4SKenneth E. Jansen 
2562b89d87eSLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
2572b89d87eSLeila Ghaffari     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, x_i, stab);
25888b783a1SJames Wright 
2592b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
26046603fc5SJames Wright       for (CeedInt k = 0; k < 3; k++) {
26146603fc5SJames 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]);
26246603fc5SJames Wright       }
2632b730f8bSJeremy L Thompson     }
264*f3e15844SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
265*f3e15844SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
266*f3e15844SJames Wright     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
26788b783a1SJames Wright 
26888b783a1SJames Wright   }  // End Quadrature Point Loop
26988b783a1SJames Wright 
27088b783a1SJames Wright   // Return
27188b783a1SJames Wright   return 0;
27288b783a1SJames Wright }
273e334ad8fSJed Brown 
2742b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
275be91e165SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
2763d02368aSJames Wright }
2773d02368aSJames Wright 
2782b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
279be91e165SJames Wright   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
2803d02368aSJames Wright }
2813d02368aSJames Wright 
282dc805cc4SLeila Ghaffari // *****************************************************************************
283ea61e9acSJeremy L Thompson // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
284dc805cc4SLeila Ghaffari // *****************************************************************************
285be91e165SJames Wright CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
286e334ad8fSJed Brown   // Inputs
28746603fc5SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
28846603fc5SJames Wright   const CeedScalar(*Grad_dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
289*f3e15844SJames Wright   const CeedScalar(*q_data)                 = in[2];
29046603fc5SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
291*f3e15844SJames Wright   const CeedScalar(*jac_data)               = in[4];
29246603fc5SJames Wright 
293e334ad8fSJed Brown   // Outputs
29446603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
29546603fc5SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
29646603fc5SJames Wright 
297e334ad8fSJed Brown   // Context
298e334ad8fSJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
299e334ad8fSJed Brown   const CeedScalar        *g       = context->g;
300e334ad8fSJed Brown 
301e334ad8fSJed Brown   // Quadrature Point Loop
30246603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
303*f3e15844SJames Wright     CeedScalar wdetJ, dXdx[3][3];
304*f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
305e334ad8fSJed Brown 
306c98a0616SJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
307*f3e15844SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
308*f3e15844SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
309*f3e15844SJames Wright     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
310e334ad8fSJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
311be91e165SJames Wright     State            s      = StateFromQ(context, qi, x_i, state_var);
312e334ad8fSJed Brown 
3133d02368aSJames Wright     CeedScalar dqi[5], dx0[3] = {0};
3143d02368aSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
315be91e165SJames Wright     State ds = StateFromQ_fwd(context, s, dqi, x_i, dx0, state_var);
316e334ad8fSJed Brown 
317e334ad8fSJed Brown     State grad_ds[3];
3187b69c783SJames Wright     for (int k = 0; k < 3; k++) {
3193d02368aSJames Wright       CeedScalar dqi_j[5];
3207b69c783SJames Wright       for (int j = 0; j < 5; j++) dqi_j[j] = Grad_dq[0][j][i] * dXdx[0][k] + Grad_dq[1][j][i] * dXdx[1][k] + Grad_dq[2][j][i] * dXdx[2][k];
3217b69c783SJames Wright       grad_ds[k] = StateFromQ_fwd(context, s, dqi_j, x_i, dx0, state_var);
322e334ad8fSJed Brown     }
323e334ad8fSJed Brown 
324e334ad8fSJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
325d08fcc28SJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
326e334ad8fSJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
327e334ad8fSJed Brown     KMUnpack(dkmstress, dstress);
328e334ad8fSJed Brown     KMUnpack(kmstress, stress);
329e334ad8fSJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
330e334ad8fSJed Brown 
331e334ad8fSJed Brown     StateConservative dF_inviscid[3];
332e334ad8fSJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
333e334ad8fSJed Brown 
334e334ad8fSJed Brown     // Total flux
335e334ad8fSJed Brown     CeedScalar dFlux[5][3];
3362b89d87eSLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
337e334ad8fSJed Brown 
33851b00d91SJames Wright     for (int j = 0; j < 5; j++) {
33951b00d91SJames 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]);
3402b730f8bSJeremy L Thompson     }
341e334ad8fSJed Brown 
342e334ad8fSJed Brown     const CeedScalar dbody_force[5] = {0, ds.U.density * g[0], ds.U.density * g[1], ds.U.density * g[2], 0};
3433d02368aSJames Wright     CeedScalar       dU[5]          = {0.};
3443d02368aSJames Wright     UnpackState_U(ds.U, dU);
3452b730f8bSJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
346e334ad8fSJed Brown 
347530ad8c4SKenneth E. Jansen     if (context->idl_enable) {
348530ad8c4SKenneth E. Jansen       CeedScalar damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
349530ad8c4SKenneth E. Jansen       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
350530ad8c4SKenneth E. Jansen       InternalDampingLayer(context, s, x_i, damp_state, idl_residual);
351530ad8c4SKenneth E. Jansen       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
352530ad8c4SKenneth E. Jansen     }
353530ad8c4SKenneth E. Jansen 
3542b89d87eSLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
3552b89d87eSLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
3562b89d87eSLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
3572b89d87eSLeila Ghaffari     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, x_i, dstab);
3582b89d87eSLeila Ghaffari 
3592b730f8bSJeremy L Thompson     for (int j = 0; j < 5; j++) {
3602b730f8bSJeremy 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]);
3612b730f8bSJeremy L Thompson     }
362e334ad8fSJed Brown   }  // End Quadrature Point Loop
363e334ad8fSJed Brown   return 0;
364e334ad8fSJed Brown }
36565dd5cafSJames Wright 
3662b730f8bSJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
367be91e165SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
3683d02368aSJames Wright }
3693d02368aSJames Wright 
3702b730f8bSJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
371be91e165SJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
3723d02368aSJames Wright }
3733d02368aSJames Wright 
3742b89d87eSLeila Ghaffari // *****************************************************************************
37565dd5cafSJames Wright // Compute boundary integral (ie. for strongly set inflows)
3762b89d87eSLeila Ghaffari // *****************************************************************************
377be91e165SJames Wright CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
37846603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
37946603fc5SJames Wright   const CeedScalar(*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
380*f3e15844SJames Wright   const CeedScalar(*q_data_sur)            = in[2];
38146603fc5SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[3];
38265dd5cafSJames Wright 
38346603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
384*f3e15844SJames Wright   CeedScalar(*jac_data_sur)  = out[1];
38565dd5cafSJames Wright 
3862c4e60d7SJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
3872c4e60d7SJames Wright   const bool                     is_implicit = context->is_implicit;
38865dd5cafSJames Wright 
3892b730f8bSJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
3902c4e60d7SJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
391efe9d856SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
392be91e165SJames Wright     State            s      = StateFromQ(context, qi, x_i, state_var);
39365dd5cafSJames Wright 
394*f3e15844SJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
395*f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
396*f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
39765dd5cafSJames Wright 
3982c4e60d7SJames Wright     State grad_s[3];
3997b69c783SJames Wright     for (CeedInt k = 0; k < 3; k++) {
400efe9d856SJames Wright       CeedScalar dx_i[3] = {0}, dqi[5];
4017b69c783SJames Wright       for (CeedInt j = 0; j < 5; j++) dqi[j] = Grad_q[0][j][i] * dXdx[0][k] + Grad_q[1][j][i] * dXdx[1][k];
4027b69c783SJames Wright       dx_i[k]   = 1.;
4037b69c783SJames Wright       grad_s[k] = StateFromQ_fwd(context, s, dqi, x_i, dx_i, state_var);
4042c4e60d7SJames Wright     }
40565dd5cafSJames Wright 
4062c4e60d7SJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
407d08fcc28SJames Wright     KMStrainRate_State(grad_s, strain_rate);
4082c4e60d7SJames Wright     NewtonianStress(context, strain_rate, kmstress);
4092c4e60d7SJames Wright     KMUnpack(kmstress, stress);
4102c4e60d7SJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
4112c4e60d7SJames Wright 
4122c4e60d7SJames Wright     StateConservative F_inviscid[3];
4132c4e60d7SJames Wright     FluxInviscid(context, s, F_inviscid);
4142c4e60d7SJames Wright 
4155bce47c7SJames Wright     CeedScalar Flux[5];
4165bce47c7SJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, norm, Flux);
4172c4e60d7SJames Wright 
4185bce47c7SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
41965dd5cafSJames Wright 
420*f3e15844SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
421*f3e15844SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
42265dd5cafSJames Wright   }
42365dd5cafSJames Wright   return 0;
42465dd5cafSJames Wright }
42565dd5cafSJames Wright 
4262b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
427be91e165SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
42820840d50SJames Wright }
42920840d50SJames Wright 
4302b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
431be91e165SJames Wright   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
43220840d50SJames Wright }
43320840d50SJames Wright 
4342b89d87eSLeila Ghaffari // *****************************************************************************
435b55ac660SJames Wright // Jacobian for "set nothing" boundary integral
4362b89d87eSLeila Ghaffari // *****************************************************************************
4372b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
438be91e165SJames Wright                                                     StateVariable state_var) {
439b55ac660SJames Wright   // Inputs
44046603fc5SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
44146603fc5SJames Wright   const CeedScalar(*Grad_dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
442*f3e15844SJames Wright   const CeedScalar(*q_data_sur)             = in[2];
44346603fc5SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
444*f3e15844SJames Wright   const CeedScalar(*jac_data_sur)           = in[4];
44546603fc5SJames Wright 
446b55ac660SJames Wright   // Outputs
447b55ac660SJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
448b55ac660SJames Wright 
449b55ac660SJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
450*f3e15844SJames Wright   const bool                     is_implicit = context->is_implicit;
451b55ac660SJames Wright 
452b55ac660SJames Wright   // Quadrature Point Loop
45346603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
454b55ac660SJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
455*f3e15844SJames Wright     CeedScalar       wdetJb, dXdx[2][3], norm[3];
456*f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
457*f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
458b55ac660SJames Wright 
459efe9d856SJames Wright     CeedScalar qi[5], kmstress[6], dqi[5], dx_i[3] = {0.};
460*f3e15844SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
461*f3e15844SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
462efe9d856SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
46357e55a1cSJames Wright 
464be91e165SJames Wright     State s  = StateFromQ(context, qi, x_i, state_var);
465be91e165SJames Wright     State ds = StateFromQ_fwd(context, s, dqi, x_i, dx_i, state_var);
466b55ac660SJames Wright 
467b55ac660SJames Wright     State grad_ds[3];
4687b69c783SJames Wright     for (CeedInt k = 0; k < 3; k++) {
469efe9d856SJames Wright       CeedScalar dx_i[3] = {0}, dqi_j[5];
4707b69c783SJames Wright       for (CeedInt j = 0; j < 5; j++) dqi_j[j] = Grad_dq[0][j][i] * dXdx[0][k] + Grad_dq[1][j][i] * dXdx[1][k];
4717b69c783SJames Wright       dx_i[k]    = 1.;
4727b69c783SJames Wright       grad_ds[k] = StateFromQ_fwd(context, s, dqi_j, x_i, dx_i, state_var);
473b55ac660SJames Wright     }
474b55ac660SJames Wright 
475b55ac660SJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
476d08fcc28SJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
477b55ac660SJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
478b55ac660SJames Wright     KMUnpack(dkmstress, dstress);
479b55ac660SJames Wright     KMUnpack(kmstress, stress);
480b55ac660SJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
481b55ac660SJames Wright 
482b55ac660SJames Wright     StateConservative dF_inviscid[3];
483b55ac660SJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
484b55ac660SJames Wright 
4855bce47c7SJames Wright     CeedScalar dFlux[5];
4865bce47c7SJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, norm, dFlux);
487b55ac660SJames Wright 
4885bce47c7SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
489b55ac660SJames Wright   }  // End Quadrature Point Loop
490b55ac660SJames Wright   return 0;
491b55ac660SJames Wright }
492b55ac660SJames Wright 
4932b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
494be91e165SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
49520840d50SJames Wright }
49620840d50SJames Wright 
4972b730f8bSJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
498be91e165SJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
49920840d50SJames Wright }
50020840d50SJames Wright 
50188b783a1SJames Wright #endif  // newtonian_h
502