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