xref: /libCEED/examples/fluids/qfunctions/newtonian.h (revision ff9b3c0e2ebd8a09dedd1c00be3d2c5b29de65cc)
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   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
147   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
148 
149   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
150   const CeedScalar        *g       = context->g;
151   const CeedScalar         dt      = context->dt;
152 
153   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
154     CeedScalar U[5], wdetJ, dXdx[3][3];
155     for (int j = 0; j < 5; j++) U[j] = q[j][i];
156     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
157     State s = StateFromU(context, U);
158 
159     State grad_s[3];
160     StatePhysicalGradientFromReference(Q, i, context, s, STATEVAR_CONSERVATIVE, Grad_q, dXdx, grad_s);
161 
162     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
163     KMStrainRate_State(grad_s, strain_rate);
164     NewtonianStress(context, strain_rate, kmstress);
165     KMUnpack(kmstress, stress);
166     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
167 
168     StateConservative F_inviscid[3];
169     FluxInviscid(context, s, F_inviscid);
170 
171     // Total flux
172     CeedScalar Flux[5][3];
173     FluxTotal(F_inviscid, stress, Fe, Flux);
174 
175     for (CeedInt j = 0; j < 5; j++) {
176       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]);
177     }
178 
179     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)};
180     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * body_force[j];
181 
182     // -- Stabilization method: none (Galerkin), SU, or SUPG
183     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0};
184     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
185     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, stab);
186 
187     for (CeedInt j = 0; j < 5; j++) {
188       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]);
189     }
190   }
191   return 0;
192 }
193 
194 // *****************************************************************************
195 // This QFunction implements the Navier-Stokes equations (mentioned above) with implicit time stepping method
196 //
197 //  SU   = Galerkin + grad(v) . ( Ai^T * Tau * (Aj q,j) )
198 //  SUPG = Galerkin + grad(v) . ( Ai^T * Tau * (q_dot + Aj q,j - body force) )
199 //                                       (diffusive terms will be added later)
200 // *****************************************************************************
201 CEED_QFUNCTION_HELPER int IFunction_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
202   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
203   const CeedScalar(*Grad_q)            = in[1];
204   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
205   const CeedScalar(*q_data)            = in[3];
206   const CeedScalar(*x)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[4];
207   CeedScalar(*v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
208   CeedScalar(*Grad_v)[5][CEED_Q_VLA]   = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
209   CeedScalar(*jac_data)                = out[2];
210 
211   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
212   const CeedScalar        *g       = context->g;
213   const CeedScalar         dt      = context->dt;
214   const CeedScalar         P0      = context->idl_pressure;
215 
216   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
217     const CeedScalar qi[5]  = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
218     const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]};
219     const State      s      = StateFromQ(context, qi, state_var);
220 
221     CeedScalar wdetJ, dXdx[3][3];
222     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
223     State grad_s[3];
224     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
225 
226     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
227     KMStrainRate_State(grad_s, strain_rate);
228     NewtonianStress(context, strain_rate, kmstress);
229     KMUnpack(kmstress, stress);
230     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
231 
232     StateConservative F_inviscid[3];
233     FluxInviscid(context, s, F_inviscid);
234 
235     // Total flux
236     CeedScalar Flux[5][3];
237     FluxTotal(F_inviscid, stress, Fe, Flux);
238 
239     for (CeedInt j = 0; j < 5; j++) {
240       for (CeedInt k = 0; k < 3; k++) {
241         Grad_v[k][j][i] = -wdetJ * (dXdx[k][0] * Flux[j][0] + dXdx[k][1] * Flux[j][1] + dXdx[k][2] * Flux[j][2]);
242       }
243     }
244 
245     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)};
246 
247     // -- Stabilization method: none (Galerkin), SU, or SUPG
248     CeedScalar Tau_d[3], stab[5][3], U_dot[5] = {0}, qi_dot[5];
249     for (int j = 0; j < 5; j++) qi_dot[j] = q_dot[j][i];
250     State s_dot = StateFromQ_fwd(context, s, qi_dot, state_var);
251     UnpackState_U(s_dot.U, U_dot);
252 
253     for (CeedInt j = 0; j < 5; j++) v[j][i] = wdetJ * (U_dot[j] - body_force[j]);
254     if (context->idl_enable) {
255       const CeedScalar sigma = LinearRampCoefficient(context->idl_amplitude, context->idl_length, context->idl_start, x_i[0]);
256       StoredValuesPack(Q, i, 14, 1, &sigma, jac_data);
257       CeedScalar damp_state[5] = {s.Y.pressure - P0, 0, 0, 0, 0}, idl_residual[5] = {0.};
258       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
259       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
260     }
261 
262     Tau_diagPrim(context, s, dXdx, dt, Tau_d);
263     Stabilization(context, s, Tau_d, grad_s, U_dot, body_force, stab);
264 
265     for (CeedInt j = 0; j < 5; j++) {
266       for (CeedInt k = 0; k < 3; k++) {
267         Grad_v[k][j][i] += wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]);
268       }
269     }
270     StoredValuesPack(Q, i, 0, 5, qi, jac_data);
271     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data);
272     StoredValuesPack(Q, i, 11, 3, Tau_d, jac_data);
273   }
274   return 0;
275 }
276 
277 CEED_QFUNCTION(IFunction_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
278   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
279 }
280 
281 CEED_QFUNCTION(IFunction_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
282   return IFunction_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
283 }
284 
285 // *****************************************************************************
286 // This QFunction implements the jacobian of the Navier-Stokes equations for implicit time stepping method.
287 // *****************************************************************************
288 CEED_QFUNCTION_HELPER int IJacobian_Newtonian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
289   const CeedScalar(*dq)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[0];
290   const CeedScalar(*Grad_dq)         = in[1];
291   const CeedScalar(*q_data)          = in[2];
292   const CeedScalar(*jac_data)        = in[3];
293   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
294   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
295 
296   NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
297   const CeedScalar        *g       = context->g;
298 
299   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
300     CeedScalar wdetJ, dXdx[3][3];
301     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
302 
303     CeedScalar qi[5], kmstress[6], Tau_d[3];
304     StoredValuesUnpack(Q, i, 0, 5, jac_data, qi);
305     StoredValuesUnpack(Q, i, 5, 6, jac_data, kmstress);
306     StoredValuesUnpack(Q, i, 11, 3, jac_data, Tau_d);
307     State s = StateFromQ(context, qi, state_var);
308 
309     CeedScalar dqi[5];
310     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
311     State ds = StateFromQ_fwd(context, s, dqi, state_var);
312 
313     State grad_ds[3];
314     StatePhysicalGradientFromReference(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
315 
316     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
317     KMStrainRate_State(grad_ds, dstrain_rate);
318     NewtonianStress(context, dstrain_rate, dkmstress);
319     KMUnpack(dkmstress, dstress);
320     KMUnpack(kmstress, stress);
321     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
322 
323     StateConservative dF_inviscid[3];
324     FluxInviscid_fwd(context, s, ds, dF_inviscid);
325 
326     // Total flux
327     CeedScalar dFlux[5][3];
328     FluxTotal(dF_inviscid, dstress, dFe, dFlux);
329 
330     for (int j = 0; j < 5; j++) {
331       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]);
332     }
333 
334     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)};
335     CeedScalar       dU[5]          = {0.};
336     UnpackState_U(ds.U, dU);
337     for (int j = 0; j < 5; j++) v[j][i] = wdetJ * (context->ijacobian_time_shift * dU[j] - dbody_force[j]);
338 
339     if (context->idl_enable) {
340       const CeedScalar sigma         = jac_data[14 * Q + i];
341       CeedScalar       damp_state[5] = {ds.Y.pressure, 0, 0, 0, 0}, idl_residual[5] = {0.};
342       // This is a Picard-type linearization of the damping and could be replaced by an InternalDampingLayer_fwd that uses s and ds.
343       InternalDampingLayer(context, s, sigma, damp_state, idl_residual);
344       for (int j = 0; j < 5; j++) v[j][i] += wdetJ * idl_residual[j];
345     }
346 
347     // -- Stabilization method: none (Galerkin), SU, or SUPG
348     CeedScalar dstab[5][3], U_dot[5] = {0};
349     for (CeedInt j = 0; j < 5; j++) U_dot[j] = context->ijacobian_time_shift * dU[j];
350     Stabilization(context, s, Tau_d, grad_ds, U_dot, dbody_force, dstab);
351 
352     for (int j = 0; j < 5; j++) {
353       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]);
354     }
355   }
356   return 0;
357 }
358 
359 CEED_QFUNCTION(IJacobian_Newtonian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
360   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
361 }
362 
363 CEED_QFUNCTION(IJacobian_Newtonian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
364   return IJacobian_Newtonian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
365 }
366 
367 // *****************************************************************************
368 // Compute boundary integral (ie. for strongly set inflows)
369 // *****************************************************************************
370 CEED_QFUNCTION_HELPER int BoundaryIntegral(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
371   const NewtonianIdealGasContext context = (NewtonianIdealGasContext)ctx;
372   const CeedScalar(*q)[CEED_Q_VLA]       = (const CeedScalar(*)[CEED_Q_VLA])in[0];
373   const CeedScalar(*Grad_q)              = in[1];
374   const CeedScalar(*q_data_sur)          = in[2];
375   CeedScalar(*v)[CEED_Q_VLA]             = (CeedScalar(*)[CEED_Q_VLA])out[0];
376   CeedScalar(*jac_data_sur)              = context->is_implicit ? out[1] : NULL;
377 
378   const bool is_implicit = context->is_implicit;
379 
380   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
381     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
382     State            s     = StateFromQ(context, qi, state_var);
383 
384     CeedScalar wdetJb, dXdx[2][3], norm[3];
385     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
386     wdetJb *= is_implicit ? -1. : 1.;
387 
388     State grad_s[3];
389     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_q, dXdx, grad_s);
390 
391     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
392     KMStrainRate_State(grad_s, strain_rate);
393     NewtonianStress(context, strain_rate, kmstress);
394     KMUnpack(kmstress, stress);
395     ViscousEnergyFlux(context, s.Y, grad_s, stress, Fe);
396 
397     StateConservative F_inviscid[3];
398     FluxInviscid(context, s, F_inviscid);
399 
400     CeedScalar Flux[5];
401     FluxTotal_Boundary(F_inviscid, stress, Fe, norm, Flux);
402 
403     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
404 
405     if (is_implicit) {
406       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
407       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
408     }
409   }
410   return 0;
411 }
412 
413 CEED_QFUNCTION(BoundaryIntegral_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
414   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
415 }
416 
417 CEED_QFUNCTION(BoundaryIntegral_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
418   return BoundaryIntegral(ctx, Q, in, out, STATEVAR_PRIMITIVE);
419 }
420 
421 // *****************************************************************************
422 // Jacobian for "set nothing" boundary integral
423 // *****************************************************************************
424 CEED_QFUNCTION_HELPER int BoundaryIntegral_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
425                                                     StateVariable state_var) {
426   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
427   const CeedScalar(*Grad_dq)        = in[1];
428   const CeedScalar(*q_data_sur)     = in[2];
429   const CeedScalar(*jac_data_sur)   = in[4];
430   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
431 
432   const NewtonianIdealGasContext context     = (NewtonianIdealGasContext)ctx;
433   const bool                     is_implicit = context->is_implicit;
434 
435   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
436     CeedScalar wdetJb, dXdx[2][3], norm[3];
437     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
438     wdetJb *= is_implicit ? -1. : 1.;
439 
440     CeedScalar qi[5], kmstress[6], dqi[5];
441     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
442     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
443     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
444 
445     State s  = StateFromQ(context, qi, state_var);
446     State ds = StateFromQ_fwd(context, s, dqi, state_var);
447 
448     State grad_ds[3];
449     StatePhysicalGradientFromReference_Boundary(Q, i, context, s, state_var, Grad_dq, dXdx, grad_ds);
450 
451     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
452     KMStrainRate_State(grad_ds, dstrain_rate);
453     NewtonianStress(context, dstrain_rate, dkmstress);
454     KMUnpack(dkmstress, dstress);
455     KMUnpack(kmstress, stress);
456     ViscousEnergyFlux_fwd(context, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
457 
458     StateConservative dF_inviscid[3];
459     FluxInviscid_fwd(context, s, ds, dF_inviscid);
460 
461     CeedScalar dFlux[5];
462     FluxTotal_Boundary(dF_inviscid, dstress, dFe, norm, dFlux);
463 
464     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
465   }
466   return 0;
467 }
468 
469 CEED_QFUNCTION(BoundaryIntegral_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
470   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
471 }
472 
473 CEED_QFUNCTION(BoundaryIntegral_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
474   return BoundaryIntegral_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
475 }
476