xref: /honee/qfunctions/bc_freestream.h (revision b193fadc19c6de9d0fe6f8bbf4cc6167f09b90af)
1dc936754SJeremy 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 #include "bc_freestream_type.h"
119ed3d70dSJames Wright #include "newtonian_state.h"
129ed3d70dSJames Wright #include "newtonian_types.h"
139ed3d70dSJames Wright #include "riemann_solver.h"
149ed3d70dSJames Wright 
159ed3d70dSJames Wright // *****************************************************************************
169ed3d70dSJames Wright // Freestream Boundary Condition
179ed3d70dSJames Wright // *****************************************************************************
189ed3d70dSJames Wright CEED_QFUNCTION_HELPER int Freestream(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var,
199ed3d70dSJames Wright                                      RiemannFluxType flux_type) {
204b96a86bSJames Wright   const FreestreamContext context  = (FreestreamContext)ctx;
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   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
244b96a86bSJames Wright   CeedScalar(*jac_data_sur)        = context->newtonian_ctx.is_implicit ? out[1] : NULL;
259ed3d70dSJames Wright 
269ed3d70dSJames Wright   const NewtonianIdealGasContext newt_ctx    = &context->newtonian_ctx;
279ed3d70dSJames Wright   const bool                     is_implicit = newt_ctx->is_implicit;
289ed3d70dSJames Wright 
299ed3d70dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
309ed3d70dSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
314b96a86bSJames Wright     const State      s     = StateFromQ(newt_ctx, qi, state_var);
329ed3d70dSJames Wright 
339ed3d70dSJames Wright     CeedScalar wdetJb, norm[3];
349ed3d70dSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
359ed3d70dSJames Wright     wdetJb *= is_implicit ? -1. : 1.;
369ed3d70dSJames Wright 
379ed3d70dSJames Wright     StateConservative flux;
389ed3d70dSJames Wright     switch (flux_type) {
399ed3d70dSJames Wright       case RIEMANN_HLL:
409ed3d70dSJames Wright         flux = RiemannFlux_HLL(newt_ctx, s, context->S_infty, norm);
419ed3d70dSJames Wright         break;
429ed3d70dSJames Wright       case RIEMANN_HLLC:
439ed3d70dSJames Wright         flux = RiemannFlux_HLLC(newt_ctx, s, context->S_infty, norm);
449ed3d70dSJames Wright         break;
459ed3d70dSJames Wright     }
469ed3d70dSJames Wright     CeedScalar Flux[5];
479ed3d70dSJames Wright     UnpackState_U(flux, Flux);
489ed3d70dSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
499ed3d70dSJames Wright 
504b96a86bSJames Wright     if (is_implicit) {
514b96a86bSJames Wright       CeedScalar zeros[6] = {0.};
529ed3d70dSJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
539ed3d70dSJames Wright       StoredValuesPack(Q, i, 5, 6, zeros, jac_data_sur);  // Every output value must be set
549ed3d70dSJames Wright     }
554b96a86bSJames 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) {
1584b96a86bSJames Wright   const OutflowContext outflow     = (OutflowContext)ctx;
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   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
1634b96a86bSJames Wright   CeedScalar(*jac_data_sur)        = outflow->gas.is_implicit ? out[1] : NULL;
1649ed3d70dSJames Wright 
1659ed3d70dSJames Wright   const NewtonianIdealGasContext gas         = &outflow->gas;
1669ed3d70dSJames Wright   const bool                     is_implicit = gas->is_implicit;
1679ed3d70dSJames Wright 
1689ed3d70dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1699ed3d70dSJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
1709ed3d70dSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
1719ed3d70dSJames Wright     wdetJb *= is_implicit ? -1. : 1.;
1729ed3d70dSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
1734b96a86bSJames Wright     const State      s_int = StateFromQ(gas, qi, state_var);
1749ed3d70dSJames Wright 
1759ed3d70dSJames Wright     StatePrimitive y_ext      = s_int.Y;
1769ed3d70dSJames Wright     y_ext.pressure            = outflow->pressure;
1779ed3d70dSJames Wright     y_ext.temperature         = outflow->temperature;
1789ed3d70dSJames Wright     const CeedScalar u_normal = Dot3(y_ext.velocity, norm);
1799ed3d70dSJames Wright     const CeedScalar proj     = (1 - outflow->recirc) * Softplus(-u_normal, outflow->softplus_velocity);
1809ed3d70dSJames Wright     for (CeedInt j = 0; j < 3; j++) {
1819ed3d70dSJames Wright       y_ext.velocity[j] += norm[j] * proj;  // (I - n n^T) projects into the plane tangent to the normal
1829ed3d70dSJames Wright     }
1839ed3d70dSJames Wright     State s_ext = StateFromPrimitive(gas, y_ext);
1849ed3d70dSJames Wright 
1859ed3d70dSJames Wright     State grad_s[3];
1869ed3d70dSJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, gas, s_int, state_var, Grad_q, dXdx, grad_s);
1879ed3d70dSJames Wright 
1889ed3d70dSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
1899ed3d70dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
1909ed3d70dSJames Wright     NewtonianStress(gas, strain_rate, kmstress);
1919ed3d70dSJames Wright     KMUnpack(kmstress, stress);
1929ed3d70dSJames Wright     ViscousEnergyFlux(gas, s_int.Y, grad_s, stress, Fe);
1939ed3d70dSJames Wright 
1949ed3d70dSJames Wright     StateConservative F_inviscid_normal = RiemannFlux_HLLC(gas, s_int, s_ext, norm);
1959ed3d70dSJames Wright 
1969ed3d70dSJames Wright     CeedScalar Flux[5];
1979ed3d70dSJames Wright     FluxTotal_RiemannBoundary(F_inviscid_normal, stress, Fe, norm, Flux);
1989ed3d70dSJames Wright 
1999ed3d70dSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
2009ed3d70dSJames Wright 
2019ed3d70dSJames Wright     // Save values for Jacobian
2024b96a86bSJames Wright     if (is_implicit) {
2039ed3d70dSJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
2049ed3d70dSJames Wright       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
2059ed3d70dSJames Wright     }
2064b96a86bSJames Wright   }
2079ed3d70dSJames Wright   return 0;
2089ed3d70dSJames Wright }
2099ed3d70dSJames Wright 
2109ed3d70dSJames Wright CEED_QFUNCTION(RiemannOutflow_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2119ed3d70dSJames Wright   return RiemannOutflow(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
2129ed3d70dSJames Wright }
2139ed3d70dSJames Wright 
2149ed3d70dSJames Wright CEED_QFUNCTION(RiemannOutflow_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2159ed3d70dSJames Wright   return RiemannOutflow(ctx, Q, in, out, STATEVAR_PRIMITIVE);
2169ed3d70dSJames Wright }
2179ed3d70dSJames Wright 
2189ed3d70dSJames Wright // *****************************************************************************
2199ed3d70dSJames Wright // Jacobian for Riemann pressure/temperature outflow boundary condition
2209ed3d70dSJames Wright // *****************************************************************************
2219ed3d70dSJames Wright CEED_QFUNCTION_HELPER int RiemannOutflow_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
2229ed3d70dSJames Wright                                                   StateVariable state_var) {
2239ed3d70dSJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2249ed3d70dSJames Wright   const CeedScalar(*Grad_dq)        = in[1];
2259ed3d70dSJames Wright   const CeedScalar(*q_data_sur)     = in[2];
2269ed3d70dSJames Wright   const CeedScalar(*jac_data_sur)   = in[4];
2279ed3d70dSJames Wright   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
2289ed3d70dSJames Wright 
2299ed3d70dSJames Wright   const OutflowContext           outflow     = (OutflowContext)ctx;
2309ed3d70dSJames Wright   const NewtonianIdealGasContext gas         = &outflow->gas;
2319ed3d70dSJames Wright   const bool                     is_implicit = gas->is_implicit;
2329ed3d70dSJames Wright 
2339ed3d70dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2349ed3d70dSJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
2359ed3d70dSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
2369ed3d70dSJames Wright     wdetJb *= is_implicit ? -1. : 1.;
2379ed3d70dSJames Wright 
2389ed3d70dSJames Wright     CeedScalar qi[5], kmstress[6], dqi[5];
2399ed3d70dSJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
2409ed3d70dSJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
2419ed3d70dSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
2429ed3d70dSJames Wright 
2439ed3d70dSJames Wright     State          s_int  = StateFromQ(gas, qi, state_var);
2449ed3d70dSJames Wright     const State    ds_int = StateFromQ_fwd(gas, s_int, dqi, state_var);
2459ed3d70dSJames Wright     StatePrimitive y_ext = s_int.Y, dy_ext = ds_int.Y;
2469ed3d70dSJames Wright     y_ext.pressure             = outflow->pressure;
2479ed3d70dSJames Wright     y_ext.temperature          = outflow->temperature;
2489ed3d70dSJames Wright     dy_ext.pressure            = 0;
2499ed3d70dSJames Wright     dy_ext.temperature         = 0;
2509ed3d70dSJames Wright     const CeedScalar u_normal  = Dot3(s_int.Y.velocity, norm);
2519ed3d70dSJames Wright     const CeedScalar du_normal = Dot3(ds_int.Y.velocity, norm);
2529ed3d70dSJames Wright     const CeedScalar proj      = (1 - outflow->recirc) * Softplus(-u_normal, outflow->softplus_velocity);
2539ed3d70dSJames Wright     const CeedScalar dproj     = (1 - outflow->recirc) * Softplus_fwd(-u_normal, -du_normal, outflow->softplus_velocity);
2549ed3d70dSJames Wright     for (CeedInt j = 0; j < 3; j++) {
2559ed3d70dSJames Wright       y_ext.velocity[j] += norm[j] * proj;
2569ed3d70dSJames Wright       dy_ext.velocity[j] += norm[j] * dproj;
2579ed3d70dSJames Wright     }
2589ed3d70dSJames Wright 
2599ed3d70dSJames Wright     State s_ext  = StateFromPrimitive(gas, y_ext);
2609ed3d70dSJames Wright     State ds_ext = StateFromPrimitive_fwd(gas, s_ext, dy_ext);
2619ed3d70dSJames Wright 
2629ed3d70dSJames Wright     State grad_ds[3];
2639ed3d70dSJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, gas, s_int, state_var, Grad_dq, dXdx, grad_ds);
2649ed3d70dSJames Wright 
2659ed3d70dSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
2669ed3d70dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
2679ed3d70dSJames Wright     NewtonianStress(gas, dstrain_rate, dkmstress);
2689ed3d70dSJames Wright     KMUnpack(dkmstress, dstress);
2699ed3d70dSJames Wright     KMUnpack(kmstress, stress);
2709ed3d70dSJames Wright     ViscousEnergyFlux_fwd(gas, s_int.Y, ds_int.Y, grad_ds, stress, dstress, dFe);
2719ed3d70dSJames Wright 
2729ed3d70dSJames Wright     StateConservative dF_inviscid_normal = RiemannFlux_HLLC_fwd(gas, s_int, ds_int, s_ext, ds_ext, norm);
2739ed3d70dSJames Wright 
2749ed3d70dSJames Wright     CeedScalar dFlux[5];
2759ed3d70dSJames Wright     FluxTotal_RiemannBoundary(dF_inviscid_normal, dstress, dFe, norm, dFlux);
2769ed3d70dSJames Wright 
2779ed3d70dSJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
278*b193fadcSJames Wright   }
2799ed3d70dSJames Wright   return 0;
2809ed3d70dSJames Wright }
2819ed3d70dSJames Wright 
2829ed3d70dSJames Wright CEED_QFUNCTION(RiemannOutflow_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2839ed3d70dSJames Wright   return RiemannOutflow_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
2849ed3d70dSJames Wright }
2859ed3d70dSJames Wright 
2869ed3d70dSJames Wright CEED_QFUNCTION(RiemannOutflow_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2879ed3d70dSJames Wright   return RiemannOutflow_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
2889ed3d70dSJames Wright }
2899ed3d70dSJames Wright 
2909ed3d70dSJames Wright // *****************************************************************************
2919ed3d70dSJames Wright // Outflow boundary condition, weakly setting a constant pressure. This is the
2929ed3d70dSJames Wright // classic outflow condition used by PHASTA-C and retained largely for
2939ed3d70dSJames Wright // comparison. In our experiments, it is never better than RiemannOutflow, and
2949ed3d70dSJames Wright // will crash if outflow ever becomes an inflow, as occurs with strong
2959ed3d70dSJames Wright // acoustics, vortices, etc.
2969ed3d70dSJames Wright // *****************************************************************************
2979ed3d70dSJames Wright CEED_QFUNCTION_HELPER int PressureOutflow(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
2984b96a86bSJames Wright   const OutflowContext outflow     = (OutflowContext)ctx;
2999ed3d70dSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3009ed3d70dSJames Wright   const CeedScalar(*Grad_q)        = in[1];
3019ed3d70dSJames Wright   const CeedScalar(*q_data_sur)    = in[2];
3029ed3d70dSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
3034b96a86bSJames Wright   CeedScalar(*jac_data_sur)        = outflow->gas.is_implicit ? out[1] : NULL;
3049ed3d70dSJames Wright 
3059ed3d70dSJames Wright   const NewtonianIdealGasContext gas         = &outflow->gas;
3069ed3d70dSJames Wright   const bool                     is_implicit = gas->is_implicit;
3079ed3d70dSJames Wright 
3089ed3d70dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
3099ed3d70dSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
3109ed3d70dSJames Wright     State            s     = StateFromQ(gas, qi, state_var);
3119ed3d70dSJames Wright     s.Y.pressure           = outflow->pressure;
3129ed3d70dSJames Wright 
3139ed3d70dSJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
3149ed3d70dSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
3159ed3d70dSJames Wright     wdetJb *= is_implicit ? -1. : 1.;
3169ed3d70dSJames Wright 
3179ed3d70dSJames Wright     State grad_s[3];
3189ed3d70dSJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, gas, s, state_var, Grad_q, dXdx, grad_s);
3199ed3d70dSJames Wright 
3209ed3d70dSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
3219ed3d70dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
3229ed3d70dSJames Wright     NewtonianStress(gas, strain_rate, kmstress);
3239ed3d70dSJames Wright     KMUnpack(kmstress, stress);
3249ed3d70dSJames Wright     ViscousEnergyFlux(gas, s.Y, grad_s, stress, Fe);
3259ed3d70dSJames Wright 
3269ed3d70dSJames Wright     StateConservative F_inviscid[3];
3279ed3d70dSJames Wright     FluxInviscid(gas, s, F_inviscid);
3289ed3d70dSJames Wright 
3299ed3d70dSJames Wright     CeedScalar Flux[5];
3309ed3d70dSJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, norm, Flux);
3319ed3d70dSJames Wright 
3329ed3d70dSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
3339ed3d70dSJames Wright 
3349ed3d70dSJames Wright     // Save values for Jacobian
3354b96a86bSJames Wright     if (is_implicit) {
3369ed3d70dSJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
3379ed3d70dSJames Wright       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
3384b96a86bSJames Wright     }
3394b96a86bSJames Wright   }
3409ed3d70dSJames Wright   return 0;
3419ed3d70dSJames Wright }
3429ed3d70dSJames Wright 
3439ed3d70dSJames Wright CEED_QFUNCTION(PressureOutflow_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3449ed3d70dSJames Wright   return PressureOutflow(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
3459ed3d70dSJames Wright }
3469ed3d70dSJames Wright 
3479ed3d70dSJames Wright CEED_QFUNCTION(PressureOutflow_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3489ed3d70dSJames Wright   return PressureOutflow(ctx, Q, in, out, STATEVAR_PRIMITIVE);
3499ed3d70dSJames Wright }
3509ed3d70dSJames Wright 
3519ed3d70dSJames Wright // *****************************************************************************
3529ed3d70dSJames Wright // Jacobian for weak-pressure outflow boundary condition
3539ed3d70dSJames Wright // *****************************************************************************
3549ed3d70dSJames Wright CEED_QFUNCTION_HELPER int PressureOutflow_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
3559ed3d70dSJames Wright                                                    StateVariable state_var) {
3569ed3d70dSJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3579ed3d70dSJames Wright   const CeedScalar(*Grad_dq)        = in[1];
3589ed3d70dSJames Wright   const CeedScalar(*q_data_sur)     = in[2];
3599ed3d70dSJames Wright   const CeedScalar(*jac_data_sur)   = in[4];
3609ed3d70dSJames Wright   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
3619ed3d70dSJames Wright 
3629ed3d70dSJames Wright   const OutflowContext           outflow     = (OutflowContext)ctx;
3639ed3d70dSJames Wright   const NewtonianIdealGasContext gas         = &outflow->gas;
3649ed3d70dSJames Wright   const bool                     is_implicit = gas->is_implicit;
3659ed3d70dSJames Wright 
3669ed3d70dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
3679ed3d70dSJames Wright     CeedScalar wdetJb, dXdx[2][3], norm[3];
3689ed3d70dSJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, norm);
3699ed3d70dSJames Wright     wdetJb *= is_implicit ? -1. : 1.;
3709ed3d70dSJames Wright 
3719ed3d70dSJames Wright     CeedScalar qi[5], kmstress[6], dqi[5];
3729ed3d70dSJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
3739ed3d70dSJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
3749ed3d70dSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
3759ed3d70dSJames Wright 
3769ed3d70dSJames Wright     State s       = StateFromQ(gas, qi, state_var);
3779ed3d70dSJames Wright     State ds      = StateFromQ_fwd(gas, s, dqi, state_var);
3789ed3d70dSJames Wright     s.Y.pressure  = outflow->pressure;
3799ed3d70dSJames Wright     ds.Y.pressure = 0.;
3809ed3d70dSJames Wright 
3819ed3d70dSJames Wright     State grad_ds[3];
3829ed3d70dSJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, gas, s, state_var, Grad_dq, dXdx, grad_ds);
3839ed3d70dSJames Wright 
3849ed3d70dSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
3859ed3d70dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
3869ed3d70dSJames Wright     NewtonianStress(gas, dstrain_rate, dkmstress);
3879ed3d70dSJames Wright     KMUnpack(dkmstress, dstress);
3889ed3d70dSJames Wright     KMUnpack(kmstress, stress);
3899ed3d70dSJames Wright     ViscousEnergyFlux_fwd(gas, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
3909ed3d70dSJames Wright 
3919ed3d70dSJames Wright     StateConservative dF_inviscid[3];
3929ed3d70dSJames Wright     FluxInviscid_fwd(gas, s, ds, dF_inviscid);
3939ed3d70dSJames Wright 
3949ed3d70dSJames Wright     CeedScalar dFlux[5];
3959ed3d70dSJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, norm, dFlux);
3969ed3d70dSJames Wright 
3979ed3d70dSJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
398*b193fadcSJames Wright   }
3999ed3d70dSJames Wright   return 0;
4009ed3d70dSJames Wright }
4019ed3d70dSJames Wright 
4029ed3d70dSJames Wright CEED_QFUNCTION(PressureOutflow_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4039ed3d70dSJames Wright   return PressureOutflow_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
4049ed3d70dSJames Wright }
4059ed3d70dSJames Wright 
4069ed3d70dSJames Wright CEED_QFUNCTION(PressureOutflow_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4079ed3d70dSJames Wright   return PressureOutflow_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
4089ed3d70dSJames Wright }
409