xref: /libCEED/examples/fluids/qfunctions/bc_freestream.h (revision 5aed82e4fa97acf4ba24a7f10a35f5303a6798e0)
1*5aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
29f844368SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
39f844368SJames Wright //
49f844368SJames Wright // SPDX-License-Identifier: BSD-2-Clause
59f844368SJames Wright //
69f844368SJames Wright // This file is part of CEED:  http://github.com/ceed
79f844368SJames Wright 
89f844368SJames Wright /// @file
99f844368SJames Wright /// QFunctions for the `bc_freestream` and `bc_outflow` boundary conditions
109f844368SJames Wright 
119f844368SJames Wright #include "bc_freestream_type.h"
129f844368SJames Wright #include "newtonian_state.h"
139f844368SJames Wright #include "newtonian_types.h"
149f844368SJames Wright #include "riemann_solver.h"
159f844368SJames Wright 
169f844368SJames Wright // *****************************************************************************
179f844368SJames Wright // Freestream Boundary Condition
189f844368SJames Wright // *****************************************************************************
199f844368SJames Wright CEED_QFUNCTION_HELPER int Freestream(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var,
209f844368SJames Wright                                      RiemannFluxType flux_type) {
219f844368SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
229f844368SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
239f844368SJames Wright 
249f844368SJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
259f844368SJames Wright   CeedScalar(*jac_data_sur)  = out[1];
269f844368SJames Wright 
279f844368SJames Wright   const FreestreamContext        context     = (FreestreamContext)ctx;
289f844368SJames Wright   const NewtonianIdealGasContext newt_ctx    = &context->newtonian_ctx;
299f844368SJames Wright   const bool                     is_implicit = newt_ctx->is_implicit;
309f844368SJames Wright   const CeedScalar               zeros[6]    = {0.};
319f844368SJames Wright 
329f844368SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
339f844368SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
349f844368SJames Wright     State            s     = StateFromQ(newt_ctx, qi, state_var);
359f844368SJames Wright 
369f844368SJames Wright     CeedScalar wdetJb, norm[3];
379f844368SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
389f844368SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
399f844368SJames Wright 
409f844368SJames Wright     StateConservative flux;
419f844368SJames Wright     switch (flux_type) {
429f844368SJames Wright       case RIEMANN_HLL:
439f844368SJames Wright         flux = RiemannFlux_HLL(newt_ctx, s, context->S_infty, norm);
449f844368SJames Wright         break;
459f844368SJames Wright       case RIEMANN_HLLC:
469f844368SJames Wright         flux = RiemannFlux_HLLC(newt_ctx, s, context->S_infty, norm);
479f844368SJames Wright         break;
489f844368SJames Wright     }
499f844368SJames Wright     CeedScalar Flux[5];
509f844368SJames Wright     UnpackState_U(flux, Flux);
519f844368SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
529f844368SJames Wright 
539f844368SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
549f844368SJames Wright     StoredValuesPack(Q, i, 5, 6, zeros, jac_data_sur);  // Every output value must be set
559f844368SJames Wright   }
569f844368SJames Wright   return 0;
579f844368SJames Wright }
589f844368SJames Wright 
599f844368SJames Wright CEED_QFUNCTION(Freestream_Conserv_HLL)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
609f844368SJames Wright   return Freestream(ctx, Q, in, out, STATEVAR_CONSERVATIVE, RIEMANN_HLL);
619f844368SJames Wright }
629f844368SJames Wright 
639f844368SJames Wright CEED_QFUNCTION(Freestream_Prim_HLL)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
649f844368SJames Wright   return Freestream(ctx, Q, in, out, STATEVAR_PRIMITIVE, RIEMANN_HLL);
659f844368SJames Wright }
669f844368SJames Wright 
679f844368SJames Wright CEED_QFUNCTION(Freestream_Conserv_HLLC)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
689f844368SJames Wright   return Freestream(ctx, Q, in, out, STATEVAR_CONSERVATIVE, RIEMANN_HLLC);
699f844368SJames Wright }
709f844368SJames Wright 
719f844368SJames Wright CEED_QFUNCTION(Freestream_Prim_HLLC)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
729f844368SJames Wright   return Freestream(ctx, Q, in, out, STATEVAR_PRIMITIVE, RIEMANN_HLLC);
739f844368SJames Wright }
749f844368SJames Wright 
759f844368SJames Wright CEED_QFUNCTION_HELPER int Freestream_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var,
769f844368SJames Wright                                               RiemannFluxType flux_type) {
779f844368SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
789f844368SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
799f844368SJames Wright   const CeedScalar(*jac_data_sur)   = in[4];
809f844368SJames Wright 
819f844368SJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
829f844368SJames Wright 
839f844368SJames Wright   const FreestreamContext        context     = (FreestreamContext)ctx;
849f844368SJames Wright   const NewtonianIdealGasContext newt_ctx    = &context->newtonian_ctx;
859f844368SJames Wright   const bool                     is_implicit = newt_ctx->is_implicit;
869f844368SJames Wright   const State                    dS_infty    = {0};
879f844368SJames Wright 
889f844368SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
899f844368SJames Wright     CeedScalar wdetJb, norm[3];
909f844368SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
919f844368SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
929f844368SJames Wright 
939f844368SJames Wright     CeedScalar qi[5], dqi[5];
949f844368SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
959f844368SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
969f844368SJames Wright     State s  = StateFromQ(newt_ctx, qi, state_var);
979f844368SJames Wright     State ds = StateFromQ_fwd(newt_ctx, s, dqi, state_var);
989f844368SJames Wright 
999f844368SJames Wright     StateConservative dflux;
1009f844368SJames Wright     switch (flux_type) {
1019f844368SJames Wright       case RIEMANN_HLL:
1029f844368SJames Wright         dflux = RiemannFlux_HLL_fwd(newt_ctx, s, ds, context->S_infty, dS_infty, norm);
1039f844368SJames Wright         break;
1049f844368SJames Wright       case RIEMANN_HLLC:
1059f844368SJames Wright         dflux = RiemannFlux_HLLC_fwd(newt_ctx, s, ds, context->S_infty, dS_infty, norm);
1069f844368SJames Wright         break;
1079f844368SJames Wright     }
1089f844368SJames Wright     CeedScalar dFlux[5];
1099f844368SJames Wright     UnpackState_U(dflux, dFlux);
1109f844368SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
1119f844368SJames Wright   }
1129f844368SJames Wright   return 0;
1139f844368SJames Wright }
1149f844368SJames Wright 
1159f844368SJames Wright CEED_QFUNCTION(Freestream_Jacobian_Conserv_HLL)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1169f844368SJames Wright   return Freestream_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE, RIEMANN_HLL);
1179f844368SJames Wright }
1189f844368SJames Wright 
1199f844368SJames Wright CEED_QFUNCTION(Freestream_Jacobian_Prim_HLL)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1209f844368SJames Wright   return Freestream_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE, RIEMANN_HLL);
1219f844368SJames Wright }
1229f844368SJames Wright 
1239f844368SJames Wright CEED_QFUNCTION(Freestream_Jacobian_Conserv_HLLC)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1249f844368SJames Wright   return Freestream_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE, RIEMANN_HLLC);
1259f844368SJames Wright }
1269f844368SJames Wright 
1279f844368SJames Wright CEED_QFUNCTION(Freestream_Jacobian_Prim_HLLC)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1289f844368SJames Wright   return Freestream_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE, RIEMANN_HLLC);
1299f844368SJames Wright }
1309f844368SJames Wright 
1319f844368SJames Wright // Note the identity
1329f844368SJames Wright //
1339f844368SJames Wright // softplus(x) - x = log(1 + exp(x)) - x
1349f844368SJames Wright //                 = log(1 + exp(x)) + log(exp(-x))
1359f844368SJames Wright //                 = log((1 + exp(x)) * exp(-x))
1369f844368SJames Wright //                 = log(exp(-x) + 1)
1379f844368SJames Wright //                 = softplus(-x)
1389f844368SJames Wright CEED_QFUNCTION_HELPER CeedScalar Softplus(CeedScalar x, CeedScalar width) {
1399f844368SJames Wright   if (x > 40 * width) return x;
1409f844368SJames Wright   return width * log1p(exp(x / width));
1419f844368SJames Wright }
1429f844368SJames Wright 
1439f844368SJames Wright CEED_QFUNCTION_HELPER CeedScalar Softplus_fwd(CeedScalar x, CeedScalar dx, CeedScalar width) {
1449f844368SJames Wright   if (x > 40 * width) return 1;
1459f844368SJames Wright   const CeedScalar t = exp(x / width);
1469f844368SJames Wright   return t / (1 + t);
1479f844368SJames Wright }
1489f844368SJames Wright 
1499f844368SJames Wright // Viscous Outflow boundary condition, setting a constant exterior pressure and
1509f844368SJames Wright // temperature as input for a Riemann solve. This condition is stable even in
1519f844368SJames Wright // recirculating flow so long as the exterior temperature is sensible.
1529f844368SJames Wright //
1539f844368SJames Wright // The velocity in the exterior state has optional softplus regularization to
1549f844368SJames Wright // keep it outflow. These parameters have been finnicky in practice and provide
1559f844368SJames Wright // little or no benefit in the tests we've run thus far, thus we recommend
1569f844368SJames Wright // skipping this feature and just allowing recirculation.
1579f844368SJames Wright CEED_QFUNCTION_HELPER int RiemannOutflow(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
1589f844368SJames Wright   // Inputs
1599f844368SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1609f844368SJames Wright   const CeedScalar(*Grad_q)        = in[1];
1619f844368SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
1629f844368SJames Wright 
1639f844368SJames Wright   // Outputs
1649f844368SJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
1659f844368SJames Wright   CeedScalar(*jac_data_sur)  = out[1];
1669f844368SJames Wright 
1679f844368SJames Wright   const OutflowContext           outflow     = (OutflowContext)ctx;
1689f844368SJames Wright   const NewtonianIdealGasContext gas         = &outflow->gas;
1699f844368SJames Wright   const bool                     is_implicit = gas->is_implicit;
1709f844368SJames Wright 
1719f844368SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1729f844368SJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
1739f844368SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
1749f844368SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
1759f844368SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
1769f844368SJames Wright     State            s_int = StateFromQ(gas, qi, state_var);
1779f844368SJames Wright 
1789f844368SJames Wright     StatePrimitive y_ext      = s_int.Y;
1799f844368SJames Wright     y_ext.pressure            = outflow->pressure;
1809f844368SJames Wright     y_ext.temperature         = outflow->temperature;
1819f844368SJames Wright     const CeedScalar u_normal = Dot3(y_ext.velocity, norm);
1829f844368SJames Wright     const CeedScalar proj     = (1 - outflow->recirc) * Softplus(-u_normal, outflow->softplus_velocity);
1839f844368SJames Wright     for (CeedInt j = 0; j < 3; j++) {
1849f844368SJames Wright       y_ext.velocity[j] += norm[j] * proj;  // (I - n n^T) projects into the plane tangent to the normal
1859f844368SJames Wright     }
1869f844368SJames Wright     State s_ext = StateFromPrimitive(gas, y_ext);
1879f844368SJames Wright 
1889f844368SJames Wright     State grad_s[3];
1899f844368SJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, gas, s_int, state_var, Grad_q, dXdx, grad_s);
1909f844368SJames Wright 
1919f844368SJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
1929f844368SJames Wright     KMStrainRate_State(grad_s, strain_rate);
1939f844368SJames Wright     NewtonianStress(gas, strain_rate, kmstress);
1949f844368SJames Wright     KMUnpack(kmstress, stress);
1959f844368SJames Wright     ViscousEnergyFlux(gas, s_int.Y, grad_s, stress, Fe);
1969f844368SJames Wright 
1979f844368SJames Wright     StateConservative F_inviscid_normal = RiemannFlux_HLLC(gas, s_int, s_ext, norm);
1989f844368SJames Wright 
1999f844368SJames Wright     CeedScalar Flux[5];
2009f844368SJames Wright     FluxTotal_RiemannBoundary(F_inviscid_normal, stress, Fe, norm, Flux);
2019f844368SJames Wright 
2029f844368SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
2039f844368SJames Wright 
2049f844368SJames Wright     // Save values for Jacobian
2059f844368SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
2069f844368SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
2079f844368SJames Wright   }
2089f844368SJames Wright   return 0;
2099f844368SJames Wright }
2109f844368SJames Wright 
2119f844368SJames Wright CEED_QFUNCTION(RiemannOutflow_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2129f844368SJames Wright   return RiemannOutflow(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
2139f844368SJames Wright }
2149f844368SJames Wright 
2159f844368SJames Wright CEED_QFUNCTION(RiemannOutflow_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2169f844368SJames Wright   return RiemannOutflow(ctx, Q, in, out, STATEVAR_PRIMITIVE);
2179f844368SJames Wright }
2189f844368SJames Wright 
2199f844368SJames Wright // *****************************************************************************
2209f844368SJames Wright // Jacobian for Riemann pressure/temperature outflow boundary condition
2219f844368SJames Wright // *****************************************************************************
2229f844368SJames Wright CEED_QFUNCTION_HELPER int RiemannOutflow_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
2239f844368SJames Wright                                                   StateVariable state_var) {
2249f844368SJames Wright   // Inputs
2259f844368SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2269f844368SJames Wright   const CeedScalar(*Grad_dq)        = in[1];
2279f844368SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
2289f844368SJames Wright   const CeedScalar(*jac_data_sur)   = in[4];
2299f844368SJames Wright 
2309f844368SJames Wright   // Outputs
2319f844368SJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
2329f844368SJames Wright 
2339f844368SJames Wright   const OutflowContext           outflow     = (OutflowContext)ctx;
2349f844368SJames Wright   const NewtonianIdealGasContext gas         = &outflow->gas;
2359f844368SJames Wright   const bool                     is_implicit = gas->is_implicit;
2369f844368SJames Wright 
2379f844368SJames Wright   // Quadrature Point Loop
2389f844368SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2399f844368SJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
2409f844368SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
2419f844368SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
2429f844368SJames Wright 
2439f844368SJames Wright     CeedScalar qi[5], kmstress[6], dqi[5];
2449f844368SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
2459f844368SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
2469f844368SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
2479f844368SJames Wright 
2489f844368SJames Wright     State          s_int  = StateFromQ(gas, qi, state_var);
2499f844368SJames Wright     const State    ds_int = StateFromQ_fwd(gas, s_int, dqi, state_var);
2509f844368SJames Wright     StatePrimitive y_ext = s_int.Y, dy_ext = ds_int.Y;
2519f844368SJames Wright     y_ext.pressure             = outflow->pressure;
2529f844368SJames Wright     y_ext.temperature          = outflow->temperature;
2539f844368SJames Wright     dy_ext.pressure            = 0;
2549f844368SJames Wright     dy_ext.temperature         = 0;
2559f844368SJames Wright     const CeedScalar u_normal  = Dot3(s_int.Y.velocity, norm);
2569f844368SJames Wright     const CeedScalar du_normal = Dot3(ds_int.Y.velocity, norm);
2579f844368SJames Wright     const CeedScalar proj      = (1 - outflow->recirc) * Softplus(-u_normal, outflow->softplus_velocity);
2589f844368SJames Wright     const CeedScalar dproj     = (1 - outflow->recirc) * Softplus_fwd(-u_normal, -du_normal, outflow->softplus_velocity);
2599f844368SJames Wright     for (CeedInt j = 0; j < 3; j++) {
2609f844368SJames Wright       y_ext.velocity[j] += norm[j] * proj;
2619f844368SJames Wright       dy_ext.velocity[j] += norm[j] * dproj;
2629f844368SJames Wright     }
2639f844368SJames Wright 
2649f844368SJames Wright     State s_ext  = StateFromPrimitive(gas, y_ext);
2659f844368SJames Wright     State ds_ext = StateFromPrimitive_fwd(gas, s_ext, dy_ext);
2669f844368SJames Wright 
2679f844368SJames Wright     State grad_ds[3];
2689f844368SJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, gas, s_int, state_var, Grad_dq, dXdx, grad_ds);
2699f844368SJames Wright 
2709f844368SJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
2719f844368SJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
2729f844368SJames Wright     NewtonianStress(gas, dstrain_rate, dkmstress);
2739f844368SJames Wright     KMUnpack(dkmstress, dstress);
2749f844368SJames Wright     KMUnpack(kmstress, stress);
2759f844368SJames Wright     ViscousEnergyFlux_fwd(gas, s_int.Y, ds_int.Y, grad_ds, stress, dstress, dFe);
2769f844368SJames Wright 
2779f844368SJames Wright     StateConservative dF_inviscid_normal = RiemannFlux_HLLC_fwd(gas, s_int, ds_int, s_ext, ds_ext, norm);
2789f844368SJames Wright 
2799f844368SJames Wright     CeedScalar dFlux[5];
2809f844368SJames Wright     FluxTotal_RiemannBoundary(dF_inviscid_normal, dstress, dFe, norm, dFlux);
2819f844368SJames Wright 
2829f844368SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
2839f844368SJames Wright   }  // End Quadrature Point Loop
2849f844368SJames Wright   return 0;
2859f844368SJames Wright }
2869f844368SJames Wright 
2879f844368SJames Wright CEED_QFUNCTION(RiemannOutflow_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2889f844368SJames Wright   return RiemannOutflow_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
2899f844368SJames Wright }
2909f844368SJames Wright 
2919f844368SJames Wright CEED_QFUNCTION(RiemannOutflow_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2929f844368SJames Wright   return RiemannOutflow_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
2939f844368SJames Wright }
2949f844368SJames Wright 
2959f844368SJames Wright // *****************************************************************************
2969f844368SJames Wright // Outflow boundary condition, weakly setting a constant pressure. This is the
2979f844368SJames Wright // classic outflow condition used by PHASTA-C and retained largely for
2989f844368SJames Wright // comparison. In our experiments, it is never better than RiemannOutflow, and
2999f844368SJames Wright // will crash if outflow ever becomes an inflow, as occurs with strong
3009f844368SJames Wright // acoustics, vortices, etc.
3019f844368SJames Wright // *****************************************************************************
3029f844368SJames Wright CEED_QFUNCTION_HELPER int PressureOutflow(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3039f844368SJames Wright   // Inputs
3049f844368SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3059f844368SJames Wright   const CeedScalar(*Grad_q)        = in[1];
3069f844368SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
3079f844368SJames Wright 
3089f844368SJames Wright   // Outputs
3099f844368SJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
3109f844368SJames Wright   CeedScalar(*jac_data_sur)  = out[1];
3119f844368SJames Wright 
3129f844368SJames Wright   const OutflowContext           outflow     = (OutflowContext)ctx;
3139f844368SJames Wright   const NewtonianIdealGasContext gas         = &outflow->gas;
3149f844368SJames Wright   const bool                     is_implicit = gas->is_implicit;
3159f844368SJames Wright 
3169f844368SJames Wright   // Quadrature Point Loop
3179f844368SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
3189f844368SJames Wright     // Setup
3199f844368SJames Wright     // -- Interp in
3209f844368SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
3219f844368SJames Wright     State            s     = StateFromQ(gas, qi, state_var);
3229f844368SJames Wright     s.Y.pressure           = outflow->pressure;
3239f844368SJames Wright 
3249f844368SJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
3259f844368SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
3269f844368SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
3279f844368SJames Wright 
3289f844368SJames Wright     State grad_s[3];
3299f844368SJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, gas, s, state_var, Grad_q, dXdx, grad_s);
3309f844368SJames Wright 
3319f844368SJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
3329f844368SJames Wright     KMStrainRate_State(grad_s, strain_rate);
3339f844368SJames Wright     NewtonianStress(gas, strain_rate, kmstress);
3349f844368SJames Wright     KMUnpack(kmstress, stress);
3359f844368SJames Wright     ViscousEnergyFlux(gas, s.Y, grad_s, stress, Fe);
3369f844368SJames Wright 
3379f844368SJames Wright     StateConservative F_inviscid[3];
3389f844368SJames Wright     FluxInviscid(gas, s, F_inviscid);
3399f844368SJames Wright 
3409f844368SJames Wright     CeedScalar Flux[5];
3419f844368SJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, norm, Flux);
3429f844368SJames Wright 
3439f844368SJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
3449f844368SJames Wright 
3459f844368SJames Wright     // Save values for Jacobian
3469f844368SJames Wright     StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
3479f844368SJames Wright     StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
3489f844368SJames Wright   }  // End Quadrature Point Loop
3499f844368SJames Wright   return 0;
3509f844368SJames Wright }
3519f844368SJames Wright 
3529f844368SJames Wright CEED_QFUNCTION(PressureOutflow_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3539f844368SJames Wright   return PressureOutflow(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
3549f844368SJames Wright }
3559f844368SJames Wright 
3569f844368SJames Wright CEED_QFUNCTION(PressureOutflow_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3579f844368SJames Wright   return PressureOutflow(ctx, Q, in, out, STATEVAR_PRIMITIVE);
3589f844368SJames Wright }
3599f844368SJames Wright 
3609f844368SJames Wright // *****************************************************************************
3619f844368SJames Wright // Jacobian for weak-pressure outflow boundary condition
3629f844368SJames Wright // *****************************************************************************
3639f844368SJames Wright CEED_QFUNCTION_HELPER int PressureOutflow_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
3649f844368SJames Wright                                                    StateVariable state_var) {
3659f844368SJames Wright   // Inputs
3669f844368SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3679f844368SJames Wright   const CeedScalar(*Grad_dq)        = in[1];
3689f844368SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
3699f844368SJames Wright   const CeedScalar(*jac_data_sur)   = in[4];
3709f844368SJames Wright 
3719f844368SJames Wright   // Outputs
3729f844368SJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
3739f844368SJames Wright 
3749f844368SJames Wright   const OutflowContext           outflow     = (OutflowContext)ctx;
3759f844368SJames Wright   const NewtonianIdealGasContext gas         = &outflow->gas;
3769f844368SJames Wright   const bool                     is_implicit = gas->is_implicit;
3779f844368SJames Wright 
3789f844368SJames Wright   // Quadrature Point Loop
3799f844368SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
3809f844368SJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
3819f844368SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
3829f844368SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
3839f844368SJames Wright 
3849f844368SJames Wright     CeedScalar qi[5], kmstress[6], dqi[5];
3859f844368SJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
3869f844368SJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
3879f844368SJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
3889f844368SJames Wright 
3899f844368SJames Wright     State s       = StateFromQ(gas, qi, state_var);
3909f844368SJames Wright     State ds      = StateFromQ_fwd(gas, s, dqi, state_var);
3919f844368SJames Wright     s.Y.pressure  = outflow->pressure;
3929f844368SJames Wright     ds.Y.pressure = 0.;
3939f844368SJames Wright 
3949f844368SJames Wright     State grad_ds[3];
3959f844368SJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, gas, s, state_var, Grad_dq, dXdx, grad_ds);
3969f844368SJames Wright 
3979f844368SJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
3989f844368SJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
3999f844368SJames Wright     NewtonianStress(gas, dstrain_rate, dkmstress);
4009f844368SJames Wright     KMUnpack(dkmstress, dstress);
4019f844368SJames Wright     KMUnpack(kmstress, stress);
4029f844368SJames Wright     ViscousEnergyFlux_fwd(gas, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
4039f844368SJames Wright 
4049f844368SJames Wright     StateConservative dF_inviscid[3];
4059f844368SJames Wright     FluxInviscid_fwd(gas, s, ds, dF_inviscid);
4069f844368SJames Wright 
4079f844368SJames Wright     CeedScalar dFlux[5];
4089f844368SJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, norm, dFlux);
4099f844368SJames Wright 
4109f844368SJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
4119f844368SJames Wright   }  // End Quadrature Point Loop
4129f844368SJames Wright   return 0;
4139f844368SJames Wright }
4149f844368SJames Wright 
4159f844368SJames Wright CEED_QFUNCTION(PressureOutflow_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4169f844368SJames Wright   return PressureOutflow_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
4179f844368SJames Wright }
4189f844368SJames Wright 
4199f844368SJames Wright CEED_QFUNCTION(PressureOutflow_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4209f844368SJames Wright   return PressureOutflow_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
4219f844368SJames Wright }
422