xref: /honee/qfunctions/newtonian.h (revision 3d65b166467e11bf054ebd404213121dbd593270)
1727da7e7SJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
33a8779fbSJames Wright //
4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
53a8779fbSJames Wright //
6727da7e7SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
73a8779fbSJames Wright 
83a8779fbSJames Wright /// @file
93a8779fbSJames Wright /// Operator for Navier-Stokes example using PETSc
103a8779fbSJames Wright 
113a8779fbSJames Wright #ifndef newtonian_h
123a8779fbSJames Wright #define newtonian_h
133a8779fbSJames Wright 
143a8779fbSJames Wright #include <ceed.h>
15d0cce58aSJeremy L Thompson #include <math.h>
167b530f2aSAdelekeBankole #include <stdlib.h>
172b916ea7SJeremy L Thompson 
18475b2820SJames Wright #include "newtonian_state.h"
19d0cce58aSJeremy L Thompson #include "newtonian_types.h"
20d1b9ef12SLeila Ghaffari #include "stabilization.h"
21d0cce58aSJeremy L Thompson #include "utils.h"
22bb8a0c61SJames Wright 
23bb8a0c61SJames Wright // *****************************************************************************
243a8779fbSJames Wright // This QFunction sets a "still" initial condition for generic Newtonian IG problems
253a8779fbSJames Wright // *****************************************************************************
262b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsNewtonianIG)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
273a8779fbSJames Wright   // Inputs
283a8779fbSJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
293a8779fbSJames Wright 
303a8779fbSJames Wright   // Outputs
313a8779fbSJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
323a8779fbSJames Wright 
33bb8a0c61SJames Wright   // Context
34bb8a0c61SJames Wright   const SetupContext context = (SetupContext)ctx;
35bb8a0c61SJames Wright   const CeedScalar   theta0  = context->theta0;
36bb8a0c61SJames Wright   const CeedScalar   P0      = context->P0;
37bb8a0c61SJames Wright   const CeedScalar   cv      = context->cv;
38bb8a0c61SJames Wright   const CeedScalar   cp      = context->cp;
39bb8a0c61SJames Wright   const CeedScalar  *g       = context->g;
40bb8a0c61SJames Wright   const CeedScalar   Rd      = cp - cv;
41bb8a0c61SJames Wright 
423a8779fbSJames Wright   // Quadrature Point Loop
432b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
443a8779fbSJames Wright     CeedScalar q[5] = {0.};
453a8779fbSJames Wright 
463a8779fbSJames Wright     // Setup
473a8779fbSJames Wright     // -- Coordinates
48bb8a0c61SJames Wright     const CeedScalar x[3]        = {X[0][i], X[1][i], X[2][i]};
49d1b9ef12SLeila Ghaffari     const CeedScalar e_potential = -Dot3(g, x);
503a8779fbSJames Wright 
513a8779fbSJames Wright     // -- Density
52bb8a0c61SJames Wright     const CeedScalar rho = P0 / (Rd * theta0);
533a8779fbSJames Wright 
543a8779fbSJames Wright     // Initial Conditions
553a8779fbSJames Wright     q[0] = rho;
563a8779fbSJames Wright     q[1] = 0.0;
573a8779fbSJames Wright     q[2] = 0.0;
583a8779fbSJames Wright     q[3] = 0.0;
59bb8a0c61SJames Wright     q[4] = rho * (cv * theta0 + e_potential);
603a8779fbSJames Wright 
612b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
62d1b9ef12SLeila Ghaffari 
633a8779fbSJames Wright   }  // End of Quadrature Point Loop
643a8779fbSJames Wright   return 0;
653a8779fbSJames Wright }
663a8779fbSJames Wright 
673a8779fbSJames Wright // *****************************************************************************
68cbe60e31SLeila Ghaffari // This QFunction sets a "still" initial condition for generic Newtonian IG
69cbe60e31SLeila Ghaffari //   problems in primitive variables
70cbe60e31SLeila Ghaffari // *****************************************************************************
712b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsNewtonianIG_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
72cbe60e31SLeila Ghaffari   // Outputs
73cbe60e31SLeila Ghaffari   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
74cbe60e31SLeila Ghaffari 
75cbe60e31SLeila Ghaffari   // Context
76cbe60e31SLeila Ghaffari   const SetupContext context = (SetupContext)ctx;
77cbe60e31SLeila Ghaffari   const CeedScalar   theta0  = context->theta0;
78cbe60e31SLeila Ghaffari   const CeedScalar   P0      = context->P0;
79cbe60e31SLeila Ghaffari 
80cbe60e31SLeila Ghaffari   // Quadrature Point Loop
812b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
82cbe60e31SLeila Ghaffari     CeedScalar q[5] = {0.};
83cbe60e31SLeila Ghaffari 
84cbe60e31SLeila Ghaffari     // Initial Conditions
85cbe60e31SLeila Ghaffari     q[0] = P0;
86cbe60e31SLeila Ghaffari     q[1] = 0.0;
87cbe60e31SLeila Ghaffari     q[2] = 0.0;
88cbe60e31SLeila Ghaffari     q[3] = 0.0;
89cbe60e31SLeila Ghaffari     q[4] = theta0;
90cbe60e31SLeila Ghaffari 
912b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
92cbe60e31SLeila Ghaffari 
93cbe60e31SLeila Ghaffari   }  // End of Quadrature Point Loop
94cbe60e31SLeila Ghaffari   return 0;
95cbe60e31SLeila Ghaffari }
96cbe60e31SLeila Ghaffari 
97cbe60e31SLeila Ghaffari // *****************************************************************************
983a8779fbSJames Wright // This QFunction implements the following formulation of Navier-Stokes with
993a8779fbSJames Wright //   explicit time stepping method
1003a8779fbSJames Wright //
1013a8779fbSJames Wright // This is 3D compressible Navier-Stokes in conservation form with state
1023a8779fbSJames Wright //   variables of density, momentum density, and total energy density.
1033a8779fbSJames Wright //
1043a8779fbSJames Wright // State Variables: q = ( rho, U1, U2, U3, E )
1053a8779fbSJames Wright //   rho - Mass Density
1063a8779fbSJames Wright //   Ui  - Momentum Density,      Ui = rho ui
1073a8779fbSJames Wright //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
1083a8779fbSJames Wright //
1093a8779fbSJames Wright // Navier-Stokes Equations:
1103a8779fbSJames Wright //   drho/dt + div( U )                               = 0
1113a8779fbSJames Wright //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
1123a8779fbSJames Wright //   dE/dt   + div( (E + P) u )                       = div( Fe )
1133a8779fbSJames Wright //
1143a8779fbSJames Wright // Viscous Stress:
1153a8779fbSJames Wright //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
1163a8779fbSJames Wright //
1173a8779fbSJames Wright // Thermal Stress:
1183a8779fbSJames Wright //   Fe = u Fu + k grad( T )
119bb8a0c61SJames Wright // Equation of State
1203a8779fbSJames Wright //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
1213a8779fbSJames Wright //
1223a8779fbSJames Wright // Stabilization:
1233a8779fbSJames Wright //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
1243a8779fbSJames Wright //     f1 = rho  sqrt(ui uj gij)
1253a8779fbSJames Wright //     gij = dXi/dX * dXi/dX
1263a8779fbSJames Wright //     TauC = Cc f1 / (8 gii)
1273a8779fbSJames Wright //     TauM = min( 1 , 1 / f1 )
1283a8779fbSJames Wright //     TauE = TauM / (Ce cv)
1293a8779fbSJames Wright //
1303a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
1313a8779fbSJames Wright //
1323a8779fbSJames Wright // Constants:
1333a8779fbSJames Wright //   lambda = - 2 / 3,  From Stokes hypothesis
1343a8779fbSJames Wright //   mu              ,  Dynamic viscosity
1353a8779fbSJames Wright //   k               ,  Thermal conductivity
1363a8779fbSJames Wright //   cv              ,  Specific heat, constant volume
1373a8779fbSJames Wright //   cp              ,  Specific heat, constant pressure
1383a8779fbSJames Wright //   g               ,  Gravity
1393a8779fbSJames Wright //   gamma  = cp / cv,  Specific heat ratio
1403a8779fbSJames Wright //
1413a8779fbSJames Wright // We require the product of the inverse of the Jacobian (dXdx_j,k) and
1423a8779fbSJames Wright // its transpose (dXdx_k,j) to properly compute integrals of the form:
1433a8779fbSJames Wright // int( gradv gradu )
1443a8779fbSJames Wright //
1453a8779fbSJames Wright // *****************************************************************************
1462b916ea7SJeremy L Thompson CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1473a8779fbSJames Wright   // Inputs
148*3d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
149*3d65b166SJames Wright   const CeedScalar(*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
150*3d65b166SJames Wright   const CeedScalar(*q_data)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])in[2];
151*3d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[3];
152*3d65b166SJames Wright 
1533a8779fbSJames Wright   // Outputs
154*3d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
155*3d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
1563a8779fbSJames Wright 
1573a8779fbSJames Wright   // Context
1583a8779fbSJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
159bb8a0c61SJames Wright   const CeedScalar        *g       = context->g;
160bb8a0c61SJames Wright   const CeedScalar         dt      = context->dt;
1613a8779fbSJames Wright 
1623a8779fbSJames Wright   // Quadrature Point Loop
163*3d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
164c1a52365SJed Brown     CeedScalar U[5];
165c1a52365SJed Brown     for (int j = 0; j < 5; j++) U[j] = q[j][i];
166c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
167c1a52365SJed Brown     State            s      = StateFromU(context, U, x_i);
168c1a52365SJed Brown 
1693a8779fbSJames Wright     // -- Interp-to-Interp q_data
1703a8779fbSJames Wright     const CeedScalar wdetJ = q_data[0][i];
1713a8779fbSJames Wright     // -- Interp-to-Grad q_data
1723a8779fbSJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
1732b916ea7SJeremy L Thompson     const CeedScalar dXdx[3][3] = {
1742b916ea7SJeremy L Thompson         {q_data[1][i], q_data[2][i], q_data[3][i]},
17534ea8d65SJames Wright         {q_data[4][i], q_data[5][i], q_data[6][i]},
17634ea8d65SJames Wright         {q_data[7][i], q_data[8][i], q_data[9][i]}
1773a8779fbSJames Wright     };
178c1a52365SJed Brown     State grad_s[3];
179eef2387dSJed Brown     for (CeedInt j = 0; j < 3; j++) {
1802f7ce6c1SJed Brown       CeedScalar dx_i[3] = {0}, dU[5];
1812b916ea7SJeremy L Thompson       for (CeedInt k = 0; k < 5; k++) dU[k] = Grad_q[0][k][i] * dXdx[0][j] + Grad_q[1][k][i] * dXdx[1][j] + Grad_q[2][k][i] * dXdx[2][j];
182c1a52365SJed Brown       dx_i[j]   = 1.;
1832f7ce6c1SJed Brown       grad_s[j] = StateFromU_fwd(context, s, dU, x_i, dx_i);
184c1a52365SJed Brown     }
185c1a52365SJed Brown 
186c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
187c1a52365SJed Brown     KMStrainRate(grad_s, strain_rate);
188c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
189c1a52365SJed Brown     KMUnpack(kmstress, stress);
190c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
191c1a52365SJed Brown 
192c1a52365SJed Brown     StateConservative F_inviscid[3];
193c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
194c1a52365SJed Brown 
195c1a52365SJed Brown     // Total flux
196c1a52365SJed Brown     CeedScalar Flux[5][3];
197d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
198c1a52365SJed Brown 
1992b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
2002b916ea7SJeremy L Thompson       for (CeedInt k = 0; k < 5; k++) Grad_v[j][k][i] = wdetJ * (dXdx[j][0] * Flux[k][0] + dXdx[j][1] * Flux[k][1] + dXdx[j][2] * Flux[k][2]);
2012b916ea7SJeremy L Thompson     }
202c1a52365SJed Brown 
203c1a52365SJed Brown     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], 0};
2042b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
2053a8779fbSJames Wright 
206d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
207d1b9ef12SLeila Ghaffari     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0};
208d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
209d1b9ef12SLeila Ghaffari     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, x_i, stab);
2103a8779fbSJames Wright 
2112b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
2122b916ea7SJeremy 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]);
2132b916ea7SJeremy L Thompson     }
2143a8779fbSJames Wright   }  // End Quadrature Point Loop
2153a8779fbSJames Wright 
2163a8779fbSJames Wright   // Return
2173a8779fbSJames Wright   return 0;
2183a8779fbSJames Wright }
2193a8779fbSJames Wright 
2203a8779fbSJames Wright // *****************************************************************************
2213a8779fbSJames Wright // This QFunction implements the Navier-Stokes equations (mentioned above) with
2223a8779fbSJames Wright //   implicit time stepping method
2233a8779fbSJames Wright //
2243a8779fbSJames Wright //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
2253a8779fbSJames Wright //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
2263a8779fbSJames Wright //                                       (diffussive terms will be added later)
2273a8779fbSJames Wright //
2283a8779fbSJames Wright // *****************************************************************************
2292b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateFromQi_t StateFromQi,
2302b916ea7SJeremy L Thompson                                               StateFromQi_fwd_t StateFromQi_fwd) {
2313a8779fbSJames Wright   // Inputs
232*3d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
233*3d65b166SJames Wright   const CeedScalar(*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
234*3d65b166SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[2];
235*3d65b166SJames Wright   const CeedScalar(*q_data)[CEED_Q_VLA]    = (const CeedScalar(*)[CEED_Q_VLA])in[3];
236*3d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[4];
237*3d65b166SJames Wright 
2383a8779fbSJames Wright   // Outputs
239*3d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
240*3d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
241*3d65b166SJames Wright   CeedScalar(*jac_data)[CEED_Q_VLA]  = (CeedScalar(*)[CEED_Q_VLA])out[2];
242*3d65b166SJames Wright 
2433a8779fbSJames Wright   // Context
2443a8779fbSJames Wright   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
245bb8a0c61SJames Wright   const CeedScalar        *g       = context->g;
246bb8a0c61SJames Wright   const CeedScalar         dt      = context->dt;
2473a8779fbSJames Wright 
2483a8779fbSJames Wright   // Quadrature Point Loop
249*3d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
250*3d65b166SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
251c1a52365SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
252*3d65b166SJames Wright     const State      s      = StateFromQi(context, qi, x_i);
253c1a52365SJed Brown 
2543a8779fbSJames Wright     // -- Interp-to-Interp q_data
2553a8779fbSJames Wright     const CeedScalar wdetJ = q_data[0][i];
2563a8779fbSJames Wright     // -- Interp-to-Grad q_data
2573a8779fbSJames Wright     // ---- Inverse of change of coordinate matrix: X_i,j
2582b916ea7SJeremy L Thompson     const CeedScalar dXdx[3][3] = {
2592b916ea7SJeremy L Thompson         {q_data[1][i], q_data[2][i], q_data[3][i]},
26034ea8d65SJames Wright         {q_data[4][i], q_data[5][i], q_data[6][i]},
26134ea8d65SJames Wright         {q_data[7][i], q_data[8][i], q_data[9][i]}
2623a8779fbSJames Wright     };
263c1a52365SJed Brown     State grad_s[3];
264493642f1SJames Wright     for (CeedInt j = 0; j < 3; j++) {
26576555becSJames Wright       CeedScalar dx_i[3] = {0}, dqi[5];
266*3d65b166SJames Wright       for (CeedInt k = 0; k < 5; k++) {
267*3d65b166SJames Wright         dqi[k] = Grad_q[0][k][i] * dXdx[0][j] + Grad_q[1][k][i] * dXdx[1][j] + Grad_q[2][k][i] * dXdx[2][j];
268*3d65b166SJames Wright       }
269c1a52365SJed Brown       dx_i[j]   = 1.;
27076555becSJames Wright       grad_s[j] = StateFromQi_fwd(context, s, dqi, x_i, dx_i);
2713a8779fbSJames Wright     }
272c1a52365SJed Brown 
273c1a52365SJed Brown     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
274c1a52365SJed Brown     KMStrainRate(grad_s, strain_rate);
275c1a52365SJed Brown     NewtonianStress(context, strain_rate, kmstress);
276c1a52365SJed Brown     KMUnpack(kmstress, stress);
277c1a52365SJed Brown     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
278c1a52365SJed Brown 
279c1a52365SJed Brown     StateConservative F_inviscid[3];
280c1a52365SJed Brown     FluxInviscid(context, s, F_inviscid);
281c1a52365SJed Brown 
282c1a52365SJed Brown     // Total flux
283c1a52365SJed Brown     CeedScalar Flux[5][3];
284d1b9ef12SLeila Ghaffari     FluxTotal(F_inviscid, stress, Fe, Flux);
285c1a52365SJed Brown 
2862b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
287*3d65b166SJames Wright       for (CeedInt k = 0; k < 5; k++) {
288*3d65b166SJames Wright         Grad_v[j][k][i] = -wdetJ * (dXdx[j][0] * Flux[k][0] + dXdx[j][1] * Flux[k][1] + dXdx[j][2] * Flux[k][2]);
289*3d65b166SJames Wright       }
2902b916ea7SJeremy L Thompson     }
291c1a52365SJed Brown 
292c1a52365SJed Brown     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], 0};
2933a8779fbSJames Wright 
294d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
29576555becSJames Wright     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5], dx0[3] = {0};
29676555becSJames Wright     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
29776555becSJames Wright     State s_dot = StateFromQi_fwd(context, s, qi_dot, x_i, dx0);
29876555becSJames Wright     UnpackState_U(s_dot.U, U_dot);
29976555becSJames Wright 
3002b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
301d1b9ef12SLeila Ghaffari     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
302d1b9ef12SLeila Ghaffari     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, x_i, stab);
3033a8779fbSJames Wright 
3042b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) {
305*3d65b166SJames Wright       for (CeedInt k = 0; k < 3; k++) {
306*3d65b166SJames 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]);
307*3d65b166SJames Wright       }
3082b916ea7SJeremy L Thompson     }
30976555becSJames Wright     for (CeedInt j = 0; j < 5; j++) jac_data[j][i] = qi[j];
310eef2387dSJed Brown     for (CeedInt j = 0; j < 6; j++) jac_data[5 + j][i] = kmstress[j];
311eef2387dSJed Brown     for (CeedInt j = 0; j < 3; j++) jac_data[5 + 6 + j][i] = Tau_d[j];
3123a8779fbSJames Wright 
3133a8779fbSJames Wright   }  // End Quadrature Point Loop
3143a8779fbSJames Wright 
3153a8779fbSJames Wright   // Return
3163a8779fbSJames Wright   return 0;
3173a8779fbSJames Wright }
318f0b65372SJed Brown 
3192b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
32076555becSJames Wright   return IFunction_Newtonian(ctx, Q, in, out, StateFromU, StateFromU_fwd);
32176555becSJames Wright }
32276555becSJames Wright 
3232b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
32476555becSJames Wright   return IFunction_Newtonian(ctx, Q, in, out, StateFromY, StateFromY_fwd);
32576555becSJames Wright }
32676555becSJames Wright 
327cbe60e31SLeila Ghaffari // *****************************************************************************
32876555becSJames Wright // This QFunction implements the jacobian of the Navier-Stokes equations
329cbe60e31SLeila Ghaffari //   for implicit time stepping method.
330cbe60e31SLeila Ghaffari // *****************************************************************************
3312b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateFromQi_t StateFromQi,
3322b916ea7SJeremy L Thompson                                               StateFromQi_fwd_t StateFromQi_fwd) {
333f0b65372SJed Brown   // Inputs
334*3d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
335*3d65b166SJames Wright   const CeedScalar(*Grad_dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
336*3d65b166SJames Wright   const CeedScalar(*q_data)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[2];
337*3d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
338*3d65b166SJames Wright   const CeedScalar(*jac_data)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[4];
339*3d65b166SJames Wright 
340f0b65372SJed Brown   // Outputs
341*3d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
342*3d65b166SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
343*3d65b166SJames Wright 
344f0b65372SJed Brown   // Context
345f0b65372SJed Brown   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
346f0b65372SJed Brown   const CeedScalar        *g       = context->g;
347f0b65372SJed Brown 
348f0b65372SJed Brown   // Quadrature Point Loop
349*3d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
350f0b65372SJed Brown     // -- Interp-to-Interp q_data
351f0b65372SJed Brown     const CeedScalar wdetJ = q_data[0][i];
352f0b65372SJed Brown     // -- Interp-to-Grad q_data
353f0b65372SJed Brown     // ---- Inverse of change of coordinate matrix: X_i,j
3542b916ea7SJeremy L Thompson     const CeedScalar dXdx[3][3] = {
3552b916ea7SJeremy L Thompson         {q_data[1][i], q_data[2][i], q_data[3][i]},
35634ea8d65SJames Wright         {q_data[4][i], q_data[5][i], q_data[6][i]},
35734ea8d65SJames Wright         {q_data[7][i], q_data[8][i], q_data[9][i]}
358f0b65372SJed Brown     };
359f0b65372SJed Brown 
3608789e95fSJames Wright     CeedScalar qi[5], kmstress[6], Tau_d[3];
36176555becSJames Wright     for (int j = 0; j < 5; j++) qi[j] = jac_data[j][i];
362f0b65372SJed Brown     for (int j = 0; j < 6; j++) kmstress[j] = jac_data[5 + j][i];
363f0b65372SJed Brown     for (int j = 0; j < 3; j++) Tau_d[j] = jac_data[5 + 6 + j][i];
364f0b65372SJed Brown     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
36576555becSJames Wright     State            s      = StateFromQi(context, qi, x_i);
366f0b65372SJed Brown 
36776555becSJames Wright     CeedScalar dqi[5], dx0[3] = {0};
36876555becSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
36976555becSJames Wright     State ds = StateFromQi_fwd(context, s, dqi, x_i, dx0);
370f0b65372SJed Brown 
371f0b65372SJed Brown     State grad_ds[3];
372f0b65372SJed Brown     for (int j = 0; j < 3; j++) {
37376555becSJames Wright       CeedScalar dqi_j[5];
3742b916ea7SJeremy L Thompson       for (int k = 0; k < 5; k++) dqi_j[k] = Grad_dq[0][k][i] * dXdx[0][j] + Grad_dq[1][k][i] * dXdx[1][j] + Grad_dq[2][k][i] * dXdx[2][j];
37576555becSJames Wright       grad_ds[j] = StateFromQi_fwd(context, s, dqi_j, x_i, dx0);
376f0b65372SJed Brown     }
377f0b65372SJed Brown 
378f0b65372SJed Brown     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
379f0b65372SJed Brown     KMStrainRate(grad_ds, dstrain_rate);
380f0b65372SJed Brown     NewtonianStress(context, dstrain_rate, dkmstress);
381f0b65372SJed Brown     KMUnpack(dkmstress, dstress);
382f0b65372SJed Brown     KMUnpack(kmstress, stress);
383f0b65372SJed Brown     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
384f0b65372SJed Brown 
385f0b65372SJed Brown     StateConservative dF_inviscid[3];
386f0b65372SJed Brown     FluxInviscid_fwd(context, s, ds, dF_inviscid);
387f0b65372SJed Brown 
388f0b65372SJed Brown     // Total flux
389f0b65372SJed Brown     CeedScalar dFlux[5][3];
390d1b9ef12SLeila Ghaffari     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
391f0b65372SJed Brown 
3922b916ea7SJeremy L Thompson     for (int j = 0; j < 3; j++) {
3932b916ea7SJeremy L Thompson       for (int k = 0; k < 5; k++) Grad_v[j][k][i] = -wdetJ * (dXdx[j][0] * dFlux[k][0] + dXdx[j][1] * dFlux[k][1] + dXdx[j][2] * dFlux[k][2]);
3942b916ea7SJeremy L Thompson     }
395f0b65372SJed Brown 
396f0b65372SJed Brown     const CeedScalar dbody_force[5] = {0, ds.U.density * g[0], ds.U.density * g[1], ds.U.density * g[2], 0};
39776555becSJames Wright     CeedScalar       dU[5]          = {0.};
39876555becSJames Wright     UnpackState_U(ds.U, dU);
3992b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
400f0b65372SJed Brown 
401d1b9ef12SLeila Ghaffari     // -- Stabilization method: none (Galerkin), SU, or SUPG
402d1b9ef12SLeila Ghaffari     CeedScalar dstab[5][3], U_dot[5] = {0};
403d1b9ef12SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
404d1b9ef12SLeila Ghaffari     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, x_i, dstab);
405d1b9ef12SLeila Ghaffari 
4062b916ea7SJeremy L Thompson     for (int j = 0; j < 5; j++) {
4072b916ea7SJeremy 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]);
4082b916ea7SJeremy L Thompson     }
409f0b65372SJed Brown   }  // End Quadrature Point Loop
410f0b65372SJed Brown   return 0;
411f0b65372SJed Brown }
4128085925cSJames Wright 
4132b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
41476555becSJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, StateFromU, StateFromU_fwd);
41576555becSJames Wright }
41676555becSJames Wright 
4172b916ea7SJeremy L Thompson CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
41876555becSJames Wright   return IJacobian_Newtonian(ctx, Q, in, out, StateFromY, StateFromY_fwd);
41976555becSJames Wright }
42076555becSJames Wright 
421d1b9ef12SLeila Ghaffari // *****************************************************************************
4228085925cSJames Wright // Compute boundary integral (ie. for strongly set inflows)
423d1b9ef12SLeila Ghaffari // *****************************************************************************
4242b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateFromQi_t StateFromQi,
4252b916ea7SJeremy L Thompson                                            StateFromQi_fwd_t StateFromQi_fwd) {
426*3d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0];
427*3d65b166SJames Wright   const CeedScalar(*Grad_q)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
428*3d65b166SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
429*3d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
4308085925cSJames Wright 
431*3d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]            = (CeedScalar(*)[CEED_Q_VLA])out[0];
432*3d65b166SJames Wright   CeedScalar(*jac_data_sur)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[1];
4338085925cSJames Wright 
434d3b25f3aSJames Wright   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
435d3b25f3aSJames Wright   const bool                     is_implicit = context->is_implicit;
4368085925cSJames Wright 
4372b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
438d3b25f3aSJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
43941e73928SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
44041e73928SJames Wright     State            s      = StateFromQi(context, qi, x_i);
4418085925cSJames Wright 
4428085925cSJames Wright     const CeedScalar wdetJb = (is_implicit ? -1. : 1.) * q_data_sur[0][i];
443c5740391SJames Wright     // ---- Normal vector
4442b916ea7SJeremy L Thompson     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
4458085925cSJames Wright 
446d3b25f3aSJames Wright     const CeedScalar dXdx[2][3] = {
447d3b25f3aSJames Wright         {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]},
448d3b25f3aSJames Wright         {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]}
449d3b25f3aSJames Wright     };
4508085925cSJames Wright 
451d3b25f3aSJames Wright     State grad_s[3];
452d3b25f3aSJames Wright     for (CeedInt j = 0; j < 3; j++) {
45341e73928SJames Wright       CeedScalar dx_i[3] = {0}, dqi[5];
4542b916ea7SJeremy L Thompson       for (CeedInt k = 0; k < 5; k++) dqi[k] = Grad_q[0][k][i] * dXdx[0][j] + Grad_q[1][k][i] * dXdx[1][j];
455d3b25f3aSJames Wright       dx_i[j]   = 1.;
45641e73928SJames Wright       grad_s[j] = StateFromQi_fwd(context, s, dqi, x_i, dx_i);
457d3b25f3aSJames Wright     }
4588085925cSJames Wright 
459d3b25f3aSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
460d3b25f3aSJames Wright     KMStrainRate(grad_s, strain_rate);
461d3b25f3aSJames Wright     NewtonianStress(context, strain_rate, kmstress);
462d3b25f3aSJames Wright     KMUnpack(kmstress, stress);
463d3b25f3aSJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
464d3b25f3aSJames Wright 
465d3b25f3aSJames Wright     StateConservative F_inviscid[3];
466d3b25f3aSJames Wright     FluxInviscid(context, s, F_inviscid);
467d3b25f3aSJames Wright 
468c5740391SJames Wright     CeedScalar Flux[5];
469c5740391SJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, norm, Flux);
470d3b25f3aSJames Wright 
471c5740391SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
4728085925cSJames Wright 
473c5740391SJames Wright     for (int j = 0; j < 5; j++) jac_data_sur[j][i] = qi[j];
47468ae065aSJames Wright     for (int j = 0; j < 6; j++) jac_data_sur[5 + j][i] = kmstress[j];
4758085925cSJames Wright   }
4768085925cSJames Wright   return 0;
4778085925cSJames Wright }
4788085925cSJames Wright 
4792b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
480d4559bbeSJames Wright   return BoundaryIntegral(ctx, Q, in, out, StateFromU, StateFromU_fwd);
481d4559bbeSJames Wright }
482d4559bbeSJames Wright 
4832b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
484d4559bbeSJames Wright   return BoundaryIntegral(ctx, Q, in, out, StateFromY, StateFromY_fwd);
485d4559bbeSJames Wright }
486d4559bbeSJames Wright 
487d1b9ef12SLeila Ghaffari // *****************************************************************************
48868ae065aSJames Wright // Jacobian for "set nothing" boundary integral
489d1b9ef12SLeila Ghaffari // *****************************************************************************
4902b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
491d4559bbeSJames Wright                                                     StateFromQi_t StateFromQi, StateFromQi_fwd_t StateFromQi_fwd) {
49268ae065aSJames Wright   // Inputs
493*3d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]           = (const CeedScalar(*)[CEED_Q_VLA])in[0];
494*3d65b166SJames Wright   const CeedScalar(*Grad_dq)[5][CEED_Q_VLA]   = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
495*3d65b166SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[2];
496*3d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]            = (const CeedScalar(*)[CEED_Q_VLA])in[3];
497*3d65b166SJames Wright   const CeedScalar(*jac_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
498*3d65b166SJames Wright 
49968ae065aSJames Wright   // Outputs
50068ae065aSJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
50168ae065aSJames Wright 
50268ae065aSJames Wright   const NewtonianIdealGasContext context  = (NewtonianIdealGasContext)ctx;
50368ae065aSJames Wright   const bool                     implicit = context->is_implicit;
50468ae065aSJames Wright 
50568ae065aSJames Wright   // Quadrature Point Loop
506*3d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
50768ae065aSJames Wright     const CeedScalar x_i[3]     = {x[0][i], x[1][i], x[2][i]};
50868ae065aSJames Wright     const CeedScalar wdetJb     = (implicit ? -1. : 1.) * q_data_sur[0][i];
5092b916ea7SJeremy L Thompson     const CeedScalar norm[3]    = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
51068ae065aSJames Wright     const CeedScalar dXdx[2][3] = {
51168ae065aSJames Wright         {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]},
51268ae065aSJames Wright         {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]}
51368ae065aSJames Wright     };
51468ae065aSJames Wright 
51541e73928SJames Wright     CeedScalar qi[5], kmstress[6], dqi[5], dx_i[3] = {0.};
51641e73928SJames Wright     for (int j = 0; j < 5; j++) qi[j] = jac_data_sur[j][i];
51768ae065aSJames Wright     for (int j = 0; j < 6; j++) kmstress[j] = jac_data_sur[5 + j][i];
51841e73928SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
5193934e2b1SJames Wright 
52041e73928SJames Wright     State s  = StateFromQi(context, qi, x_i);
52141e73928SJames Wright     State ds = StateFromQi_fwd(context, s, dqi, x_i, dx_i);
52268ae065aSJames Wright 
52368ae065aSJames Wright     State grad_ds[3];
52468ae065aSJames Wright     for (CeedInt j = 0; j < 3; j++) {
52541e73928SJames Wright       CeedScalar dx_i[3] = {0}, dqi_j[5];
5262b916ea7SJeremy L Thompson       for (CeedInt k = 0; k < 5; k++) dqi_j[k] = Grad_dq[0][k][i] * dXdx[0][j] + Grad_dq[1][k][i] * dXdx[1][j];
52768ae065aSJames Wright       dx_i[j]    = 1.;
52841e73928SJames Wright       grad_ds[j] = StateFromQi_fwd(context, s, dqi_j, x_i, dx_i);
52968ae065aSJames Wright     }
53068ae065aSJames Wright 
53168ae065aSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
53268ae065aSJames Wright     KMStrainRate(grad_ds, dstrain_rate);
53368ae065aSJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
53468ae065aSJames Wright     KMUnpack(dkmstress, dstress);
53568ae065aSJames Wright     KMUnpack(kmstress, stress);
53668ae065aSJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
53768ae065aSJames Wright 
53868ae065aSJames Wright     StateConservative dF_inviscid[3];
53968ae065aSJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
54068ae065aSJames Wright 
541c5740391SJames Wright     CeedScalar dFlux[5];
542c5740391SJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, norm, dFlux);
54368ae065aSJames Wright 
544c5740391SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
54568ae065aSJames Wright   }  // End Quadrature Point Loop
54668ae065aSJames Wright   return 0;
54768ae065aSJames Wright }
54868ae065aSJames Wright 
5492b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
550d4559bbeSJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, StateFromU, StateFromU_fwd);
551d4559bbeSJames Wright }
552d4559bbeSJames Wright 
5532b916ea7SJeremy L Thompson CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
554d4559bbeSJames Wright   return BoundaryIntegral_Jacobian(ctx, Q, in, out, StateFromY, StateFromY_fwd);
555d4559bbeSJames Wright }
556d4559bbeSJames Wright 
557d1b9ef12SLeila Ghaffari // *****************************************************************************
55804b9037bSJames Wright // Outflow boundary condition, weakly setting a constant pressure
559d1b9ef12SLeila Ghaffari // *****************************************************************************
5602b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int PressureOutflow(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateFromQi_t StateFromQi,
5612b916ea7SJeremy L Thompson                                           StateFromQi_fwd_t StateFromQi_fwd) {
56204b9037bSJames Wright   // Inputs
563*3d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0];
564*3d65b166SJames Wright   const CeedScalar(*Grad_q)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
565*3d65b166SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
566*3d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
567*3d65b166SJames Wright 
56804b9037bSJames Wright   // Outputs
569*3d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]            = (CeedScalar(*)[CEED_Q_VLA])out[0];
570*3d65b166SJames Wright   CeedScalar(*jac_data_sur)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[1];
57104b9037bSJames Wright 
57204b9037bSJames Wright   const NewtonianIdealGasContext context  = (NewtonianIdealGasContext)ctx;
57304b9037bSJames Wright   const bool                     implicit = context->is_implicit;
57404b9037bSJames Wright   const CeedScalar               P0       = context->P0;
57504b9037bSJames Wright 
57604b9037bSJames Wright   // Quadrature Point Loop
577*3d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
57804b9037bSJames Wright     // Setup
57904b9037bSJames Wright     // -- Interp in
58025bfcc41SJames Wright     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
58141e73928SJames Wright     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
58241e73928SJames Wright     State            s      = StateFromQi(context, qi, x_i);
58325bfcc41SJames Wright     s.Y.pressure            = P0;
58404b9037bSJames Wright 
58504b9037bSJames Wright     // -- Interp-to-Interp q_data
58604b9037bSJames Wright     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
58704b9037bSJames Wright     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
58804b9037bSJames Wright     // We can effect this by swapping the sign on this weight
58904b9037bSJames Wright     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
59004b9037bSJames Wright 
591c5740391SJames Wright     // ---- Normal vector
592d4559bbeSJames Wright     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
59304b9037bSJames Wright 
59425bfcc41SJames Wright     const CeedScalar dXdx[2][3] = {
59525bfcc41SJames Wright         {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]},
59625bfcc41SJames Wright         {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]}
59725bfcc41SJames Wright     };
59804b9037bSJames Wright 
59925bfcc41SJames Wright     State grad_s[3];
60025bfcc41SJames Wright     for (CeedInt j = 0; j < 3; j++) {
60141e73928SJames Wright       CeedScalar dx_i[3] = {0}, dqi[5];
6022b916ea7SJeremy L Thompson       for (CeedInt k = 0; k < 5; k++) dqi[k] = Grad_q[0][k][i] * dXdx[0][j] + Grad_q[1][k][i] * dXdx[1][j];
60325bfcc41SJames Wright       dx_i[j]   = 1.;
60441e73928SJames Wright       grad_s[j] = StateFromQi_fwd(context, s, dqi, x_i, dx_i);
60525bfcc41SJames Wright     }
60625bfcc41SJames Wright 
60725bfcc41SJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
60825bfcc41SJames Wright     KMStrainRate(grad_s, strain_rate);
60925bfcc41SJames Wright     NewtonianStress(context, strain_rate, kmstress);
61025bfcc41SJames Wright     KMUnpack(kmstress, stress);
61125bfcc41SJames Wright     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
61225bfcc41SJames Wright 
61325bfcc41SJames Wright     StateConservative F_inviscid[3];
61425bfcc41SJames Wright     FluxInviscid(context, s, F_inviscid);
61525bfcc41SJames Wright 
616c5740391SJames Wright     CeedScalar Flux[5];
617c5740391SJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, norm, Flux);
61804b9037bSJames Wright 
619c5740391SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
62004b9037bSJames Wright 
62104b9037bSJames Wright     // Save values for Jacobian
622c5740391SJames Wright     for (int j = 0; j < 5; j++) jac_data_sur[j][i] = qi[j];
623b01ba163SJames Wright     for (int j = 0; j < 6; j++) jac_data_sur[5 + j][i] = kmstress[j];
62404b9037bSJames Wright   }  // End Quadrature Point Loop
62504b9037bSJames Wright   return 0;
62604b9037bSJames Wright }
62704b9037bSJames Wright 
6282b916ea7SJeremy L Thompson CEED_QFUNCTION(PressureOutflow_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
629d4559bbeSJames Wright   return PressureOutflow(ctx, Q, in, out, StateFromU, StateFromU_fwd);
630d4559bbeSJames Wright }
631d4559bbeSJames Wright 
6322b916ea7SJeremy L Thompson CEED_QFUNCTION(PressureOutflow_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
633d4559bbeSJames Wright   return PressureOutflow(ctx, Q, in, out, StateFromY, StateFromY_fwd);
634d4559bbeSJames Wright }
635d4559bbeSJames Wright 
636d1b9ef12SLeila Ghaffari // *****************************************************************************
63704b9037bSJames Wright // Jacobian for weak-pressure outflow boundary condition
638d1b9ef12SLeila Ghaffari // *****************************************************************************
6392b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER int PressureOutflow_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
640d4559bbeSJames Wright                                                    StateFromQi_t StateFromQi, StateFromQi_fwd_t StateFromQi_fwd) {
64104b9037bSJames Wright   // Inputs
642*3d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]           = (const CeedScalar(*)[CEED_Q_VLA])in[0];
643*3d65b166SJames Wright   const CeedScalar(*Grad_dq)[5][CEED_Q_VLA]   = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
644*3d65b166SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[2];
645*3d65b166SJames Wright   const CeedScalar(*x)[CEED_Q_VLA]            = (const CeedScalar(*)[CEED_Q_VLA])in[3];
646*3d65b166SJames Wright   const CeedScalar(*jac_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
647*3d65b166SJames Wright 
64804b9037bSJames Wright   // Outputs
64904b9037bSJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
65004b9037bSJames Wright 
65104b9037bSJames Wright   const NewtonianIdealGasContext context  = (NewtonianIdealGasContext)ctx;
65204b9037bSJames Wright   const bool                     implicit = context->is_implicit;
65304b9037bSJames Wright 
65404b9037bSJames Wright   // Quadrature Point Loop
655*3d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
656b01ba163SJames Wright     const CeedScalar x_i[3]     = {x[0][i], x[1][i], x[2][i]};
65704b9037bSJames Wright     const CeedScalar wdetJb     = (implicit ? -1. : 1.) * q_data_sur[0][i];
658d4559bbeSJames Wright     const CeedScalar norm[3]    = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
659b01ba163SJames Wright     const CeedScalar dXdx[2][3] = {
660b01ba163SJames Wright         {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]},
661b01ba163SJames Wright         {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]}
662b01ba163SJames Wright     };
663b01ba163SJames Wright 
66441e73928SJames Wright     CeedScalar qi[5], kmstress[6], dqi[5], dx_i[3] = {0.};
66541e73928SJames Wright     for (int j = 0; j < 5; j++) qi[j] = jac_data_sur[j][i];
666b01ba163SJames Wright     for (int j = 0; j < 6; j++) kmstress[j] = jac_data_sur[5 + j][i];
66741e73928SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
6683934e2b1SJames Wright 
66941e73928SJames Wright     State s       = StateFromQi(context, qi, x_i);
67041e73928SJames Wright     State ds      = StateFromQi_fwd(context, s, dqi, x_i, dx_i);
671b01ba163SJames Wright     s.Y.pressure  = context->P0;
672b01ba163SJames Wright     ds.Y.pressure = 0.;
673b01ba163SJames Wright 
674b01ba163SJames Wright     State grad_ds[3];
675b01ba163SJames Wright     for (CeedInt j = 0; j < 3; j++) {
67641e73928SJames Wright       CeedScalar dx_i[3] = {0}, dqi_j[5];
6772b916ea7SJeremy L Thompson       for (CeedInt k = 0; k < 5; k++) dqi_j[k] = Grad_dq[0][k][i] * dXdx[0][j] + Grad_dq[1][k][i] * dXdx[1][j];
678b01ba163SJames Wright       dx_i[j]    = 1.;
67941e73928SJames Wright       grad_ds[j] = StateFromQi_fwd(context, s, dqi_j, x_i, dx_i);
680b01ba163SJames Wright     }
681b01ba163SJames Wright 
682b01ba163SJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
683b01ba163SJames Wright     KMStrainRate(grad_ds, dstrain_rate);
684b01ba163SJames Wright     NewtonianStress(context, dstrain_rate, dkmstress);
685b01ba163SJames Wright     KMUnpack(dkmstress, dstress);
686b01ba163SJames Wright     KMUnpack(kmstress, stress);
687b01ba163SJames Wright     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
68804b9037bSJames Wright 
689e6b47afbSJames Wright     StateConservative dF_inviscid[3];
690e6b47afbSJames Wright     FluxInviscid_fwd(context, s, ds, dF_inviscid);
69104b9037bSJames Wright 
692c5740391SJames Wright     CeedScalar dFlux[5];
693c5740391SJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, norm, dFlux);
694e6b47afbSJames Wright 
695c5740391SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
69604b9037bSJames Wright   }  // End Quadrature Point Loop
69704b9037bSJames Wright   return 0;
69804b9037bSJames Wright }
69904b9037bSJames Wright 
7002b916ea7SJeremy L Thompson CEED_QFUNCTION(PressureOutflow_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
701d4559bbeSJames Wright   return PressureOutflow_Jacobian(ctx, Q, in, out, StateFromU, StateFromU_fwd);
702d4559bbeSJames Wright }
703d4559bbeSJames Wright 
7042b916ea7SJeremy L Thompson CEED_QFUNCTION(PressureOutflow_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
705d4559bbeSJames Wright   return PressureOutflow_Jacobian(ctx, Q, in, out, StateFromY, StateFromY_fwd);
706d4559bbeSJames Wright }
7072b916ea7SJeremy L Thompson 
7083a8779fbSJames Wright #endif  // newtonian_h
709