xref: /honee/qfunctions/bc_freestream.h (revision ae2b091fac884a554e48acc4b4c187524c2a2818)
1*ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2*ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
39ed3d70dSJames Wright 
49ed3d70dSJames Wright /// @file
59ed3d70dSJames Wright /// QFunctions for the `bc_freestream` and `bc_outflow` boundary conditions
69ed3d70dSJames Wright #include "bc_freestream_type.h"
79ed3d70dSJames Wright #include "newtonian_state.h"
89ed3d70dSJames Wright #include "newtonian_types.h"
99ed3d70dSJames Wright #include "riemann_solver.h"
109ed3d70dSJames Wright 
119ed3d70dSJames Wright // *****************************************************************************
129ed3d70dSJames Wright // Freestream Boundary Condition
139ed3d70dSJames Wright // *****************************************************************************
149ed3d70dSJames Wright CEED_QFUNCTION_HELPER int Freestream(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var,
159ed3d70dSJames Wright                                      RiemannFluxType flux_type) {
164b96a86bSJames Wright   const FreestreamContext context  = (FreestreamContext)ctx;
179ed3d70dSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
189ed3d70dSJames Wright   const CeedScalar(*q_data_sur)    = in[2];
199ed3d70dSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
204b96a86bSJames Wright   CeedScalar(*jac_data_sur)        = context->newtonian_ctx.is_implicit ? out[1] : NULL;
219ed3d70dSJames Wright 
229ed3d70dSJames Wright   const NewtonianIdealGasContext newt_ctx    = &context->newtonian_ctx;
239ed3d70dSJames Wright   const bool                     is_implicit = newt_ctx->is_implicit;
249ed3d70dSJames Wright 
259ed3d70dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
269ed3d70dSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
274b96a86bSJames Wright     const State      s     = StateFromQ(newt_ctx, qi, state_var);
289ed3d70dSJames Wright 
29feb491a7SJames Wright     CeedScalar wdetJb, normal[3];
30feb491a7SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, normal);
319ed3d70dSJames Wright     wdetJb *= is_implicit ? -1. : 1.;
329ed3d70dSJames Wright 
339ed3d70dSJames Wright     StateConservative flux;
349ed3d70dSJames Wright     switch (flux_type) {
359ed3d70dSJames Wright       case RIEMANN_HLL:
36feb491a7SJames Wright         flux = RiemannFlux_HLL(newt_ctx, s, context->S_infty, normal);
379ed3d70dSJames Wright         break;
389ed3d70dSJames Wright       case RIEMANN_HLLC:
39feb491a7SJames Wright         flux = RiemannFlux_HLLC(newt_ctx, s, context->S_infty, normal);
409ed3d70dSJames Wright         break;
419ed3d70dSJames Wright     }
429ed3d70dSJames Wright     CeedScalar Flux[5];
439ed3d70dSJames Wright     UnpackState_U(flux, Flux);
449ed3d70dSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
459ed3d70dSJames Wright 
464b96a86bSJames Wright     if (is_implicit) {
474b96a86bSJames Wright       CeedScalar zeros[6] = {0.};
489ed3d70dSJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
499ed3d70dSJames Wright       StoredValuesPack(Q, i, 5, 6, zeros, jac_data_sur);  // Every output value must be set
509ed3d70dSJames Wright     }
514b96a86bSJames Wright   }
529ed3d70dSJames Wright   return 0;
539ed3d70dSJames Wright }
549ed3d70dSJames Wright 
559ed3d70dSJames Wright CEED_QFUNCTION(Freestream_Conserv_HLL)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
569ed3d70dSJames Wright   return Freestream(ctx, Q, in, out, STATEVAR_CONSERVATIVE, RIEMANN_HLL);
579ed3d70dSJames Wright }
589ed3d70dSJames Wright 
599ed3d70dSJames Wright CEED_QFUNCTION(Freestream_Prim_HLL)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
609ed3d70dSJames Wright   return Freestream(ctx, Q, in, out, STATEVAR_PRIMITIVE, RIEMANN_HLL);
619ed3d70dSJames Wright }
629ed3d70dSJames Wright 
639b103f75SJames Wright CEED_QFUNCTION(Freestream_Entropy_HLL)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
649b103f75SJames Wright   return Freestream(ctx, Q, in, out, STATEVAR_ENTROPY, RIEMANN_HLL);
659b103f75SJames Wright }
669b103f75SJames 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 
759b103f75SJames Wright CEED_QFUNCTION(Freestream_Entropy_HLLC)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
769b103f75SJames Wright   return Freestream(ctx, Q, in, out, STATEVAR_ENTROPY, RIEMANN_HLLC);
779b103f75SJames Wright }
789b103f75SJames Wright 
799ed3d70dSJames Wright CEED_QFUNCTION_HELPER int Freestream_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var,
809ed3d70dSJames Wright                                               RiemannFluxType flux_type) {
819ed3d70dSJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
829ed3d70dSJames Wright   const CeedScalar(*q_data_sur)     = in[2];
839ed3d70dSJames Wright   const CeedScalar(*jac_data_sur)   = in[4];
849ed3d70dSJames Wright 
859ed3d70dSJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
869ed3d70dSJames Wright 
879ed3d70dSJames Wright   const FreestreamContext        context     = (FreestreamContext)ctx;
889ed3d70dSJames Wright   const NewtonianIdealGasContext newt_ctx    = &context->newtonian_ctx;
899ed3d70dSJames Wright   const bool                     is_implicit = newt_ctx->is_implicit;
909ed3d70dSJames Wright   const State                    dS_infty    = {0};
919ed3d70dSJames Wright 
929ed3d70dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
93feb491a7SJames Wright     CeedScalar wdetJb, normal[3];
94feb491a7SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, normal);
959ed3d70dSJames Wright     wdetJb *= is_implicit ? -1. : 1.;
969ed3d70dSJames Wright 
979ed3d70dSJames Wright     CeedScalar qi[5], dqi[5];
989ed3d70dSJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
999ed3d70dSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
1009ed3d70dSJames Wright     State s  = StateFromQ(newt_ctx, qi, state_var);
1019ed3d70dSJames Wright     State ds = StateFromQ_fwd(newt_ctx, s, dqi, state_var);
1029ed3d70dSJames Wright 
1039ed3d70dSJames Wright     StateConservative dflux;
1049ed3d70dSJames Wright     switch (flux_type) {
1059ed3d70dSJames Wright       case RIEMANN_HLL:
106feb491a7SJames Wright         dflux = RiemannFlux_HLL_fwd(newt_ctx, s, ds, context->S_infty, dS_infty, normal);
1079ed3d70dSJames Wright         break;
1089ed3d70dSJames Wright       case RIEMANN_HLLC:
109feb491a7SJames Wright         dflux = RiemannFlux_HLLC_fwd(newt_ctx, s, ds, context->S_infty, dS_infty, normal);
1109ed3d70dSJames Wright         break;
1119ed3d70dSJames Wright     }
1129ed3d70dSJames Wright     CeedScalar dFlux[5];
1139ed3d70dSJames Wright     UnpackState_U(dflux, dFlux);
1149ed3d70dSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
1159ed3d70dSJames Wright   }
1169ed3d70dSJames Wright   return 0;
1179ed3d70dSJames Wright }
1189ed3d70dSJames Wright 
1199ed3d70dSJames Wright CEED_QFUNCTION(Freestream_Jacobian_Conserv_HLL)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1209ed3d70dSJames Wright   return Freestream_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE, RIEMANN_HLL);
1219ed3d70dSJames Wright }
1229ed3d70dSJames Wright 
1239ed3d70dSJames Wright CEED_QFUNCTION(Freestream_Jacobian_Prim_HLL)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1249ed3d70dSJames Wright   return Freestream_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE, RIEMANN_HLL);
1259ed3d70dSJames Wright }
1269ed3d70dSJames Wright 
1279b103f75SJames Wright CEED_QFUNCTION(Freestream_Jacobian_Entropy_HLL)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1289b103f75SJames Wright   return Freestream_Jacobian(ctx, Q, in, out, STATEVAR_ENTROPY, RIEMANN_HLL);
1299b103f75SJames Wright }
1309b103f75SJames Wright 
1319ed3d70dSJames Wright CEED_QFUNCTION(Freestream_Jacobian_Conserv_HLLC)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1329ed3d70dSJames Wright   return Freestream_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE, RIEMANN_HLLC);
1339ed3d70dSJames Wright }
1349ed3d70dSJames Wright 
1359ed3d70dSJames Wright CEED_QFUNCTION(Freestream_Jacobian_Prim_HLLC)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1369ed3d70dSJames Wright   return Freestream_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE, RIEMANN_HLLC);
1379ed3d70dSJames Wright }
1389ed3d70dSJames Wright 
1399b103f75SJames Wright CEED_QFUNCTION(Freestream_Jacobian_Entropy_HLLC)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1409b103f75SJames Wright   return Freestream_Jacobian(ctx, Q, in, out, STATEVAR_ENTROPY, RIEMANN_HLLC);
1419b103f75SJames Wright }
1429b103f75SJames Wright 
1439ed3d70dSJames Wright // Note the identity
1449ed3d70dSJames Wright //
1459ed3d70dSJames Wright // softplus(x) - x = log(1 + exp(x)) - x
1469ed3d70dSJames Wright //                 = log(1 + exp(x)) + log(exp(-x))
1479ed3d70dSJames Wright //                 = log((1 + exp(x)) * exp(-x))
1489ed3d70dSJames Wright //                 = log(exp(-x) + 1)
1499ed3d70dSJames Wright //                 = softplus(-x)
1509ed3d70dSJames Wright CEED_QFUNCTION_HELPER CeedScalar Softplus(CeedScalar x, CeedScalar width) {
1519ed3d70dSJames Wright   if (x > 40 * width) return x;
1529ed3d70dSJames Wright   return width * log1p(exp(x / width));
1539ed3d70dSJames Wright }
1549ed3d70dSJames Wright 
1559ed3d70dSJames Wright CEED_QFUNCTION_HELPER CeedScalar Softplus_fwd(CeedScalar x, CeedScalar dx, CeedScalar width) {
1569ed3d70dSJames Wright   if (x > 40 * width) return 1;
1579ed3d70dSJames Wright   const CeedScalar t = exp(x / width);
1589ed3d70dSJames Wright   return t / (1 + t);
1599ed3d70dSJames Wright }
1609ed3d70dSJames Wright 
1619ed3d70dSJames Wright // Viscous Outflow boundary condition, setting a constant exterior pressure and
1629ed3d70dSJames Wright // temperature as input for a Riemann solve. This condition is stable even in
1639ed3d70dSJames Wright // recirculating flow so long as the exterior temperature is sensible.
1649ed3d70dSJames Wright //
1659ed3d70dSJames Wright // The velocity in the exterior state has optional softplus regularization to
1669ed3d70dSJames Wright // keep it outflow. These parameters have been finnicky in practice and provide
1679ed3d70dSJames Wright // little or no benefit in the tests we've run thus far, thus we recommend
1689ed3d70dSJames Wright // skipping this feature and just allowing recirculation.
1699ed3d70dSJames Wright CEED_QFUNCTION_HELPER int RiemannOutflow(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
1704b96a86bSJames Wright   const OutflowContext outflow     = (OutflowContext)ctx;
1719ed3d70dSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1729ed3d70dSJames Wright   const CeedScalar(*Grad_q)        = in[1];
1739ed3d70dSJames Wright   const CeedScalar(*q_data_sur)    = in[2];
1749ed3d70dSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
1754b96a86bSJames Wright   CeedScalar(*jac_data_sur)        = outflow->gas.is_implicit ? out[1] : NULL;
1769ed3d70dSJames Wright 
1779ed3d70dSJames Wright   const NewtonianIdealGasContext gas         = &outflow->gas;
1789ed3d70dSJames Wright   const bool                     is_implicit = gas->is_implicit;
1799ed3d70dSJames Wright 
1809ed3d70dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
181feb491a7SJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
182feb491a7SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
1839ed3d70dSJames Wright     wdetJb *= is_implicit ? -1. : 1.;
1849ed3d70dSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
1854b96a86bSJames Wright     const State      s_int = StateFromQ(gas, qi, state_var);
1869ed3d70dSJames Wright 
1879ed3d70dSJames Wright     StatePrimitive y_ext      = s_int.Y;
1889ed3d70dSJames Wright     y_ext.pressure            = outflow->pressure;
1899ed3d70dSJames Wright     y_ext.temperature         = outflow->temperature;
190feb491a7SJames Wright     const CeedScalar u_normal = Dot3(y_ext.velocity, normal);
1919ed3d70dSJames Wright     const CeedScalar proj     = (1 - outflow->recirc) * Softplus(-u_normal, outflow->softplus_velocity);
1929ed3d70dSJames Wright     for (CeedInt j = 0; j < 3; j++) {
193feb491a7SJames Wright       y_ext.velocity[j] += normal[j] * proj;  // (I - n n^T) projects into the plane tangent to the normal
1949ed3d70dSJames Wright     }
1959ed3d70dSJames Wright     State s_ext = StateFromPrimitive(gas, y_ext);
1969ed3d70dSJames Wright 
1979ed3d70dSJames Wright     State grad_s[3];
1989ed3d70dSJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, gas, s_int, state_var, Grad_q, dXdx, grad_s);
1999ed3d70dSJames Wright 
2009ed3d70dSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
2019ed3d70dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
2029ed3d70dSJames Wright     NewtonianStress(gas, strain_rate, kmstress);
2039ed3d70dSJames Wright     KMUnpack(kmstress, stress);
2049ed3d70dSJames Wright     ViscousEnergyFlux(gas, s_int.Y, grad_s, stress, Fe);
2059ed3d70dSJames Wright 
206feb491a7SJames Wright     StateConservative F_inviscid_normal = RiemannFlux_HLLC(gas, s_int, s_ext, normal);
2079ed3d70dSJames Wright 
2089ed3d70dSJames Wright     CeedScalar Flux[5];
209feb491a7SJames Wright     FluxTotal_RiemannBoundary(F_inviscid_normal, stress, Fe, normal, Flux);
2109ed3d70dSJames Wright 
2119ed3d70dSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
2129ed3d70dSJames Wright 
2139ed3d70dSJames Wright     // Save values for Jacobian
2144b96a86bSJames Wright     if (is_implicit) {
2159ed3d70dSJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
2169ed3d70dSJames Wright       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
2179ed3d70dSJames Wright     }
2184b96a86bSJames Wright   }
2199ed3d70dSJames Wright   return 0;
2209ed3d70dSJames Wright }
2219ed3d70dSJames Wright 
2229ed3d70dSJames Wright CEED_QFUNCTION(RiemannOutflow_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2239ed3d70dSJames Wright   return RiemannOutflow(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
2249ed3d70dSJames Wright }
2259ed3d70dSJames Wright 
2269ed3d70dSJames Wright CEED_QFUNCTION(RiemannOutflow_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2279ed3d70dSJames Wright   return RiemannOutflow(ctx, Q, in, out, STATEVAR_PRIMITIVE);
2289ed3d70dSJames Wright }
2299ed3d70dSJames Wright 
2309b103f75SJames Wright CEED_QFUNCTION(RiemannOutflow_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2319b103f75SJames Wright   return RiemannOutflow(ctx, Q, in, out, STATEVAR_ENTROPY);
2329b103f75SJames Wright }
2339b103f75SJames Wright 
2349ed3d70dSJames Wright // *****************************************************************************
2359ed3d70dSJames Wright // Jacobian for Riemann pressure/temperature outflow boundary condition
2369ed3d70dSJames Wright // *****************************************************************************
2379ed3d70dSJames Wright CEED_QFUNCTION_HELPER int RiemannOutflow_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
2389ed3d70dSJames Wright                                                   StateVariable state_var) {
2399ed3d70dSJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2409ed3d70dSJames Wright   const CeedScalar(*Grad_dq)        = in[1];
2419ed3d70dSJames Wright   const CeedScalar(*q_data_sur)     = in[2];
2429ed3d70dSJames Wright   const CeedScalar(*jac_data_sur)   = in[4];
2439ed3d70dSJames Wright   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
2449ed3d70dSJames Wright 
2459ed3d70dSJames Wright   const OutflowContext           outflow     = (OutflowContext)ctx;
2469ed3d70dSJames Wright   const NewtonianIdealGasContext gas         = &outflow->gas;
2479ed3d70dSJames Wright   const bool                     is_implicit = gas->is_implicit;
2489ed3d70dSJames Wright 
2499ed3d70dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
250feb491a7SJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
251feb491a7SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
2529ed3d70dSJames Wright     wdetJb *= is_implicit ? -1. : 1.;
2539ed3d70dSJames Wright 
2549ed3d70dSJames Wright     CeedScalar qi[5], kmstress[6], dqi[5];
2559ed3d70dSJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
2569ed3d70dSJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
2579ed3d70dSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
2589ed3d70dSJames Wright 
2599ed3d70dSJames Wright     State          s_int  = StateFromQ(gas, qi, state_var);
2609ed3d70dSJames Wright     const State    ds_int = StateFromQ_fwd(gas, s_int, dqi, state_var);
2619ed3d70dSJames Wright     StatePrimitive y_ext = s_int.Y, dy_ext = ds_int.Y;
2629ed3d70dSJames Wright     y_ext.pressure             = outflow->pressure;
2639ed3d70dSJames Wright     y_ext.temperature          = outflow->temperature;
2649ed3d70dSJames Wright     dy_ext.pressure            = 0;
2659ed3d70dSJames Wright     dy_ext.temperature         = 0;
266feb491a7SJames Wright     const CeedScalar u_normal  = Dot3(s_int.Y.velocity, normal);
267feb491a7SJames Wright     const CeedScalar du_normal = Dot3(ds_int.Y.velocity, normal);
2689ed3d70dSJames Wright     const CeedScalar proj      = (1 - outflow->recirc) * Softplus(-u_normal, outflow->softplus_velocity);
2699ed3d70dSJames Wright     const CeedScalar dproj     = (1 - outflow->recirc) * Softplus_fwd(-u_normal, -du_normal, outflow->softplus_velocity);
2709ed3d70dSJames Wright     for (CeedInt j = 0; j < 3; j++) {
271feb491a7SJames Wright       y_ext.velocity[j] += normal[j] * proj;
272feb491a7SJames Wright       dy_ext.velocity[j] += normal[j] * dproj;
2739ed3d70dSJames Wright     }
2749ed3d70dSJames Wright 
2759ed3d70dSJames Wright     State s_ext  = StateFromPrimitive(gas, y_ext);
2769ed3d70dSJames Wright     State ds_ext = StateFromPrimitive_fwd(gas, s_ext, dy_ext);
2779ed3d70dSJames Wright 
2789ed3d70dSJames Wright     State grad_ds[3];
2799ed3d70dSJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, gas, s_int, state_var, Grad_dq, dXdx, grad_ds);
2809ed3d70dSJames Wright 
2819ed3d70dSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
2829ed3d70dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
2839ed3d70dSJames Wright     NewtonianStress(gas, dstrain_rate, dkmstress);
2849ed3d70dSJames Wright     KMUnpack(dkmstress, dstress);
2859ed3d70dSJames Wright     KMUnpack(kmstress, stress);
2869ed3d70dSJames Wright     ViscousEnergyFlux_fwd(gas, s_int.Y, ds_int.Y, grad_ds, stress, dstress, dFe);
2879ed3d70dSJames Wright 
288feb491a7SJames Wright     StateConservative dF_inviscid_normal = RiemannFlux_HLLC_fwd(gas, s_int, ds_int, s_ext, ds_ext, normal);
2899ed3d70dSJames Wright 
2909ed3d70dSJames Wright     CeedScalar dFlux[5];
291feb491a7SJames Wright     FluxTotal_RiemannBoundary(dF_inviscid_normal, dstress, dFe, normal, dFlux);
2929ed3d70dSJames Wright 
2939ed3d70dSJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
294b193fadcSJames Wright   }
2959ed3d70dSJames Wright   return 0;
2969ed3d70dSJames Wright }
2979ed3d70dSJames Wright 
2989ed3d70dSJames Wright CEED_QFUNCTION(RiemannOutflow_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2999ed3d70dSJames Wright   return RiemannOutflow_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
3009ed3d70dSJames Wright }
3019ed3d70dSJames Wright 
3029ed3d70dSJames Wright CEED_QFUNCTION(RiemannOutflow_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3039ed3d70dSJames Wright   return RiemannOutflow_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
3049ed3d70dSJames Wright }
3059ed3d70dSJames Wright 
3069b103f75SJames Wright CEED_QFUNCTION(RiemannOutflow_Jacobian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3079b103f75SJames Wright   return RiemannOutflow_Jacobian(ctx, Q, in, out, STATEVAR_ENTROPY);
3089b103f75SJames Wright }
3099b103f75SJames Wright 
3109ed3d70dSJames Wright // *****************************************************************************
3119ed3d70dSJames Wright // Outflow boundary condition, weakly setting a constant pressure. This is the
3129ed3d70dSJames Wright // classic outflow condition used by PHASTA-C and retained largely for
3139ed3d70dSJames Wright // comparison. In our experiments, it is never better than RiemannOutflow, and
3149ed3d70dSJames Wright // will crash if outflow ever becomes an inflow, as occurs with strong
3159ed3d70dSJames Wright // acoustics, vortices, etc.
3169ed3d70dSJames Wright // *****************************************************************************
3179ed3d70dSJames Wright CEED_QFUNCTION_HELPER int PressureOutflow(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, StateVariable state_var) {
3184b96a86bSJames Wright   const OutflowContext outflow     = (OutflowContext)ctx;
3199ed3d70dSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3209ed3d70dSJames Wright   const CeedScalar(*Grad_q)        = in[1];
3219ed3d70dSJames Wright   const CeedScalar(*q_data_sur)    = in[2];
3229ed3d70dSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
3234b96a86bSJames Wright   CeedScalar(*jac_data_sur)        = outflow->gas.is_implicit ? out[1] : NULL;
3249ed3d70dSJames Wright 
3259ed3d70dSJames Wright   const NewtonianIdealGasContext gas         = &outflow->gas;
3269ed3d70dSJames Wright   const bool                     is_implicit = gas->is_implicit;
3279ed3d70dSJames Wright 
3289ed3d70dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
3299ed3d70dSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
3309ed3d70dSJames Wright     State            s     = StateFromQ(gas, qi, state_var);
3319ed3d70dSJames Wright     s.Y.pressure           = outflow->pressure;
3329ed3d70dSJames Wright 
333feb491a7SJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
334feb491a7SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
3359ed3d70dSJames Wright     wdetJb *= is_implicit ? -1. : 1.;
3369ed3d70dSJames Wright 
3379ed3d70dSJames Wright     State grad_s[3];
3389ed3d70dSJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, gas, s, state_var, Grad_q, dXdx, grad_s);
3399ed3d70dSJames Wright 
3409ed3d70dSJames Wright     CeedScalar strain_rate[6], kmstress[6], stress[3][3], Fe[3];
3419ed3d70dSJames Wright     KMStrainRate_State(grad_s, strain_rate);
3429ed3d70dSJames Wright     NewtonianStress(gas, strain_rate, kmstress);
3439ed3d70dSJames Wright     KMUnpack(kmstress, stress);
3449ed3d70dSJames Wright     ViscousEnergyFlux(gas, s.Y, grad_s, stress, Fe);
3459ed3d70dSJames Wright 
3469ed3d70dSJames Wright     StateConservative F_inviscid[3];
3479ed3d70dSJames Wright     FluxInviscid(gas, s, F_inviscid);
3489ed3d70dSJames Wright 
3499ed3d70dSJames Wright     CeedScalar Flux[5];
350feb491a7SJames Wright     FluxTotal_Boundary(F_inviscid, stress, Fe, normal, Flux);
3519ed3d70dSJames Wright 
3529ed3d70dSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
3539ed3d70dSJames Wright 
3549ed3d70dSJames Wright     // Save values for Jacobian
3554b96a86bSJames Wright     if (is_implicit) {
3569ed3d70dSJames Wright       StoredValuesPack(Q, i, 0, 5, qi, jac_data_sur);
3579ed3d70dSJames Wright       StoredValuesPack(Q, i, 5, 6, kmstress, jac_data_sur);
3584b96a86bSJames Wright     }
3594b96a86bSJames Wright   }
3609ed3d70dSJames Wright   return 0;
3619ed3d70dSJames Wright }
3629ed3d70dSJames Wright 
3639ed3d70dSJames Wright CEED_QFUNCTION(PressureOutflow_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3649ed3d70dSJames Wright   return PressureOutflow(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
3659ed3d70dSJames Wright }
3669ed3d70dSJames Wright 
3679ed3d70dSJames Wright CEED_QFUNCTION(PressureOutflow_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3689ed3d70dSJames Wright   return PressureOutflow(ctx, Q, in, out, STATEVAR_PRIMITIVE);
3699ed3d70dSJames Wright }
3709ed3d70dSJames Wright 
3719b103f75SJames Wright CEED_QFUNCTION(PressureOutflow_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3729b103f75SJames Wright   return PressureOutflow(ctx, Q, in, out, STATEVAR_ENTROPY);
3739b103f75SJames Wright }
3749b103f75SJames Wright 
3759ed3d70dSJames Wright // *****************************************************************************
3769ed3d70dSJames Wright // Jacobian for weak-pressure outflow boundary condition
3779ed3d70dSJames Wright // *****************************************************************************
3789ed3d70dSJames Wright CEED_QFUNCTION_HELPER int PressureOutflow_Jacobian(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out,
3799ed3d70dSJames Wright                                                    StateVariable state_var) {
3809ed3d70dSJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3819ed3d70dSJames Wright   const CeedScalar(*Grad_dq)        = in[1];
3829ed3d70dSJames Wright   const CeedScalar(*q_data_sur)     = in[2];
3839ed3d70dSJames Wright   const CeedScalar(*jac_data_sur)   = in[4];
3849ed3d70dSJames Wright   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
3859ed3d70dSJames Wright 
3869ed3d70dSJames Wright   const OutflowContext           outflow     = (OutflowContext)ctx;
3879ed3d70dSJames Wright   const NewtonianIdealGasContext gas         = &outflow->gas;
3889ed3d70dSJames Wright   const bool                     is_implicit = gas->is_implicit;
3899ed3d70dSJames Wright 
3909ed3d70dSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
391feb491a7SJames Wright     CeedScalar wdetJb, dXdx[2][3], normal[3];
392feb491a7SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, dXdx, normal);
3939ed3d70dSJames Wright     wdetJb *= is_implicit ? -1. : 1.;
3949ed3d70dSJames Wright 
3959ed3d70dSJames Wright     CeedScalar qi[5], kmstress[6], dqi[5];
3969ed3d70dSJames Wright     StoredValuesUnpack(Q, i, 0, 5, jac_data_sur, qi);
3979ed3d70dSJames Wright     StoredValuesUnpack(Q, i, 5, 6, jac_data_sur, kmstress);
3989ed3d70dSJames Wright     for (int j = 0; j < 5; j++) dqi[j] = dq[j][i];
3999ed3d70dSJames Wright 
4009ed3d70dSJames Wright     State s       = StateFromQ(gas, qi, state_var);
4019ed3d70dSJames Wright     State ds      = StateFromQ_fwd(gas, s, dqi, state_var);
4029ed3d70dSJames Wright     s.Y.pressure  = outflow->pressure;
4039ed3d70dSJames Wright     ds.Y.pressure = 0.;
4049ed3d70dSJames Wright 
4059ed3d70dSJames Wright     State grad_ds[3];
4069ed3d70dSJames Wright     StatePhysicalGradientFromReference_Boundary(Q, i, gas, s, state_var, Grad_dq, dXdx, grad_ds);
4079ed3d70dSJames Wright 
4089ed3d70dSJames Wright     CeedScalar dstrain_rate[6], dkmstress[6], stress[3][3], dstress[3][3], dFe[3];
4099ed3d70dSJames Wright     KMStrainRate_State(grad_ds, dstrain_rate);
4109ed3d70dSJames Wright     NewtonianStress(gas, dstrain_rate, dkmstress);
4119ed3d70dSJames Wright     KMUnpack(dkmstress, dstress);
4129ed3d70dSJames Wright     KMUnpack(kmstress, stress);
4139ed3d70dSJames Wright     ViscousEnergyFlux_fwd(gas, s.Y, ds.Y, grad_ds, stress, dstress, dFe);
4149ed3d70dSJames Wright 
4159ed3d70dSJames Wright     StateConservative dF_inviscid[3];
4169ed3d70dSJames Wright     FluxInviscid_fwd(gas, s, ds, dF_inviscid);
4179ed3d70dSJames Wright 
4189ed3d70dSJames Wright     CeedScalar dFlux[5];
419feb491a7SJames Wright     FluxTotal_Boundary(dF_inviscid, dstress, dFe, normal, dFlux);
4209ed3d70dSJames Wright 
4219ed3d70dSJames Wright     for (int j = 0; j < 5; j++) v[j][i] = -wdetJb * dFlux[j];
422b193fadcSJames Wright   }
4239ed3d70dSJames Wright   return 0;
4249ed3d70dSJames Wright }
4259ed3d70dSJames Wright 
4269ed3d70dSJames Wright CEED_QFUNCTION(PressureOutflow_Jacobian_Conserv)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4279ed3d70dSJames Wright   return PressureOutflow_Jacobian(ctx, Q, in, out, STATEVAR_CONSERVATIVE);
4289ed3d70dSJames Wright }
4299ed3d70dSJames Wright 
4309ed3d70dSJames Wright CEED_QFUNCTION(PressureOutflow_Jacobian_Prim)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4319ed3d70dSJames Wright   return PressureOutflow_Jacobian(ctx, Q, in, out, STATEVAR_PRIMITIVE);
4329ed3d70dSJames Wright }
4339b103f75SJames Wright 
4349b103f75SJames Wright CEED_QFUNCTION(PressureOutflow_Jacobian_Entropy)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4359b103f75SJames Wright   return PressureOutflow_Jacobian(ctx, Q, in, out, STATEVAR_ENTROPY);
4369b103f75SJames Wright }
437