xref: /libCEED/examples/fluids/qfunctions/newtonian.h (revision 13fa47b256d7b8fa7dc04000fe86398448c8602c)
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 
1288b783a1SJames Wright #ifndef newtonian_h
1388b783a1SJames Wright #define newtonian_h
1488b783a1SJames Wright 
1588b783a1SJames Wright #include <math.h>
1688b783a1SJames Wright #include <ceed.h>
17841e4c73SJed Brown #include "newtonian_types.h"
18c6e8c570SJames Wright #include "newtonian_state.h"
19*13fa47b2SJames Wright #include "utils.h"
2088b783a1SJames Wright 
2188b783a1SJames Wright // *****************************************************************************
2288b783a1SJames Wright // Helper function for computing flux Jacobian
2388b783a1SJames Wright // *****************************************************************************
2488b783a1SJames Wright CEED_QFUNCTION_HELPER void computeFluxJacobian_NS(CeedScalar dF[3][5][5],
2588b783a1SJames Wright     const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
2688626eedSJames Wright     const CeedScalar gamma, const CeedScalar g[3], const CeedScalar x[3]) {
2788b783a1SJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square
2888626eedSJames Wright   CeedScalar e_potential = -(g[0]*x[0] + g[1]*x[1] + g[2]*x[2]);
2988b783a1SJames Wright   for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions
3088b783a1SJames Wright     for (CeedInt j=0; j<3; j++) { // Rows of each Jacobian matrix
3188626eedSJames Wright       dF[i][j+1][0] = ((i==j) ? ((gamma-1.)*(u_sq/2. - e_potential)) : 0.) -
3288626eedSJames Wright                       u[i]*u[j];
3388b783a1SJames Wright       for (CeedInt k=0; k<3; k++) { // Columns of each Jacobian matrix
3488b783a1SJames Wright         dF[i][0][k+1]   = ((i==k) ? 1. : 0.);
3588b783a1SJames Wright         dF[i][j+1][k+1] = ((j==k) ? u[i] : 0.) +
3688b783a1SJames Wright                           ((i==k) ? u[j] : 0.) -
3788b783a1SJames Wright                           ((i==j) ? u[k] : 0.) * (gamma-1.);
3888b783a1SJames Wright         dF[i][4][k+1]   = ((i==k) ? (E*gamma/rho - (gamma-1.)*u_sq/2.) : 0.) -
3988b783a1SJames Wright                           (gamma-1.)*u[i]*u[k];
4088b783a1SJames Wright       }
4188b783a1SJames Wright       dF[i][j+1][4] = ((i==j) ? (gamma-1.) : 0.);
4288b783a1SJames Wright     }
4388b783a1SJames Wright     dF[i][4][0] = u[i] * ((gamma-1.)*u_sq - E*gamma/rho);
4488b783a1SJames Wright     dF[i][4][4] = u[i] * gamma;
4588b783a1SJames Wright   }
4688b783a1SJames Wright }
4788b783a1SJames Wright 
4888b783a1SJames Wright // *****************************************************************************
4988626eedSJames Wright // Helper function for computing flux Jacobian of Primitive variables
5088626eedSJames Wright // *****************************************************************************
5188626eedSJames Wright CEED_QFUNCTION_HELPER void computeFluxJacobian_NSp(CeedScalar dF[3][5][5],
5288626eedSJames Wright     const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
5388626eedSJames Wright     const CeedScalar Rd, const CeedScalar cv) {
5488626eedSJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square
5588626eedSJames Wright   // TODO Add in gravity's contribution
5688626eedSJames Wright 
5788626eedSJames Wright   CeedScalar T    = ( E / rho - u_sq / 2. ) / cv;
5888626eedSJames Wright   CeedScalar drdT = -rho / T;
5988626eedSJames Wright   CeedScalar drdP = 1. / ( Rd * T);
6088626eedSJames Wright   CeedScalar etot =  E / rho ;
6188626eedSJames Wright   CeedScalar e2p  = drdP * etot + 1. ;
6288626eedSJames Wright   CeedScalar e3p  = ( E  + rho * Rd * T );
6388626eedSJames Wright   CeedScalar e4p  = drdT * etot + rho * cv ;
6488626eedSJames Wright 
6588626eedSJames Wright   for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions
6688626eedSJames Wright     for (CeedInt j=0; j<3; j++) { // j counts F^{m_j}
6788626eedSJames Wright //        [row][col] of A_i
6888626eedSJames Wright       dF[i][j+1][0] = drdP * u[i] * u[j] + ((i==j) ? 1. : 0.); // F^{{m_j} wrt p
6988626eedSJames Wright       for (CeedInt k=0; k<3; k++) { // k counts the wrt vel_k
70871db79fSKenneth E. Jansen         dF[i][0][k+1]   =  ((i==k) ? rho  : 0.);   // F^c wrt u_k
7188626eedSJames Wright         dF[i][j+1][k+1] = (((j==k) ? u[i] : 0.) +  // F^m_j wrt u_k
7288626eedSJames Wright                            ((i==k) ? u[j] : 0.) ) * rho;
7388626eedSJames Wright         dF[i][4][k+1]   = rho * u[i] * u[k]
7488626eedSJames Wright                           + ((i==k) ? e3p  : 0.) ; // F^e wrt u_k
7588626eedSJames Wright       }
7688626eedSJames Wright       dF[i][j+1][4] = drdT * u[i] * u[j]; // F^{m_j} wrt T
7788626eedSJames Wright     }
7888626eedSJames Wright     dF[i][4][0] = u[i] * e2p; // F^e wrt p
7988626eedSJames Wright     dF[i][4][4] = u[i] * e4p; // F^e wrt T
8088626eedSJames Wright     dF[i][0][0] = u[i] * drdP; // F^c wrt p
8188626eedSJames Wright     dF[i][0][4] = u[i] * drdT; // F^c wrt T
8288626eedSJames Wright   }
8388626eedSJames Wright }
8488626eedSJames Wright 
8588626eedSJames Wright CEED_QFUNCTION_HELPER void PrimitiveToConservative_fwd(const CeedScalar rho,
8688626eedSJames Wright     const CeedScalar u[3], const CeedScalar E, const CeedScalar Rd,
8788626eedSJames Wright     const CeedScalar cv, const CeedScalar dY[5], CeedScalar dU[5]) {
8888626eedSJames Wright   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2];
8988626eedSJames Wright   CeedScalar T    = ( E / rho - u_sq / 2. ) / cv;
9088626eedSJames Wright   CeedScalar drdT = -rho / T;
9188626eedSJames Wright   CeedScalar drdP = 1. / ( Rd * T);
9288626eedSJames Wright   dU[0] = drdP * dY[0] + drdT * dY[4];
9388626eedSJames Wright   CeedScalar de_kinetic = 0;
94ba6664aeSJames Wright   for (CeedInt i=0; i<3; i++) {
9588626eedSJames Wright     dU[1+i] = dU[0] * u[i] + rho * dY[1+i];
9688626eedSJames Wright     de_kinetic += u[i] * dY[1+i];
9788626eedSJames Wright   }
9888626eedSJames Wright   dU[4] = rho * cv * dY[4] + dU[0] * cv * T // internal energy: rho * e
9988626eedSJames Wright           + rho * de_kinetic + .5 * dU[0] * u_sq; // kinetic energy: .5 * rho * |u|^2
10088626eedSJames Wright }
10188626eedSJames Wright 
10288626eedSJames Wright // *****************************************************************************
10388626eedSJames Wright // Helper function for computing Tau elements (stabilization constant)
10488626eedSJames Wright //   Model from:
10588626eedSJames Wright //     PHASTA
10688626eedSJames Wright //
10788626eedSJames Wright //   Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial)
10888626eedSJames Wright //
10988626eedSJames Wright // Where NOT UPDATED YET
11088626eedSJames Wright // *****************************************************************************
11188626eedSJames Wright CEED_QFUNCTION_HELPER void Tau_diagPrim(CeedScalar Tau_d[3],
11288626eedSJames Wright                                         const CeedScalar dXdx[3][3], const CeedScalar u[3],
11388626eedSJames Wright                                         const CeedScalar cv, const NewtonianIdealGasContext newt_ctx,
11488626eedSJames Wright                                         const CeedScalar mu, const CeedScalar dt,
11588626eedSJames Wright                                         const CeedScalar rho) {
11688626eedSJames Wright   // Context
11788626eedSJames Wright   const CeedScalar Ctau_t = newt_ctx->Ctau_t;
11888626eedSJames Wright   const CeedScalar Ctau_v = newt_ctx->Ctau_v;
11988626eedSJames Wright   const CeedScalar Ctau_C = newt_ctx->Ctau_C;
12088626eedSJames Wright   const CeedScalar Ctau_M = newt_ctx->Ctau_M;
12188626eedSJames Wright   const CeedScalar Ctau_E = newt_ctx->Ctau_E;
12288626eedSJames Wright   CeedScalar gijd[6];
12388626eedSJames Wright   CeedScalar tau;
12488626eedSJames Wright   CeedScalar dts;
12588626eedSJames Wright   CeedScalar fact;
12688626eedSJames Wright 
12788626eedSJames Wright   //*INDENT-OFF*
12888626eedSJames Wright   gijd[0] =   dXdx[0][0] * dXdx[0][0]
12988626eedSJames Wright             + dXdx[1][0] * dXdx[1][0]
13088626eedSJames Wright             + dXdx[2][0] * dXdx[2][0];
13188626eedSJames Wright 
13288626eedSJames Wright   gijd[1] =   dXdx[0][0] * dXdx[0][1]
13388626eedSJames Wright             + dXdx[1][0] * dXdx[1][1]
13488626eedSJames Wright             + dXdx[2][0] * dXdx[2][1];
13588626eedSJames Wright 
13688626eedSJames Wright   gijd[2] =   dXdx[0][1] * dXdx[0][1]
13788626eedSJames Wright             + dXdx[1][1] * dXdx[1][1]
13888626eedSJames Wright             + dXdx[2][1] * dXdx[2][1];
13988626eedSJames Wright 
14088626eedSJames Wright   gijd[3] =   dXdx[0][0] * dXdx[0][2]
14188626eedSJames Wright             + dXdx[1][0] * dXdx[1][2]
14288626eedSJames Wright             + dXdx[2][0] * dXdx[2][2];
14388626eedSJames Wright 
14488626eedSJames Wright   gijd[4] =   dXdx[0][1] * dXdx[0][2]
14588626eedSJames Wright             + dXdx[1][1] * dXdx[1][2]
14688626eedSJames Wright             + dXdx[2][1] * dXdx[2][2];
14788626eedSJames Wright 
14888626eedSJames Wright   gijd[5] =   dXdx[0][2] * dXdx[0][2]
14988626eedSJames Wright             + dXdx[1][2] * dXdx[1][2]
15088626eedSJames Wright             + dXdx[2][2] * dXdx[2][2];
15188626eedSJames Wright   //*INDENT-ON*
15288626eedSJames Wright 
15388626eedSJames Wright   dts = Ctau_t / dt ;
15488626eedSJames Wright 
15588626eedSJames Wright   tau = rho*rho*((4. * dts * dts)
15688626eedSJames Wright                  + u[0] * ( u[0] * gijd[0] + 2. * ( u[1] * gijd[1] + u[2] * gijd[3]))
15788626eedSJames Wright                  + u[1] * ( u[1] * gijd[2] + 2. *   u[2] * gijd[4])
15888626eedSJames Wright                  + u[2] *   u[2] * gijd[5])
15988626eedSJames Wright         + Ctau_v* mu * mu *
16088626eedSJames Wright         (gijd[0]*gijd[0] + gijd[2]*gijd[2] + gijd[5]*gijd[5] +
16188626eedSJames Wright          + 2. * (gijd[1]*gijd[1] + gijd[3]*gijd[3] + gijd[4]*gijd[4]));
16288626eedSJames Wright 
16388626eedSJames Wright   fact=sqrt(tau);
16488626eedSJames Wright 
16588626eedSJames Wright   Tau_d[0] = Ctau_C * fact / (rho*(gijd[0] + gijd[2] + gijd[5]))*0.125;
16688626eedSJames Wright 
16788626eedSJames Wright   Tau_d[1] = Ctau_M / fact;
16888626eedSJames Wright   Tau_d[2] = Ctau_E / ( fact * cv );
16988626eedSJames Wright 
17088626eedSJames Wright // consider putting back the way I initially had it  Ctau_E * Tau_d[1] /cv
17188626eedSJames Wright //  to avoid a division if the compiler is smart enough to see that cv IS
17288626eedSJames Wright // a constant that it could invert once for all elements
17388626eedSJames Wright // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M
17488626eedSJames Wright // OR we could absorb cv into Ctau_E but this puts more burden on user to
17588626eedSJames Wright // know how to change constants with a change of fluid or units.  Same for
17688626eedSJames Wright // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T)
17788626eedSJames Wright }
17888626eedSJames Wright 
17988626eedSJames Wright // *****************************************************************************
18088b783a1SJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
18188b783a1SJames Wright // *****************************************************************************
18288b783a1SJames Wright CEED_QFUNCTION(ICsNewtonianIG)(void *ctx, CeedInt Q,
18388b783a1SJames Wright                                const CeedScalar *const *in, CeedScalar *const *out) {
18488b783a1SJames Wright   // Inputs
18588b783a1SJames Wright   const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
18688b783a1SJames Wright 
18788b783a1SJames Wright   // Outputs
18888b783a1SJames Wright   CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
18988b783a1SJames Wright 
19088626eedSJames Wright   // Context
19188626eedSJames Wright   const SetupContext context = (SetupContext)ctx;
19288626eedSJames Wright   const CeedScalar theta0    = context->theta0;
19388626eedSJames Wright   const CeedScalar P0        = context->P0;
19488626eedSJames Wright   const CeedScalar cv        = context->cv;
19588626eedSJames Wright   const CeedScalar cp        = context->cp;
19688626eedSJames Wright   const CeedScalar *g        = context->g;
19788626eedSJames Wright   const CeedScalar Rd        = cp - cv;
19888626eedSJames Wright 
19988b783a1SJames Wright   // Quadrature Point Loop
20088b783a1SJames Wright   CeedPragmaSIMD
20188b783a1SJames Wright   for (CeedInt i=0; i<Q; i++) {
20288b783a1SJames Wright     CeedScalar q[5] = {0.};
20388b783a1SJames Wright 
20488b783a1SJames Wright     // Setup
20588b783a1SJames Wright     // -- Coordinates
20688626eedSJames Wright     const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
20788626eedSJames Wright     const CeedScalar e_potential = -(g[0]*x[0] + g[1]*x[1] + g[2]*x[2]);
20888b783a1SJames Wright 
20988b783a1SJames Wright     // -- Density
21088626eedSJames Wright     const CeedScalar rho = P0 / (Rd*theta0);
21188b783a1SJames Wright 
21288b783a1SJames Wright     // Initial Conditions
21388b783a1SJames Wright     q[0] = rho;
21488b783a1SJames Wright     q[1] = 0.0;
21588b783a1SJames Wright     q[2] = 0.0;
21688b783a1SJames Wright     q[3] = 0.0;
21788626eedSJames Wright     q[4] = rho * (cv*theta0 + e_potential);
21888b783a1SJames Wright 
21988b783a1SJames Wright     for (CeedInt j=0; j<5; j++)
22088b783a1SJames Wright       q0[j][i] = q[j];
22188b783a1SJames Wright   } // End of Quadrature Point Loop
22288b783a1SJames Wright   return 0;
22388b783a1SJames Wright }
22488b783a1SJames Wright 
22588b783a1SJames Wright // *****************************************************************************
22688b783a1SJames Wright // This QFunction implements the following formulation of Navier-Stokes with
22788b783a1SJames Wright //   explicit time stepping method
22888b783a1SJames Wright //
22988b783a1SJames Wright // This is 3D compressible Navier-Stokes in conservation form with state
23088b783a1SJames Wright //   variables of density, momentum density, and total energy density.
23188b783a1SJames Wright //
23288b783a1SJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
23388b783a1SJames Wright //   rho - Mass Density
23488b783a1SJames Wright //   Ui  - Momentum Density,      Ui = rho ui
23588b783a1SJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
23688b783a1SJames Wright //
23788b783a1SJames Wright // Navier-Stokes Equations:
23888b783a1SJames Wright //   drho/dt + div( U )                               = 0
23988b783a1SJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
24088b783a1SJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
24188b783a1SJames Wright //
24288b783a1SJames Wright // Viscous Stress:
24388b783a1SJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
24488b783a1SJames Wright //
24588b783a1SJames Wright // Thermal Stress:
24688b783a1SJames Wright //   Fe = u Fu + k grad( T )
24788626eedSJames Wright // Equation of State
24888b783a1SJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
24988b783a1SJames Wright //
25088b783a1SJames Wright // Stabilization:
25188b783a1SJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
25288b783a1SJames Wright //     f1 = rho  sqrt(ui uj gij)
25388b783a1SJames Wright //     gij = dXi/dX * dXi/dX
25488b783a1SJames Wright //     TauC = Cc f1 / (8 gii)
25588b783a1SJames Wright //     TauM = min( 1 , 1 / f1 )
25688b783a1SJames Wright //     TauE = TauM / (Ce cv)
25788b783a1SJames Wright //
25888b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
25988b783a1SJames Wright //
26088b783a1SJames Wright // Constants:
26188b783a1SJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
26288b783a1SJames Wright //   mu              ,  Dynamic viscosity
26388b783a1SJames Wright //   k               ,  Thermal conductivity
26488b783a1SJames Wright //   cv              ,  Specific heat, constant volume
26588b783a1SJames Wright //   cp              ,  Specific heat, constant pressure
26688b783a1SJames Wright //   g               ,  Gravity
26788b783a1SJames Wright //   gamma  = cp / cv,  Specific heat ratio
26888b783a1SJames Wright //
26988b783a1SJames Wright // We require the product of the inverse of the Jacobian (dXdx_j,k) and
27088b783a1SJames Wright // its transpose (dXdx_k,j) to properly compute integrals of the form:
27188b783a1SJames Wright // int( gradv gradu )
27288b783a1SJames Wright //
27388b783a1SJames Wright // *****************************************************************************
2745c677226SJed Brown CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q,
27588b783a1SJames Wright                                       const CeedScalar *const *in, CeedScalar *const *out) {
27688b783a1SJames Wright   // *INDENT-OFF*
27788b783a1SJames Wright   // Inputs
27888b783a1SJames Wright   const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
279a3ae0734SJed Brown                    (*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
28088b783a1SJames Wright                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
28188b783a1SJames Wright                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
28288b783a1SJames Wright   // Outputs
28388b783a1SJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
284a3ae0734SJed Brown              (*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
28588b783a1SJames Wright   // *INDENT-ON*
28688b783a1SJames Wright 
28788b783a1SJames Wright   // Context
28888b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
28988b783a1SJames Wright   const CeedScalar mu     = context->mu;
29088b783a1SJames Wright   const CeedScalar cv     = context->cv;
29188b783a1SJames Wright   const CeedScalar cp     = context->cp;
29288626eedSJames Wright   const CeedScalar *g     = context->g;
29388626eedSJames Wright   const CeedScalar dt     = context->dt;
29488b783a1SJames Wright   const CeedScalar gamma  = cp / cv;
29588626eedSJames Wright   const CeedScalar Rd     = cp - cv;
29688b783a1SJames Wright 
29788b783a1SJames Wright   CeedPragmaSIMD
29888b783a1SJames Wright   // Quadrature Point Loop
29988b783a1SJames Wright   for (CeedInt i=0; i<Q; i++) {
3005c677226SJed Brown     CeedScalar U[5];
3015c677226SJed Brown     for (int j=0; j<5; j++) U[j] = q[j][i];
3025c677226SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
3035c677226SJed Brown     State s = StateFromU(context, U, x_i);
3045c677226SJed Brown 
30588b783a1SJames Wright     // -- Interp-to-Interp q_data
30688b783a1SJames Wright     const CeedScalar wdetJ      =   q_data[0][i];
30788b783a1SJames Wright     // -- Interp-to-Grad q_data
30888b783a1SJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
30988b783a1SJames Wright     // *INDENT-OFF*
31088b783a1SJames Wright     const CeedScalar dXdx[3][3] = {{q_data[1][i],
31188b783a1SJames Wright                                     q_data[2][i],
31288b783a1SJames Wright                                     q_data[3][i]},
31388b783a1SJames Wright                                    {q_data[4][i],
31488b783a1SJames Wright                                     q_data[5][i],
31588b783a1SJames Wright                                     q_data[6][i]},
31688b783a1SJames Wright                                    {q_data[7][i],
31788b783a1SJames Wright                                     q_data[8][i],
31888b783a1SJames Wright                                     q_data[9][i]}
31988b783a1SJames Wright                                   };
32088b783a1SJames Wright     // *INDENT-ON*
32188b783a1SJames Wright 
3225c677226SJed Brown     State grad_s[3];
3233c4b7af6SJed Brown     for (CeedInt j=0; j<3; j++) {
3246f00d0e6SJed Brown       CeedScalar dx_i[3] = {0}, dU[5];
32539c69132SJed Brown       for (CeedInt k=0; k<5; k++)
32639c69132SJed Brown         dU[k] = Grad_q[0][k][i] * dXdx[0][j] +
32739c69132SJed Brown                 Grad_q[1][k][i] * dXdx[1][j] +
32839c69132SJed Brown                 Grad_q[2][k][i] * dXdx[2][j];
3295c677226SJed Brown       dx_i[j] = 1.;
3306f00d0e6SJed Brown       grad_s[j] = StateFromU_fwd(context, s, dU, x_i, dx_i);
3315c677226SJed Brown     }
3325c677226SJed Brown 
3335c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
3345c677226SJed Brown     KMStrainRate(grad_s, strain_rate);
3355c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
3365c677226SJed Brown     KMUnpack(kmstress, stress);
3375c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
3385c677226SJed Brown 
3395c677226SJed Brown     StateConservative F_inviscid[3];
3405c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
3415c677226SJed Brown 
3425c677226SJed Brown     // Total flux
3435c677226SJed Brown     CeedScalar Flux[5][3];
3443c4b7af6SJed Brown     for (CeedInt j=0; j<3; j++) {
3455c677226SJed Brown       Flux[0][j] = F_inviscid[j].density;
3463c4b7af6SJed Brown       for (CeedInt k=0; k<3; k++)
3475c677226SJed Brown         Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
3485c677226SJed Brown       Flux[4][j] = F_inviscid[j].E_total + Fe[j];
3495c677226SJed Brown     }
3505c677226SJed Brown 
3513c4b7af6SJed Brown     for (CeedInt j=0; j<3; j++) {
3523c4b7af6SJed Brown       for (CeedInt k=0; k<5; k++) {
353a3ae0734SJed Brown         Grad_v[j][k][i] = wdetJ * (dXdx[j][0] * Flux[k][0] +
3545c677226SJed Brown                                    dXdx[j][1] * Flux[k][1] +
3555c677226SJed Brown                                    dXdx[j][2] * Flux[k][2]);
3565c677226SJed Brown       }
3575c677226SJed Brown     }
3585c677226SJed Brown 
3595c677226SJed Brown     const CeedScalar body_force[5] = {0, s.U.density *g[0], s.U.density *g[1], s.U.density *g[2], 0};
3605c677226SJed Brown     for (int j=0; j<5; j++)
3615c677226SJed Brown       v[j][i] = wdetJ * body_force[j];
36288b783a1SJames Wright 
36388b783a1SJames Wright     // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
3645c677226SJed Brown     CeedScalar jacob_F_conv[3][5][5] = {0};
3655c677226SJed Brown     computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
3665c677226SJed Brown                            gamma, g, x_i);
3675c677226SJed Brown     CeedScalar grad_U[5][3];
368ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++) {
3695c677226SJed Brown       grad_U[0][j] = grad_s[j].U.density;
3703c4b7af6SJed Brown       for (CeedInt k=0; k<3; k++) grad_U[k+1][j] = grad_s[j].U.momentum[k];
3715c677226SJed Brown       grad_U[4][j] = grad_s[j].U.E_total;
37288b783a1SJames Wright     }
37388b783a1SJames Wright 
37488b783a1SJames Wright     // strong_conv = dF/dq * dq/dx    (Strong convection)
37588b783a1SJames Wright     CeedScalar strong_conv[5] = {0};
376ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++)
377ba6664aeSJames Wright       for (CeedInt k=0; k<5; k++)
378ba6664aeSJames Wright         for (CeedInt l=0; l<5; l++)
3795c677226SJed Brown           strong_conv[k] += jacob_F_conv[j][k][l] * grad_U[l][j];
38088b783a1SJames Wright 
38188626eedSJames Wright     // -- Stabilization method: none, SU, or SUPG
38288626eedSJames Wright     CeedScalar stab[5][3] = {{0.}};
38388626eedSJames Wright     CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0};
38488626eedSJames Wright     CeedScalar Tau_d[3] = {0.};
38588b783a1SJames Wright     switch (context->stabilization) {
38688b783a1SJames Wright     case STAB_NONE:        // Galerkin
38788b783a1SJames Wright       break;
38888b783a1SJames Wright     case STAB_SU:        // SU
3895c677226SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
39088626eedSJames Wright       tau_strong_conv[0] = Tau_d[0] * strong_conv[0];
39188626eedSJames Wright       tau_strong_conv[1] = Tau_d[1] * strong_conv[1];
39288626eedSJames Wright       tau_strong_conv[2] = Tau_d[1] * strong_conv[2];
39388626eedSJames Wright       tau_strong_conv[3] = Tau_d[1] * strong_conv[3];
39488626eedSJames Wright       tau_strong_conv[4] = Tau_d[2] * strong_conv[4];
3955c677226SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
3965c677226SJed Brown                                   tau_strong_conv,
39788626eedSJames Wright                                   tau_strong_conv_conservative);
398ba6664aeSJames Wright       for (CeedInt j=0; j<3; j++)
399ba6664aeSJames Wright         for (CeedInt k=0; k<5; k++)
400ba6664aeSJames Wright           for (CeedInt l=0; l<5; l++)
40188626eedSJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l];
40288b783a1SJames Wright 
403ba6664aeSJames Wright       for (CeedInt j=0; j<5; j++)
404ba6664aeSJames Wright         for (CeedInt k=0; k<3; k++)
405a3ae0734SJed Brown           Grad_v[k][j][i] -= wdetJ*(stab[j][0] * dXdx[k][0] +
40688b783a1SJames Wright                                     stab[j][1] * dXdx[k][1] +
40788b783a1SJames Wright                                     stab[j][2] * dXdx[k][2]);
40888b783a1SJames Wright       break;
40988b783a1SJames Wright     case STAB_SUPG:        // SUPG is not implemented for explicit scheme
41088b783a1SJames Wright       break;
41188b783a1SJames Wright     }
41288b783a1SJames Wright 
41388b783a1SJames Wright   } // End Quadrature Point Loop
41488b783a1SJames Wright 
41588b783a1SJames Wright   // Return
41688b783a1SJames Wright   return 0;
41788b783a1SJames Wright }
41888b783a1SJames Wright 
41988b783a1SJames Wright // *****************************************************************************
42088b783a1SJames Wright // This QFunction implements the Navier-Stokes equations (mentioned above) with
42188b783a1SJames Wright //   implicit time stepping method
42288b783a1SJames Wright //
42388b783a1SJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
42488b783a1SJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
42588b783a1SJames Wright //                                       (diffussive terms will be added later)
42688b783a1SJames Wright //
42788b783a1SJames Wright // *****************************************************************************
42888b783a1SJames Wright CEED_QFUNCTION(IFunction_Newtonian)(void *ctx, CeedInt Q,
42988b783a1SJames Wright                                     const CeedScalar *const *in,
43088b783a1SJames Wright                                     CeedScalar *const *out) {
43188b783a1SJames Wright   // *INDENT-OFF*
43288b783a1SJames Wright   // Inputs
43388b783a1SJames Wright   const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
434a3ae0734SJed Brown                    (*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
43588b783a1SJames Wright                    (*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
43688b783a1SJames Wright                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3],
43788b783a1SJames Wright                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
43888b783a1SJames Wright   // Outputs
43988b783a1SJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
440a3ae0734SJed Brown              (*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1],
441a3ae0734SJed Brown              (*jac_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[2];
44288b783a1SJames Wright   // *INDENT-ON*
44388b783a1SJames Wright   // Context
44488b783a1SJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
44588b783a1SJames Wright   const CeedScalar mu     = context->mu;
44688b783a1SJames Wright   const CeedScalar cv     = context->cv;
44788b783a1SJames Wright   const CeedScalar cp     = context->cp;
44888626eedSJames Wright   const CeedScalar *g     = context->g;
44988626eedSJames Wright   const CeedScalar dt     = context->dt;
45088b783a1SJames Wright   const CeedScalar gamma  = cp / cv;
45188626eedSJames Wright   const CeedScalar Rd     = cp-cv;
45288b783a1SJames Wright 
45388b783a1SJames Wright   CeedPragmaSIMD
45488b783a1SJames Wright   // Quadrature Point Loop
45588b783a1SJames Wright   for (CeedInt i=0; i<Q; i++) {
4565c677226SJed Brown     CeedScalar U[5];
4573c4b7af6SJed Brown     for (CeedInt j=0; j<5; j++) U[j] = q[j][i];
4585c677226SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
4595c677226SJed Brown     State s = StateFromU(context, U, x_i);
4605c677226SJed Brown 
46188b783a1SJames Wright     // -- Interp-to-Interp q_data
46288b783a1SJames Wright     const CeedScalar wdetJ      =   q_data[0][i];
46388b783a1SJames Wright     // -- Interp-to-Grad q_data
46488b783a1SJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
46588b783a1SJames Wright     // *INDENT-OFF*
46688b783a1SJames Wright     const CeedScalar dXdx[3][3] = {{q_data[1][i],
46788b783a1SJames Wright                                     q_data[2][i],
46888b783a1SJames Wright                                     q_data[3][i]},
46988b783a1SJames Wright                                    {q_data[4][i],
47088b783a1SJames Wright                                     q_data[5][i],
47188b783a1SJames Wright                                     q_data[6][i]},
47288b783a1SJames Wright                                    {q_data[7][i],
47388b783a1SJames Wright                                     q_data[8][i],
47488b783a1SJames Wright                                     q_data[9][i]}
47588b783a1SJames Wright                                   };
47688b783a1SJames Wright     // *INDENT-ON*
4775c677226SJed Brown     State grad_s[3];
478ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++) {
4796f00d0e6SJed Brown       CeedScalar dx_i[3] = {0}, dU[5];
48039c69132SJed Brown       for (CeedInt k=0; k<5; k++)
48139c69132SJed Brown         dU[k] = Grad_q[0][k][i] * dXdx[0][j] +
48239c69132SJed Brown                 Grad_q[1][k][i] * dXdx[1][j] +
48339c69132SJed Brown                 Grad_q[2][k][i] * dXdx[2][j];
4845c677226SJed Brown       dx_i[j] = 1.;
4856f00d0e6SJed Brown       grad_s[j] = StateFromU_fwd(context, s, dU, x_i, dx_i);
48688b783a1SJames Wright     }
4875c677226SJed Brown 
4885c677226SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
4895c677226SJed Brown     KMStrainRate(grad_s, strain_rate);
4905c677226SJed Brown     NewtonianStress(context, strain_rate, kmstress);
4915c677226SJed Brown     KMUnpack(kmstress, stress);
4925c677226SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
4935c677226SJed Brown 
4945c677226SJed Brown     StateConservative F_inviscid[3];
4955c677226SJed Brown     FluxInviscid(context, s, F_inviscid);
4965c677226SJed Brown 
4975c677226SJed Brown 
4985c677226SJed Brown     // Total flux
4995c677226SJed Brown     CeedScalar Flux[5][3];
5003c4b7af6SJed Brown     for (CeedInt j=0; j<3; j++) {
5015c677226SJed Brown       Flux[0][j] = F_inviscid[j].density;
502ba6664aeSJames Wright       for (CeedInt k=0; k<3; k++)
5035c677226SJed Brown         Flux[k+1][j] = F_inviscid[j].momentum[k] - stress[k][j];
5045c677226SJed Brown       Flux[4][j] = F_inviscid[j].E_total + Fe[j];
5055c677226SJed Brown     }
5065c677226SJed Brown 
5073c4b7af6SJed Brown     for (CeedInt j=0; j<3; j++) {
5083c4b7af6SJed Brown       for (CeedInt k=0; k<5; k++) {
509a3ae0734SJed Brown         Grad_v[j][k][i] = -wdetJ * (dXdx[j][0] * Flux[k][0] +
5105c677226SJed Brown                                     dXdx[j][1] * Flux[k][1] +
5115c677226SJed Brown                                     dXdx[j][2] * Flux[k][2]);
5125c677226SJed Brown       }
5135c677226SJed Brown     }
5145c677226SJed Brown 
5155c677226SJed Brown     const CeedScalar body_force[5] = {0, s.U.density *g[0], s.U.density *g[1], s.U.density *g[2], 0};
5163c4b7af6SJed Brown     for (CeedInt j=0; j<5; j++)
5175c677226SJed Brown       v[j][i] = wdetJ * (q_dot[j][i] - body_force[j]);
51888b783a1SJames Wright 
51988b783a1SJames Wright     // jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
5205c677226SJed Brown     CeedScalar jacob_F_conv[3][5][5] = {0};
5215c677226SJed Brown     computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
5225c677226SJed Brown                            gamma, g, x_i);
5235c677226SJed Brown     CeedScalar grad_U[5][3];
524ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++) {
5255c677226SJed Brown       grad_U[0][j] = grad_s[j].U.density;
5263c4b7af6SJed Brown       for (CeedInt k=0; k<3; k++) grad_U[k+1][j] = grad_s[j].U.momentum[k];
5275c677226SJed Brown       grad_U[4][j] = grad_s[j].U.E_total;
52888b783a1SJames Wright     }
5295c677226SJed Brown 
53088b783a1SJames Wright     // strong_conv = dF/dq * dq/dx    (Strong convection)
53188b783a1SJames Wright     CeedScalar strong_conv[5] = {0};
532ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++)
533ba6664aeSJames Wright       for (CeedInt k=0; k<5; k++)
534ba6664aeSJames Wright         for (CeedInt l=0; l<5; l++)
5355c677226SJed Brown           strong_conv[k] += jacob_F_conv[j][k][l] * grad_U[l][j];
53688b783a1SJames Wright 
53788b783a1SJames Wright     // Strong residual
53888b783a1SJames Wright     CeedScalar strong_res[5];
539ba6664aeSJames Wright     for (CeedInt j=0; j<5; j++)
54088b783a1SJames Wright       strong_res[j] = q_dot[j][i] + strong_conv[j] - body_force[j];
54188b783a1SJames Wright 
54288b783a1SJames Wright     // -- Stabilization method: none, SU, or SUPG
54388626eedSJames Wright     CeedScalar stab[5][3] = {{0.}};
54488626eedSJames Wright     CeedScalar tau_strong_res[5] = {0.}, tau_strong_res_conservative[5] = {0};
54588626eedSJames Wright     CeedScalar tau_strong_conv[5] = {0.}, tau_strong_conv_conservative[5] = {0};
54688626eedSJames Wright     CeedScalar Tau_d[3] = {0.};
54788b783a1SJames Wright     switch (context->stabilization) {
54888b783a1SJames Wright     case STAB_NONE:        // Galerkin
54988b783a1SJames Wright       break;
55088b783a1SJames Wright     case STAB_SU:        // SU
5515c677226SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
55288626eedSJames Wright       tau_strong_conv[0] = Tau_d[0] * strong_conv[0];
55388626eedSJames Wright       tau_strong_conv[1] = Tau_d[1] * strong_conv[1];
55488626eedSJames Wright       tau_strong_conv[2] = Tau_d[1] * strong_conv[2];
55588626eedSJames Wright       tau_strong_conv[3] = Tau_d[1] * strong_conv[3];
55688626eedSJames Wright       tau_strong_conv[4] = Tau_d[2] * strong_conv[4];
5575c677226SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
5585c677226SJed Brown                                   tau_strong_conv, tau_strong_conv_conservative);
559ba6664aeSJames Wright       for (CeedInt j=0; j<3; j++)
560ba6664aeSJames Wright         for (CeedInt k=0; k<5; k++)
561ba6664aeSJames Wright           for (CeedInt l=0; l<5; l++)
56288626eedSJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_conv_conservative[l];
56388b783a1SJames Wright 
564ba6664aeSJames Wright       for (CeedInt j=0; j<5; j++)
565ba6664aeSJames Wright         for (CeedInt k=0; k<3; k++)
566a3ae0734SJed Brown           Grad_v[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] +
56788b783a1SJames Wright                                     stab[j][1] * dXdx[k][1] +
56888b783a1SJames Wright                                     stab[j][2] * dXdx[k][2]);
5693c4b7af6SJed Brown 
57088b783a1SJames Wright       break;
57188b783a1SJames Wright     case STAB_SUPG:        // SUPG
5725c677226SJed Brown       Tau_diagPrim(Tau_d, dXdx, s.Y.velocity, cv, context, mu, dt, s.U.density);
57388626eedSJames Wright       tau_strong_res[0] = Tau_d[0] * strong_res[0];
57488626eedSJames Wright       tau_strong_res[1] = Tau_d[1] * strong_res[1];
57588626eedSJames Wright       tau_strong_res[2] = Tau_d[1] * strong_res[2];
57688626eedSJames Wright       tau_strong_res[3] = Tau_d[1] * strong_res[3];
57788626eedSJames Wright       tau_strong_res[4] = Tau_d[2] * strong_res[4];
57888626eedSJames Wright // Alternate route (useful later with primitive variable code)
57988626eedSJames Wright // this function was verified against PHASTA for as IC that was as close as possible
58088626eedSJames Wright //    computeFluxJacobian_NSp(jacob_F_conv_p, rho, u, E, Rd, cv);
58188626eedSJames Wright // it has also been verified to compute a correct through the following
58288626eedSJames Wright //   stab[k][j] += jacob_F_conv_p[j][k][l] * tau_strong_res[l] // flux Jacobian wrt primitive
58388626eedSJames Wright // applied in the triple loop below
58488626eedSJames Wright //  However, it is more flops than using the existing Jacobian wrt q after q_{,Y} viz
5855c677226SJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
5865c677226SJed Brown                                   tau_strong_res, tau_strong_res_conservative);
587ba6664aeSJames Wright       for (CeedInt j=0; j<3; j++)
588ba6664aeSJames Wright         for (CeedInt k=0; k<5; k++)
589ba6664aeSJames Wright           for (CeedInt l=0; l<5; l++)
59088626eedSJames Wright             stab[k][j] += jacob_F_conv[j][k][l] * tau_strong_res_conservative[l];
59188b783a1SJames Wright 
592ba6664aeSJames Wright       for (CeedInt j=0; j<5; j++)
593ba6664aeSJames Wright         for (CeedInt k=0; k<3; k++)
594a3ae0734SJed Brown           Grad_v[k][j][i] += wdetJ*(stab[j][0] * dXdx[k][0] +
59588b783a1SJames Wright                                     stab[j][1] * dXdx[k][1] +
59688b783a1SJames Wright                                     stab[j][2] * dXdx[k][2]);
59788b783a1SJames Wright       break;
59888b783a1SJames Wright     }
5993c4b7af6SJed Brown     for (CeedInt j=0; j<5; j++) jac_data[j][i] = U[j];
6003c4b7af6SJed Brown     for (CeedInt j=0; j<6; j++) jac_data[5+j][i] = kmstress[j];
6013c4b7af6SJed Brown     for (CeedInt j=0; j<3; j++) jac_data[5+6+j][i] = Tau_d[j];
60288b783a1SJames Wright 
60388b783a1SJames Wright   } // End Quadrature Point Loop
60488b783a1SJames Wright 
60588b783a1SJames Wright   // Return
60688b783a1SJames Wright   return 0;
60788b783a1SJames Wright }
608e334ad8fSJed Brown 
609e334ad8fSJed Brown CEED_QFUNCTION(IJacobian_Newtonian)(void *ctx, CeedInt Q,
610e334ad8fSJed Brown                                     const CeedScalar *const *in,
611e334ad8fSJed Brown                                     CeedScalar *const *out) {
612e334ad8fSJed Brown   // *INDENT-OFF*
613e334ad8fSJed Brown   // Inputs
614e334ad8fSJed Brown   const CeedScalar (*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
615e334ad8fSJed Brown                    (*Grad_dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
616e334ad8fSJed Brown                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
617e334ad8fSJed Brown                    (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3],
618e334ad8fSJed Brown                    (*jac_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
619e334ad8fSJed Brown   // Outputs
620e334ad8fSJed Brown   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
621e334ad8fSJed Brown              (*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
622e334ad8fSJed Brown   // *INDENT-ON*
623e334ad8fSJed Brown   // Context
624e334ad8fSJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
625e334ad8fSJed Brown   const CeedScalar *g = context->g;
626e334ad8fSJed Brown   const CeedScalar cp = context->cp;
627e334ad8fSJed Brown   const CeedScalar cv = context->cv;
628e334ad8fSJed Brown   const CeedScalar Rd = cp - cv;
629e334ad8fSJed Brown   const CeedScalar gamma = cp / cv;
630e334ad8fSJed Brown 
631e334ad8fSJed Brown   CeedPragmaSIMD
632e334ad8fSJed Brown   // Quadrature Point Loop
633e334ad8fSJed Brown   for (CeedInt i=0; i<Q; i++) {
634e334ad8fSJed Brown     // -- Interp-to-Interp q_data
635e334ad8fSJed Brown     const CeedScalar wdetJ      =   q_data[0][i];
636e334ad8fSJed Brown     // -- Interp-to-Grad q_data
637e334ad8fSJed Brown     // ---- Inverse of change of coordinate matrix: X_i,j
638e334ad8fSJed Brown     // *INDENT-OFF*
639e334ad8fSJed Brown     const CeedScalar dXdx[3][3] = {{q_data[1][i],
640e334ad8fSJed Brown                                     q_data[2][i],
641e334ad8fSJed Brown                                     q_data[3][i]},
642e334ad8fSJed Brown                                    {q_data[4][i],
643e334ad8fSJed Brown                                     q_data[5][i],
644e334ad8fSJed Brown                                     q_data[6][i]},
645e334ad8fSJed Brown                                    {q_data[7][i],
646e334ad8fSJed Brown                                     q_data[8][i],
647e334ad8fSJed Brown                                     q_data[9][i]}
648e334ad8fSJed Brown                                   };
649e334ad8fSJed Brown     // *INDENT-ON*
650e334ad8fSJed Brown 
651e334ad8fSJed Brown     CeedScalar U[5], kmstress[6], Tau_d[3] __attribute((unused));
652e334ad8fSJed Brown     for (int j=0; j<5; j++) U[j] = jac_data[j][i];
653e334ad8fSJed Brown     for (int j=0; j<6; j++) kmstress[j] = jac_data[5+j][i];
654e334ad8fSJed Brown     for (int j=0; j<3; j++) Tau_d[j] = jac_data[5+6+j][i];
655e334ad8fSJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
656e334ad8fSJed Brown     State s = StateFromU(context, U, x_i);
657e334ad8fSJed Brown 
658e334ad8fSJed Brown     CeedScalar dU[5], dx0[3] = {0};
659e334ad8fSJed Brown     for (int j=0; j<5; j++) dU[j] = dq[j][i];
660e334ad8fSJed Brown     State ds = StateFromU_fwd(context, s, dU, x_i, dx0);
661e334ad8fSJed Brown 
662e334ad8fSJed Brown     State grad_ds[3];
663e334ad8fSJed Brown     for (int j=0; j<3; j++) {
664e334ad8fSJed Brown       CeedScalar dUj[5];
665e334ad8fSJed Brown       for (int k=0; k<5; k++) dUj[k] = Grad_dq[0][k][i] * dXdx[0][j]
666e334ad8fSJed Brown                                          + Grad_dq[1][k][i] * dXdx[1][j]
667e334ad8fSJed Brown                                          + Grad_dq[2][k][i] * dXdx[2][j];
668e334ad8fSJed Brown       grad_ds[j] = StateFromU_fwd(context, s, dUj, x_i, dx0);
669e334ad8fSJed Brown     }
670e334ad8fSJed Brown 
671e334ad8fSJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
672e334ad8fSJed Brown     KMStrainRate(grad_ds, dstrain_rate);
673e334ad8fSJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
674e334ad8fSJed Brown     KMUnpack(dkmstress, dstress);
675e334ad8fSJed Brown     KMUnpack(kmstress, stress);
676e334ad8fSJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
677e334ad8fSJed Brown 
678e334ad8fSJed Brown     StateConservative dF_inviscid[3];
679e334ad8fSJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
680e334ad8fSJed Brown 
681e334ad8fSJed Brown     // Total flux
682e334ad8fSJed Brown     CeedScalar dFlux[5][3];
683e334ad8fSJed Brown     for (int j=0; j<3; j++) {
684e334ad8fSJed Brown       dFlux[0][j] = dF_inviscid[j].density;
685e334ad8fSJed Brown       for (int k=0; k<3; k++)
686e334ad8fSJed Brown         dFlux[k+1][j] = dF_inviscid[j].momentum[k] - dstress[k][j];
687e334ad8fSJed Brown       dFlux[4][j] = dF_inviscid[j].E_total + dFe[j];
688e334ad8fSJed Brown     }
689e334ad8fSJed Brown 
690e334ad8fSJed Brown     for (int j=0; j<3; j++) {
691e334ad8fSJed Brown       for (int k=0; k<5; k++) {
692e334ad8fSJed Brown         Grad_v[j][k][i] = -wdetJ * (dXdx[j][0] * dFlux[k][0] +
693e334ad8fSJed Brown                                     dXdx[j][1] * dFlux[k][1] +
694e334ad8fSJed Brown                                     dXdx[j][2] * dFlux[k][2]);
695e334ad8fSJed Brown       }
696e334ad8fSJed Brown     }
697e334ad8fSJed Brown 
698e334ad8fSJed Brown     const CeedScalar dbody_force[5] = {0, ds.U.density *g[0], ds.U.density *g[1], ds.U.density *g[2], 0};
699e334ad8fSJed Brown     for (int j=0; j<5; j++)
700e334ad8fSJed Brown       v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
701e334ad8fSJed Brown 
702e334ad8fSJed Brown     if (1) {
703e334ad8fSJed Brown       CeedScalar jacob_F_conv[3][5][5] = {0};
704e334ad8fSJed Brown       computeFluxJacobian_NS(jacob_F_conv, s.U.density, s.Y.velocity, s.U.E_total,
705e334ad8fSJed Brown                              gamma, g, x_i);
706e334ad8fSJed Brown       CeedScalar grad_dU[5][3];
707e334ad8fSJed Brown       for (int j=0; j<3; j++) {
708e334ad8fSJed Brown         grad_dU[0][j] = grad_ds[j].U.density;
709e334ad8fSJed Brown         for (int k=0; k<3; k++) grad_dU[k+1][j] = grad_ds[j].U.momentum[k];
710e334ad8fSJed Brown         grad_dU[4][j] = grad_ds[j].U.E_total;
711e334ad8fSJed Brown       }
712e334ad8fSJed Brown       CeedScalar dstrong_conv[5] = {0};
713e334ad8fSJed Brown       for (int j=0; j<3; j++)
714e334ad8fSJed Brown         for (int k=0; k<5; k++)
715e334ad8fSJed Brown           for (int l=0; l<5; l++)
716e334ad8fSJed Brown             dstrong_conv[k] += jacob_F_conv[j][k][l] * grad_dU[l][j];
717e334ad8fSJed Brown       CeedScalar dstrong_res[5];
718e334ad8fSJed Brown       for (int j=0; j<5; j++)
719e334ad8fSJed Brown         dstrong_res[j] = context->ijacobian_time_shift * dU[j] + dstrong_conv[j] -
720e334ad8fSJed Brown                          dbody_force[j];
721e334ad8fSJed Brown       CeedScalar dtau_strong_res[5] = {0.}, dtau_strong_res_conservative[5] = {0};
722e334ad8fSJed Brown       dtau_strong_res[0] = Tau_d[0] * dstrong_res[0];
723e334ad8fSJed Brown       dtau_strong_res[1] = Tau_d[1] * dstrong_res[1];
724e334ad8fSJed Brown       dtau_strong_res[2] = Tau_d[1] * dstrong_res[2];
725e334ad8fSJed Brown       dtau_strong_res[3] = Tau_d[1] * dstrong_res[3];
726e334ad8fSJed Brown       dtau_strong_res[4] = Tau_d[2] * dstrong_res[4];
727e334ad8fSJed Brown       PrimitiveToConservative_fwd(s.U.density, s.Y.velocity, s.U.E_total, Rd, cv,
728e334ad8fSJed Brown                                   dtau_strong_res, dtau_strong_res_conservative);
729e334ad8fSJed Brown       CeedScalar dstab[5][3] = {0};
730e334ad8fSJed Brown       for (int j=0; j<3; j++)
731e334ad8fSJed Brown         for (int k=0; k<5; k++)
732e334ad8fSJed Brown           for (int l=0; l<5; l++)
733e334ad8fSJed Brown             dstab[k][j] += jacob_F_conv[j][k][l] * dtau_strong_res_conservative[l];
734e334ad8fSJed Brown       for (int j=0; j<5; j++)
735e334ad8fSJed Brown         for (int k=0; k<3; k++)
736e334ad8fSJed Brown           Grad_v[k][j][i] += wdetJ*(dstab[j][0] * dXdx[k][0] +
737e334ad8fSJed Brown                                     dstab[j][1] * dXdx[k][1] +
738e334ad8fSJed Brown                                     dstab[j][2] * dXdx[k][2]);
739e334ad8fSJed Brown 
740e334ad8fSJed Brown     }
741e334ad8fSJed Brown   } // End Quadrature Point Loop
742e334ad8fSJed Brown   return 0;
743e334ad8fSJed Brown }
74465dd5cafSJames Wright 
74565dd5cafSJames Wright // Compute boundary integral (ie. for strongly set inflows)
74665dd5cafSJames Wright CEED_QFUNCTION(BoundaryIntegral)(void *ctx, CeedInt Q,
74765dd5cafSJames Wright                                  const CeedScalar *const *in,
74865dd5cafSJames Wright                                  CeedScalar *const *out) {
74965dd5cafSJames Wright 
75065dd5cafSJames Wright   //*INDENT-OFF*
75165dd5cafSJames Wright   const CeedScalar (*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0],
7522c4e60d7SJames Wright                    (*Grad_q)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
7532c4e60d7SJames Wright                    (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
7542c4e60d7SJames Wright                    (*x)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
75565dd5cafSJames Wright 
756b55ac660SJames Wright   CeedScalar (*v)[CEED_Q_VLA]            = (CeedScalar(*)[CEED_Q_VLA]) out[0],
757b55ac660SJames Wright              (*jac_data_sur)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA]) out[1];
75865dd5cafSJames Wright 
75965dd5cafSJames Wright   //*INDENT-ON*
76065dd5cafSJames Wright 
7612c4e60d7SJames Wright   const NewtonianIdealGasContext context = (NewtonianIdealGasContext) ctx;
7622c4e60d7SJames Wright   const bool is_implicit  = context->is_implicit;
76365dd5cafSJames Wright 
76465dd5cafSJames Wright   CeedPragmaSIMD
76565dd5cafSJames Wright   for(CeedInt i=0; i<Q; i++) {
7662c4e60d7SJames Wright     const CeedScalar U[5]   = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
7672c4e60d7SJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
7682c4e60d7SJames Wright     const State      s      = StateFromU(context, U, x_i);
76965dd5cafSJames Wright 
77065dd5cafSJames Wright     const CeedScalar wdetJb  = (is_implicit ? -1. : 1.) * q_data_sur[0][i];
77165dd5cafSJames Wright     // ---- Normal vect
77265dd5cafSJames Wright     const CeedScalar norm[3] = {q_data_sur[1][i],
77365dd5cafSJames Wright                                 q_data_sur[2][i],
77465dd5cafSJames Wright                                 q_data_sur[3][i]
77565dd5cafSJames Wright                                };
77665dd5cafSJames Wright 
7772c4e60d7SJames Wright     const CeedScalar dXdx[2][3] = {
7782c4e60d7SJames Wright       {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]},
7792c4e60d7SJames Wright       {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]}
7802c4e60d7SJames Wright     };
78165dd5cafSJames Wright 
7822c4e60d7SJames Wright     State grad_s[3];
7832c4e60d7SJames Wright     for (CeedInt j=0; j<3; j++) {
7842c4e60d7SJames Wright       CeedScalar dx_i[3] = {0}, dU[5];
7852c4e60d7SJames Wright       for (CeedInt k=0; k<5; k++)
7862c4e60d7SJames Wright         dU[k] = Grad_q[0][k][i] * dXdx[0][j] +
7872c4e60d7SJames Wright                 Grad_q[1][k][i] * dXdx[1][j];
7882c4e60d7SJames Wright       dx_i[j] = 1.;
7892c4e60d7SJames Wright       grad_s[j] = StateFromU_fwd(context, s, dU, x_i, dx_i);
7902c4e60d7SJames Wright     }
79165dd5cafSJames Wright 
7922c4e60d7SJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
7932c4e60d7SJames Wright     KMStrainRate(grad_s, strain_rate);
7942c4e60d7SJames Wright     NewtonianStress(context, strain_rate, kmstress);
7952c4e60d7SJames Wright     KMUnpack(kmstress, stress);
7962c4e60d7SJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
7972c4e60d7SJames Wright 
7982c4e60d7SJames Wright     StateConservative F_inviscid[3];
7992c4e60d7SJames Wright     FluxInviscid(context, s, F_inviscid);
8002c4e60d7SJames Wright 
8012c4e60d7SJames Wright     CeedScalar Flux[5] = {0.};
8022c4e60d7SJames Wright     for (int j=0; j<3; j++) {
8032c4e60d7SJames Wright       Flux[0] += F_inviscid[j].density * norm[j];
8042c4e60d7SJames Wright       for (int k=0; k<3; k++)
8052c4e60d7SJames Wright         Flux[k+1] += (F_inviscid[j].momentum[k] - stress[k][j]) * norm[j];
8062c4e60d7SJames Wright       Flux[4] += (F_inviscid[j].E_total + Fe[j])*norm[j];
8072c4e60d7SJames Wright     }
8082c4e60d7SJames Wright 
80965dd5cafSJames Wright     // -- Density
8102c4e60d7SJames Wright     v[0][i] = -wdetJb * Flux[0];
81165dd5cafSJames Wright 
81265dd5cafSJames Wright     // -- Momentum
81365dd5cafSJames Wright     for (CeedInt j=0; j<3; j++)
8142c4e60d7SJames Wright       v[j+1][i] = -wdetJb * Flux[j+1];
81565dd5cafSJames Wright 
81665dd5cafSJames Wright     // -- Total Energy Density
8172c4e60d7SJames Wright     v[4][i] = -wdetJb * Flux[4];
818b55ac660SJames Wright 
819b55ac660SJames Wright     jac_data_sur[0][i] = s.U.density;
820b55ac660SJames Wright     jac_data_sur[1][i] = s.Y.velocity[0];
821b55ac660SJames Wright     jac_data_sur[2][i] = s.Y.velocity[1];
822b55ac660SJames Wright     jac_data_sur[3][i] = s.Y.velocity[2];
823b55ac660SJames Wright     jac_data_sur[4][i] = s.U.E_total;
824b55ac660SJames Wright     for (int j=0; j<6; j++) jac_data_sur[5+j][i] = kmstress[j];
82565dd5cafSJames Wright   }
82665dd5cafSJames Wright   return 0;
82765dd5cafSJames Wright }
82865dd5cafSJames Wright 
829b55ac660SJames Wright // Jacobian for "set nothing" boundary integral
830b55ac660SJames Wright CEED_QFUNCTION(BoundaryIntegral_Jacobian)(void *ctx, CeedInt Q,
831b55ac660SJames Wright     const CeedScalar *const *in,
832b55ac660SJames Wright     CeedScalar *const *out) {
833b55ac660SJames Wright   // *INDENT-OFF*
834b55ac660SJames Wright   // Inputs
835b55ac660SJames Wright   const CeedScalar (*dq)[CEED_Q_VLA]           = (const CeedScalar(*)[CEED_Q_VLA])in[0],
836b55ac660SJames Wright                    (*Grad_dq)[5][CEED_Q_VLA]   = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
837b55ac660SJames Wright                    (*q_data_sur)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[2],
838b55ac660SJames Wright                    (*x)[CEED_Q_VLA]            = (const CeedScalar(*)[CEED_Q_VLA])in[3],
839b55ac660SJames Wright                    (*jac_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
840b55ac660SJames Wright   // Outputs
841b55ac660SJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
842b55ac660SJames Wright   // *INDENT-ON*
843b55ac660SJames Wright 
844b55ac660SJames Wright   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
845b55ac660SJames Wright   const bool implicit     = context->is_implicit;
846b55ac660SJames Wright 
847b55ac660SJames Wright   CeedPragmaSIMD
848b55ac660SJames Wright   // Quadrature Point Loop
849b55ac660SJames Wright   for (CeedInt i=0; i<Q; i++) {
850b55ac660SJames Wright     const CeedScalar x_i[3]  = {x[0][i], x[1][i], x[2][i]};
851b55ac660SJames Wright     const CeedScalar wdetJb  = (implicit ? -1. : 1.) * q_data_sur[0][i];
852b55ac660SJames Wright     const CeedScalar norm[3] = {q_data_sur[1][i],
853b55ac660SJames Wright                                 q_data_sur[2][i],
854b55ac660SJames Wright                                 q_data_sur[3][i]
855b55ac660SJames Wright                                };
856b55ac660SJames Wright     const CeedScalar dXdx[2][3] = {
857b55ac660SJames Wright       {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]},
858b55ac660SJames Wright       {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]}
859b55ac660SJames Wright     };
860b55ac660SJames Wright 
861b55ac660SJames Wright     CeedScalar U[5], kmstress[6], dU[5], dx_i[3] = {0.};
862b55ac660SJames Wright     for (int j=0; j<5; j++) U[j]         = jac_data_sur[j][i];
863b55ac660SJames Wright     for (int j=0; j<6; j++) kmstress[j]  = jac_data_sur[5+j][i];
864b55ac660SJames Wright     for (int j=0; j<3; j++) U[j+1]      *= U[0];
865b55ac660SJames Wright     for (int j=0; j<5; j++) dU[j]        = dq[j][i];
866b55ac660SJames Wright     State s  = StateFromU(context, U, x_i);
867b55ac660SJames Wright     State ds = StateFromU_fwd(context, s, dU, x_i, dx_i);
868b55ac660SJames Wright 
869b55ac660SJames Wright     State grad_ds[3];
870b55ac660SJames Wright     for (CeedInt j=0; j<3; j++) {
871b55ac660SJames Wright       CeedScalar dx_i[3] = {0}, dUj[5];
872b55ac660SJames Wright       for (CeedInt k=0; k<5; k++)
873b55ac660SJames Wright         dUj[k] = Grad_dq[0][k][i] * dXdx[0][j] +
874b55ac660SJames Wright                  Grad_dq[1][k][i] * dXdx[1][j];
875b55ac660SJames Wright       dx_i[j] = 1.;
876b55ac660SJames Wright       grad_ds[j] = StateFromU_fwd(context, s, dUj, x_i, dx_i);
877b55ac660SJames Wright     }
878b55ac660SJames Wright 
879b55ac660SJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
880b55ac660SJames Wright     KMStrainRate(grad_ds, dstrain_rate);
881b55ac660SJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
882b55ac660SJames Wright     KMUnpack(dkmstress, dstress);
883b55ac660SJames Wright     KMUnpack(kmstress, stress);
884b55ac660SJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
885b55ac660SJames Wright 
886b55ac660SJames Wright     StateConservative dF_inviscid[3];
887b55ac660SJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
888b55ac660SJames Wright 
889b55ac660SJames Wright     CeedScalar dFlux[5] = {0.};
890b55ac660SJames Wright     for (int j=0; j<3; j++) {
891b55ac660SJames Wright       dFlux[0] += dF_inviscid[j].density * norm[j];
892b55ac660SJames Wright       for (int k=0; k<3; k++)
893b55ac660SJames Wright         dFlux[k+1] += (dF_inviscid[j].momentum[k] - dstress[k][j]) * norm[j];
894b55ac660SJames Wright       dFlux[4] += (dF_inviscid[j].E_total + dFe[j]) * norm[j];
895b55ac660SJames Wright     }
896b55ac660SJames Wright 
897b55ac660SJames Wright     for (int j=0; j<5; j++)
898b55ac660SJames Wright       v[j][i] = -wdetJb * dFlux[j];
899b55ac660SJames Wright   } // End Quadrature Point Loop
900b55ac660SJames Wright   return 0;
901b55ac660SJames Wright }
902b55ac660SJames Wright 
90330e9fa81SJames Wright // Outflow boundary condition, weakly setting a constant pressure
90430e9fa81SJames Wright CEED_QFUNCTION(PressureOutflow)(void *ctx, CeedInt Q,
90530e9fa81SJames Wright                                 const CeedScalar *const *in,
90630e9fa81SJames Wright                                 CeedScalar *const *out) {
90730e9fa81SJames Wright   // *INDENT-OFF*
90830e9fa81SJames Wright   // Inputs
90930e9fa81SJames Wright   const CeedScalar (*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0],
910ce9b5c20SJames Wright                    (*Grad_q)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
911ce9b5c20SJames Wright                    (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
912ce9b5c20SJames Wright                    (*x)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
91330e9fa81SJames Wright   // Outputs
91430e9fa81SJames Wright   CeedScalar (*v)[CEED_Q_VLA]            = (CeedScalar(*)[CEED_Q_VLA])out[0],
91530e9fa81SJames Wright              (*jac_data_sur)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[1];
91630e9fa81SJames Wright   // *INDENT-ON*
91730e9fa81SJames Wright 
91830e9fa81SJames Wright   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
91930e9fa81SJames Wright   const bool       implicit = context->is_implicit;
92030e9fa81SJames Wright   const CeedScalar P0       = context->P0;
92130e9fa81SJames Wright 
92230e9fa81SJames Wright   CeedPragmaSIMD
92330e9fa81SJames Wright   // Quadrature Point Loop
92430e9fa81SJames Wright   for (CeedInt i=0; i<Q; i++) {
92530e9fa81SJames Wright     // Setup
92630e9fa81SJames Wright     // -- Interp in
927ce9b5c20SJames Wright     const CeedScalar U[5]   = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
928ce9b5c20SJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
929ce9b5c20SJames Wright     State            s      = StateFromU(context, U, x_i);
930ce9b5c20SJames Wright     s.Y.pressure = P0;
93130e9fa81SJames Wright 
93230e9fa81SJames Wright     // -- Interp-to-Interp q_data
93330e9fa81SJames Wright     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
93430e9fa81SJames Wright     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
93530e9fa81SJames Wright     // We can effect this by swapping the sign on this weight
93630e9fa81SJames Wright     const CeedScalar wdetJb  = (implicit ? -1. : 1.) * q_data_sur[0][i];
93730e9fa81SJames Wright 
93830e9fa81SJames Wright     // ---- Normal vect
93930e9fa81SJames Wright     const CeedScalar norm[3] = {q_data_sur[1][i],
94030e9fa81SJames Wright                                 q_data_sur[2][i],
94130e9fa81SJames Wright                                 q_data_sur[3][i]
94230e9fa81SJames Wright                                };
94330e9fa81SJames Wright 
944ce9b5c20SJames Wright     const CeedScalar dXdx[2][3] = {
945ce9b5c20SJames Wright       {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]},
946ce9b5c20SJames Wright       {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]}
947ce9b5c20SJames Wright     };
94830e9fa81SJames Wright 
949ce9b5c20SJames Wright     State grad_s[3];
950ce9b5c20SJames Wright     for (CeedInt j=0; j<3; j++) {
951ce9b5c20SJames Wright       CeedScalar dx_i[3] = {0}, dU[5];
952ce9b5c20SJames Wright       for (CeedInt k=0; k<5; k++)
953ce9b5c20SJames Wright         dU[k] = Grad_q[0][k][i] * dXdx[0][j] +
954ce9b5c20SJames Wright                 Grad_q[1][k][i] * dXdx[1][j];
955ce9b5c20SJames Wright       dx_i[j] = 1.;
956ce9b5c20SJames Wright       grad_s[j] = StateFromU_fwd(context, s, dU, x_i, dx_i);
957ce9b5c20SJames Wright     }
958ce9b5c20SJames Wright 
959ce9b5c20SJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
960ce9b5c20SJames Wright     KMStrainRate(grad_s, strain_rate);
961ce9b5c20SJames Wright     NewtonianStress(context, strain_rate, kmstress);
962ce9b5c20SJames Wright     KMUnpack(kmstress, stress);
963ce9b5c20SJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
964ce9b5c20SJames Wright 
965ce9b5c20SJames Wright     StateConservative F_inviscid[3];
966ce9b5c20SJames Wright     FluxInviscid(context, s, F_inviscid);
967ce9b5c20SJames Wright 
968ce9b5c20SJames Wright     CeedScalar Flux[5] = {0.};
969ce9b5c20SJames Wright     for (int j=0; j<3; j++) {
970ce9b5c20SJames Wright       Flux[0] += F_inviscid[j].density * norm[j];
971ce9b5c20SJames Wright       for (int k=0; k<3; k++)
972ce9b5c20SJames Wright         Flux[k+1] += (F_inviscid[j].momentum[k] - stress[k][j]) * norm[j];
973ce9b5c20SJames Wright       Flux[4] += (F_inviscid[j].E_total + Fe[j])*norm[j];
974ce9b5c20SJames Wright     }
97530e9fa81SJames Wright 
97630e9fa81SJames Wright     // -- Density
977ce9b5c20SJames Wright     v[0][i] = -wdetJb * Flux[0];
97830e9fa81SJames Wright 
97930e9fa81SJames Wright     // -- Momentum
98030e9fa81SJames Wright     for (CeedInt j=0; j<3; j++)
981ce9b5c20SJames Wright       v[j+1][i] = -wdetJb * Flux[j+1];
98230e9fa81SJames Wright 
98330e9fa81SJames Wright     // -- Total Energy Density
984ce9b5c20SJames Wright     v[4][i] = -wdetJb * Flux[4];
98530e9fa81SJames Wright 
98630e9fa81SJames Wright     // Save values for Jacobian
987ce9b5c20SJames Wright     jac_data_sur[0][i] = s.U.density;
988ce9b5c20SJames Wright     jac_data_sur[1][i] = s.Y.velocity[0];
989ce9b5c20SJames Wright     jac_data_sur[2][i] = s.Y.velocity[1];
990ce9b5c20SJames Wright     jac_data_sur[3][i] = s.Y.velocity[2];
991ce9b5c20SJames Wright     jac_data_sur[4][i] = s.U.E_total;
9920ec2498eSJames Wright     for (int j=0; j<6; j++) jac_data_sur[5+j][i] = kmstress[j];
99330e9fa81SJames Wright   } // End Quadrature Point Loop
99430e9fa81SJames Wright   return 0;
99530e9fa81SJames Wright }
99630e9fa81SJames Wright 
99730e9fa81SJames Wright // Jacobian for weak-pressure outflow boundary condition
99830e9fa81SJames Wright CEED_QFUNCTION(PressureOutflow_Jacobian)(void *ctx, CeedInt Q,
99930e9fa81SJames Wright     const CeedScalar *const *in,
100030e9fa81SJames Wright     CeedScalar *const *out) {
100130e9fa81SJames Wright   // *INDENT-OFF*
100230e9fa81SJames Wright   // Inputs
100330e9fa81SJames Wright   const CeedScalar (*dq)[CEED_Q_VLA]           = (const CeedScalar(*)[CEED_Q_VLA])in[0],
10040ec2498eSJames Wright                    (*Grad_dq)[5][CEED_Q_VLA]   = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
10050ec2498eSJames Wright                    (*q_data_sur)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[2],
10060ec2498eSJames Wright                    (*x)[CEED_Q_VLA]            = (const CeedScalar(*)[CEED_Q_VLA])in[3],
10070ec2498eSJames Wright                    (*jac_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
100830e9fa81SJames Wright   // Outputs
100930e9fa81SJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
101030e9fa81SJames Wright   // *INDENT-ON*
101130e9fa81SJames Wright 
101230e9fa81SJames Wright   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
101330e9fa81SJames Wright   const bool implicit     = context->is_implicit;
101430e9fa81SJames Wright 
101530e9fa81SJames Wright   CeedPragmaSIMD
101630e9fa81SJames Wright   // Quadrature Point Loop
101730e9fa81SJames Wright   for (CeedInt i=0; i<Q; i++) {
10180ec2498eSJames Wright     const CeedScalar x_i[3]  = {x[0][i], x[1][i], x[2][i]};
101930e9fa81SJames Wright     const CeedScalar wdetJb  = (implicit ? -1. : 1.) * q_data_sur[0][i];
102030e9fa81SJames Wright     const CeedScalar norm[3] = {q_data_sur[1][i],
102130e9fa81SJames Wright                                 q_data_sur[2][i],
102230e9fa81SJames Wright                                 q_data_sur[3][i]
102330e9fa81SJames Wright                                };
10240ec2498eSJames Wright     const CeedScalar dXdx[2][3] = {
10250ec2498eSJames Wright       {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]},
10260ec2498eSJames Wright       {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]}
10270ec2498eSJames Wright     };
10280ec2498eSJames Wright 
10290ec2498eSJames Wright     CeedScalar U[5], kmstress[6], dU[5], dx_i[3] = {0.};
10300ec2498eSJames Wright     for (int j=0; j<5; j++) U[j]         = jac_data_sur[j][i];
10310ec2498eSJames Wright     for (int j=0; j<6; j++) kmstress[j]  = jac_data_sur[5+j][i];
10320ec2498eSJames Wright     for (int j=0; j<3; j++) U[j+1]      *= U[0];
10330ec2498eSJames Wright     for (int j=0; j<5; j++) dU[j]        = dq[j][i];
10340ec2498eSJames Wright     State s  = StateFromU(context, U, x_i);
10350ec2498eSJames Wright     State ds = StateFromU_fwd(context, s, dU, x_i, dx_i);
10360ec2498eSJames Wright     s.Y.pressure  = context->P0;
10370ec2498eSJames Wright     ds.Y.pressure = 0.;
10380ec2498eSJames Wright 
10390ec2498eSJames Wright     State grad_ds[3];
10400ec2498eSJames Wright     for (CeedInt j=0; j<3; j++) {
10410ec2498eSJames Wright       CeedScalar dx_i[3] = {0}, dUj[5];
10420ec2498eSJames Wright       for (CeedInt k=0; k<5; k++)
10430ec2498eSJames Wright         dUj[k] = Grad_dq[0][k][i] * dXdx[0][j] +
10440ec2498eSJames Wright                  Grad_dq[1][k][i] * dXdx[1][j];
10450ec2498eSJames Wright       dx_i[j] = 1.;
10460ec2498eSJames Wright       grad_ds[j] = StateFromU_fwd(context, s, dUj, x_i, dx_i);
10470ec2498eSJames Wright     }
10480ec2498eSJames Wright 
10490ec2498eSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
10500ec2498eSJames Wright     KMStrainRate(grad_ds, dstrain_rate);
10510ec2498eSJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
10520ec2498eSJames Wright     KMUnpack(dkmstress, dstress);
10530ec2498eSJames Wright     KMUnpack(kmstress, stress);
10540ec2498eSJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
105530e9fa81SJames Wright 
1056b5d317f8SJames Wright     StateConservative dF_inviscid[3];
1057b5d317f8SJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
105830e9fa81SJames Wright 
1059b5d317f8SJames Wright     CeedScalar dFlux[5] = {0.};
1060b5d317f8SJames Wright     for (int j=0; j<3; j++) {
1061b5d317f8SJames Wright       dFlux[0] += dF_inviscid[j].density * norm[j];
1062b5d317f8SJames Wright       for (int k=0; k<3; k++)
10630ec2498eSJames Wright         dFlux[k+1] += (dF_inviscid[j].momentum[k] - dstress[k][j]) * norm[j];
10640ec2498eSJames Wright       dFlux[4] += (dF_inviscid[j].E_total + dFe[j]) * norm[j];
1065b5d317f8SJames Wright     }
1066b5d317f8SJames Wright 
1067b5d317f8SJames Wright     for (int j=0; j<5; j++)
1068b5d317f8SJames Wright       v[j][i] = -wdetJb * dFlux[j];
106930e9fa81SJames Wright   } // End Quadrature Point Loop
107030e9fa81SJames Wright   return 0;
107130e9fa81SJames Wright }
107230e9fa81SJames Wright 
107388b783a1SJames Wright // *****************************************************************************
107488b783a1SJames Wright #endif // newtonian_h
1075