xref: /libCEED/examples/fluids/qfunctions/newtonian.h (revision 216bbcafa310ca9c8563aabbff60855a4edc792b)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// Operator for Navier-Stokes example using PETSc
10 
11 #ifndef newtonian_h
12 #define newtonian_h
13 
14 #include <ceed.h>
15 #include <math.h>
16 #include <stdlib.h>
17 
18 #include "newtonian_state.h"
19 #include "newtonian_types.h"
20 #include "stabilization.h"
21 #include "utils.h"
22 
23 CEED_QFUNCTION_HELPER void InternalDampingLayer(const NewtonianIdealGasContext context, const State s, const CeedScalar x_i[3], CeedScalar damp_Y[5],
24                                                 CeedScalar damp_residual[5]) {
25   const CeedScalar sigma = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
26   ScaleN(damp_Y, sigma, 5);
27   CeedScalar dx_i[3] = {0};
28   State      damp_s  = StateFromY_fwd(context, s, damp_Y, x_i, dx_i);
29 
30   CeedScalar U[5];
31   UnpackState_U(damp_s.U, U);
32   for (int i = 0; i < 5; i++) damp_residual[i] += U[i];
33 }
34 
35 // *****************************************************************************
36 // This QFunction sets a "still" initial condition for generic Newtonian IG problems
37 // *****************************************************************************
38 CEED_QFUNCTION_HELPER int ICsNewtonianIG(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
39   // Inputs
40   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
41 
42   // Outputs
43   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
44 
45   // Context
46   const SetupContext context = (SetupContext)ctx;
47 
48   // Quadrature Point Loop
49   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
50     CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
51     CeedScalar q[5] = {0.};
52     State      s    = StateFromPrimitive(&context->gas, context->reference, x);
53     StateToQ(&context->gas, s, q, state_var);
54     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
55   }  // End of Quadrature Point Loop
56   return 0;
57 }
58 
59 CEED_QFUNCTION(ICsNewtonianIG_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
60   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_PRIMITIVE);
61 }
62 CEED_QFUNCTION(ICsNewtonianIG_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
63   return ICsNewtonianIG(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
64 }
65 
66 // *****************************************************************************
67 // This QFunction implements the following formulation of Navier-Stokes with explicit time stepping method
68 //
69 // This is 3D compressible Navier-Stokes in conservation form with state variables of density, momentum density, and total energy density.
70 //
71 // State Variables: q = ( rho, U1, U2, U3, E )
72 //   rho - Mass Density
73 //   Ui  - Momentum Density,      Ui = rho ui
74 //   E   - Total Energy Density,  E  = rho (cv T + (u u)/2 + g z)
75 //
76 // Navier-Stokes Equations:
77 //   drho/dt + div( U )                               = 0
78 //   dU/dt   + div( rho (u x u) + P I3 ) + rho g khat = div( Fu )
79 //   dE/dt   + div( (E + P) u )                       = div( Fe )
80 //
81 // Viscous Stress:
82 //   Fu = mu (grad( u ) + grad( u )^T + lambda div ( u ) I3)
83 //
84 // Thermal Stress:
85 //   Fe = u Fu + k grad( T )
86 // Equation of State
87 //   P = (gamma - 1) (E - rho (u u) / 2 - rho g z)
88 //
89 // Stabilization:
90 //   Tau = diag(TauC, TauM, TauM, TauM, TauE)
91 //     f1 = rho  sqrt(ui uj gij)
92 //     gij = dXi/dX * dXi/dX
93 //     TauC = Cc f1 / (8 gii)
94 //     TauM = min( 1 , 1 / f1 )
95 //     TauE = TauM / (Ce cv)
96 //
97 //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
98 //
99 // Constants:
100 //   lambda = - 2 / 3,  From Stokes hypothesis
101 //   mu              ,  Dynamic viscosity
102 //   k               ,  Thermal conductivity
103 //   cv              ,  Specific heat, constant volume
104 //   cp              ,  Specific heat, constant pressure
105 //   g               ,  Gravity
106 //   gamma  = cp / cv,  Specific heat ratio
107 //
108 // We require the product of the inverse of the Jacobian (dXdx_j,k) and its transpose (dXdx_k,j) to properly compute integrals of the form: int( gradv
109 // gradu )
110 // *****************************************************************************
111 CEED_QFUNCTION(RHSFunction_Newtonian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
112   // Inputs
113   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
114   const CeedScalar(*Grad_q)        = in[1];
115   const CeedScalar(*q_data)        = in[2];
116   const CeedScalar(*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
117 
118   // Outputs
119   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
120   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
121 
122   // Context
123   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
124   const CeedScalar        *g       = context->g;
125   const CeedScalar         dt      = context->dt;
126 
127   // Quadrature Point Loop
128   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
129     CeedScalar U[5], wdetJ, dXdx[3][3];
130     for (int j = 0; j < 5; j++) U[j] = q[j][i];
131     StoredValuesUnpack(Q, i, 0, 1, q_data, &wdetJ);
132     StoredValuesUnpack(Q, i, 1, 9, q_data, (CeedScalar *)dXdx);
133     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
134     State            s      = StateFromU(context, U, x_i);
135 
136     State grad_s[3];
137     StatePhysicalGradientFromReference(Q, i, context, s, x_i, STATEVAR_CONSERVATIVE, Grad_q, dXdx, false, grad_s);
138 
139     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
140     KMStrainRate_State(grad_s, strain_rate);
141     NewtonianStress(context, strain_rate, kmstress);
142     KMUnpack(kmstress, stress);
143     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
144 
145     StateConservative F_inviscid[3];
146     FluxInviscid(context, s, F_inviscid);
147 
148     // Total flux
149     CeedScalar Flux[5][3];
150     FluxTotal(F_inviscid, stress, Fe, Flux);
151 
152     for (CeedInt j = 0; j < 5; j++) {
153       for (CeedInt k = 0; k < 3; k++) Grad_v[k][j][i] = wdetJ * (dXdx[k][0] * Flux[j][0] + dXdx[k][1] * Flux[j][1] + dXdx[k][2] * Flux[j][2]);
154     }
155 
156     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], 0};
157     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
158 
159     // -- Stabilization method: none (Galerkin), SU, or SUPG
160     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0};
161     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
162     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, x_i, stab);
163 
164     for (CeedInt j = 0; j < 5; j++) {
165       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]);
166     }
167   }  // End Quadrature Point Loop
168 
169   // Return
170   return 0;
171 }
172 
173 // *****************************************************************************
174 // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
175 //
176 //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
177 //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
178 //                                       (diffusive terms will be added later)
179 // *****************************************************************************
180 CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
181   // Inputs
182   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
183   const CeedScalar(*Grad_q)            = in[1];
184   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
185   const CeedScalar(*q_data)            = in[3];
186   const CeedScalar(*x)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[4];
187 
188   // Outputs
189   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
190   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
191   CeedScalar(*jac_data)              = out[2];
192 
193   // Context
194   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
195   const CeedScalar        *g       = context->g;
196   const CeedScalar         dt      = context->dt;
197   const CeedScalar         P0      = context->P0;
198 
199   // Quadrature Point Loop
200   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
201     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
202     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
203     const State      s      = StateFromQ(context, qi, x_i, state_var);
204 
205     CeedScalar wdetJ, dXdx[3][3];
206     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
207     State grad_s[3];
208     StatePhysicalGradientFromReference(Q, i, context, s, x_i, state_var, Grad_q, dXdx, false, grad_s);
209 
210     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
211     KMStrainRate_State(grad_s, strain_rate);
212     NewtonianStress(context, strain_rate, kmstress);
213     KMUnpack(kmstress, stress);
214     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
215 
216     StateConservative F_inviscid[3];
217     FluxInviscid(context, s, F_inviscid);
218 
219     // Total flux
220     CeedScalar Flux[5][3];
221     FluxTotal(F_inviscid, stress, Fe, Flux);
222 
223     for (CeedInt j = 0; j < 5; j++) {
224       for (CeedInt k = 0; k < 3; k++) {
225         Grad_v[k][j][i] = -wdetJ * (dXdx[k][0] * Flux[j][0] + dXdx[k][1] * Flux[j][1] + dXdx[k][2] * Flux[j][2]);
226       }
227     }
228 
229     const CeedScalar body_force[5] = {0, s.U.density * g[0], s.U.density * g[1], s.U.density * g[2], 0};
230 
231     // -- Stabilization method: none (Galerkin), SU, or SUPG
232     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5], dx0[3] = {0};
233     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
234     State s_dot = StateFromQ_fwd(context, s, qi_dot, x_i, dx0, state_var);
235     UnpackState_U(s_dot.U, U_dot);
236 
237     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
238     if (context->idl_enable) {
239       CeedScalar damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
240       InternalDampingLayer(context, s, x_i, damp_state, idl_residual);
241       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
242     }
243 
244     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
245     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, x_i, stab);
246 
247     for (CeedInt j = 0; j < 5; j++) {
248       for (CeedInt k = 0; k < 3; k++) {
249         Grad_v[k][j][i] += wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]);
250       }
251     }
252     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
253     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
254     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
255 
256   }  // End Quadrature Point Loop
257 
258   // Return
259   return 0;
260 }
261 
262 CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
263   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
264 }
265 
266 CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
267   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
268 }
269 
270 // *****************************************************************************
271 // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
272 // *****************************************************************************
273 CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
274   // Inputs
275   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
276   const CeedScalar(*Grad_dq)        = in[1];
277   const CeedScalar(*q_data)         = in[2];
278   const CeedScalar(*x)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[3];
279   const CeedScalar(*jac_data)       = in[4];
280 
281   // Outputs
282   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
283   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
284 
285   // Context
286   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
287   const CeedScalar        *g       = context->g;
288 
289   // Quadrature Point Loop
290   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
291     CeedScalar wdetJ, dXdx[3][3];
292     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
293 
294     CeedScalar qi[5], kmstress[6], Tau_d[3];
295     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
296     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
297     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
298     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
299     State            s      = StateFromQ(context, qi, x_i, state_var);
300 
301     CeedScalar dqi[5], dx0[3] = {0};
302     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
303     State ds = StateFromQ_fwd(context, s, dqi, x_i, dx0, state_var);
304 
305     State grad_ds[3];
306     StatePhysicalGradientFromReference(Q, i, context, s, x_i, state_var, Grad_dq, dXdx, true, grad_ds);
307 
308     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
309     KMStrainRate_State(grad_ds, dstrain_rate);
310     NewtonianStress(context, dstrain_rate, dkmstress);
311     KMUnpack(dkmstress, dstress);
312     KMUnpack(kmstress, stress);
313     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
314 
315     StateConservative dF_inviscid[3];
316     FluxInviscid_fwd(context, s, ds, dF_inviscid);
317 
318     // Total flux
319     CeedScalar dFlux[5][3];
320     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
321 
322     for (int j = 0; j < 5; j++) {
323       for (int k = 0; k < 3; k++) Grad_v[k][j][i] = -wdetJ * (dXdx[k][0] * dFlux[j][0] + dXdx[k][1] * dFlux[j][1] + dXdx[k][2] * dFlux[j][2]);
324     }
325 
326     const CeedScalar dbody_force[5] = {0, ds.U.density * g[0], ds.U.density * g[1], ds.U.density * g[2], 0};
327     CeedScalar       dU[5]          = {0.};
328     UnpackState_U(ds.U, dU);
329     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
330 
331     if (context->idl_enable) {
332       CeedScalar damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
333       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
334       InternalDampingLayer(context, s, x_i, damp_state, idl_residual);
335       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
336     }
337 
338     // -- Stabilization method: none (Galerkin), SU, or SUPG
339     CeedScalar dstab[5][3], U_dot[5] = {0};
340     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
341     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, x_i, dstab);
342 
343     for (int j = 0; j < 5; j++) {
344       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]);
345     }
346   }  // End Quadrature Point Loop
347   return 0;
348 }
349 
350 CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
351   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
352 }
353 
354 CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
355   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
356 }
357 
358 // *****************************************************************************
359 // Compute boundary integral (ie. for strongly set inflows)
360 // *****************************************************************************
361 CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
362   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
363   const CeedScalar(*Grad_q)        = in[1];
364   const CeedScalar(*q_data_sur)    = in[2];
365   const CeedScalar(*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
366 
367   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
368   CeedScalar(*jac_data_sur)  = out[1];
369 
370   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
371   const bool                     is_implicit = context->is_implicit;
372 
373   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
374     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
375     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
376     State            s      = StateFromQ(context, qi, x_i, state_var);
377 
378     CeedScalar wdetJb, dXdx[2][3], norm[3];
379     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
380     wdetJb *= is_implicit ? -1. : 1.;
381 
382     State grad_s[3];
383     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, x_i, state_var, Grad_q, dXdx, false, grad_s);
384 
385     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
386     KMStrainRate_State(grad_s, strain_rate);
387     NewtonianStress(context, strain_rate, kmstress);
388     KMUnpack(kmstress, stress);
389     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
390 
391     StateConservative F_inviscid[3];
392     FluxInviscid(context, s, F_inviscid);
393 
394     CeedScalar Flux[5];
395     FluxTotal_Boundary(F_inviscid, stress, Fe, norm, Flux);
396 
397     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
398 
399     StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
400     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
401   }
402   return 0;
403 }
404 
405 CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
406   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
407 }
408 
409 CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
410   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
411 }
412 
413 // *****************************************************************************
414 // Jacobian for "set nothing" boundary integral
415 // *****************************************************************************
416 CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
417                                                     StateVariable state_var) {
418   // Inputs
419   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
420   const CeedScalar(*Grad_dq)        = in[1];
421   const CeedScalar(*q_data_sur)     = in[2];
422   const CeedScalar(*x)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[3];
423   const CeedScalar(*jac_data_sur)   = in[4];
424 
425   // Outputs
426   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
427 
428   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
429   const bool                     is_implicit = context->is_implicit;
430 
431   // Quadrature Point Loop
432   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
433     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
434     CeedScalar       wdetJb, dXdx[2][3], norm[3];
435     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
436     wdetJb *= is_implicit ? -1. : 1.;
437 
438     CeedScalar qi[5], kmstress[6], dqi[5], dx_i[3] = {0.};
439     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
440     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
441     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
442 
443     State s  = StateFromQ(context, qi, x_i, state_var);
444     State ds = StateFromQ_fwd(context, s, dqi, x_i, dx_i, state_var);
445 
446     State grad_ds[3];
447     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, x_i, state_var, Grad_dq, dXdx, false, grad_ds);
448 
449     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
450     KMStrainRate_State(grad_ds, dstrain_rate);
451     NewtonianStress(context, dstrain_rate, dkmstress);
452     KMUnpack(dkmstress, dstress);
453     KMUnpack(kmstress, stress);
454     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
455 
456     StateConservative dF_inviscid[3];
457     FluxInviscid_fwd(context, s, ds, dF_inviscid);
458 
459     CeedScalar dFlux[5];
460     FluxTotal_Boundary(dF_inviscid, dstress, dFe, norm, dFlux);
461 
462     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
463   }  // End Quadrature Point Loop
464   return 0;
465 }
466 
467 CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
468   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
469 }
470 
471 CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
472   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
473 }
474 
475 #endif  // newtonian_h
476