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