xref: /honee/qfunctions/advection.h (revision 78e8b7da6af85c065c35ddcb7a107c82a97d64cc)
1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3a515125bSLeila Ghaffari 
4a515125bSLeila Ghaffari /// @file
5a515125bSLeila Ghaffari /// Advection initial condition and operator for Navier-Stokes example using PETSc
6493642f1SJames Wright #include <ceed.h>
7d0cce58aSJeremy L Thompson #include <math.h>
8a515125bSLeila Ghaffari 
9e88b842aSJames Wright #include "advection_types.h"
10ce192147SJames Wright #include "newtonian_state.h"
11ce192147SJames Wright #include "newtonian_types.h"
12e88b842aSJames Wright #include "stabilization_types.h"
131a74fa30SJames Wright #include "utils.h"
141a74fa30SJames Wright 
15a515125bSLeila Ghaffari // *****************************************************************************
169529d636SJames Wright // This QFunction sets the initial conditions and the boundary conditions
179529d636SJames Wright //   for two test cases: ROTATION and TRANSLATION
189529d636SJames Wright //
199529d636SJames Wright // -- ROTATION (default)
209529d636SJames Wright //      Initial Conditions:
219529d636SJames Wright //        Mass Density:
229529d636SJames Wright //          Constant mass density of 1.0
239529d636SJames Wright //        Momentum Density:
249529d636SJames Wright //          Rotational field in x,y
259529d636SJames Wright //        Energy Density:
269529d636SJames Wright //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
279529d636SJames Wright //            increases to (1.-r/rc), then 0. everywhere else
289529d636SJames Wright //
299529d636SJames Wright //      Boundary Conditions:
309529d636SJames Wright //        Mass Density:
319529d636SJames Wright //          0.0 flux
329529d636SJames Wright //        Momentum Density:
339529d636SJames Wright //          0.0
349529d636SJames Wright //        Energy Density:
359529d636SJames Wright //          0.0 flux
369529d636SJames Wright //
379529d636SJames Wright // -- TRANSLATION
389529d636SJames Wright //      Initial Conditions:
399529d636SJames Wright //        Mass Density:
409529d636SJames Wright //          Constant mass density of 1.0
419529d636SJames Wright //        Momentum Density:
429529d636SJames Wright //           Constant rectilinear field in x,y
439529d636SJames Wright //        Energy Density:
449529d636SJames Wright //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
459529d636SJames Wright //            increases to (1.-r/rc), then 0. everywhere else
469529d636SJames Wright //
479529d636SJames Wright //      Boundary Conditions:
489529d636SJames Wright //        Mass Density:
499529d636SJames Wright //          0.0 flux
509529d636SJames Wright //        Momentum Density:
519529d636SJames Wright //          0.0
529529d636SJames Wright //        Energy Density:
539529d636SJames Wright //          Inflow BCs:
549529d636SJames Wright //            E = E_wind
559529d636SJames Wright //          Outflow BCs:
569529d636SJames Wright //            E = E(boundary)
579529d636SJames Wright //          Both In/Outflow BCs for E are applied weakly in the
589529d636SJames Wright //            QFunction "Advection2d_Sur"
599529d636SJames Wright //
609529d636SJames Wright // *****************************************************************************
619529d636SJames Wright 
629529d636SJames Wright // *****************************************************************************
639529d636SJames Wright // This helper function provides the exact, time-dependent solution and IC formulation for 2D advection
649529d636SJames Wright // *****************************************************************************
659529d636SJames Wright CEED_QFUNCTION_HELPER CeedInt Exact_AdvectionGeneric(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
669529d636SJames Wright   const SetupContextAdv context = (SetupContextAdv)ctx;
679529d636SJames Wright   const CeedScalar      rc      = context->rc;
689529d636SJames Wright   const CeedScalar      lx      = context->lx;
699529d636SJames Wright   const CeedScalar      ly      = context->ly;
709529d636SJames Wright   const CeedScalar      lz      = dim == 2 ? 0. : context->lz;
719529d636SJames Wright   const CeedScalar     *wind    = context->wind;
729529d636SJames Wright 
739529d636SJames Wright   const CeedScalar center[3] = {0.5 * lx, 0.5 * ly, 0.5 * lz};
749529d636SJames Wright   const CeedScalar theta     = dim == 2 ? M_PI / 3 : M_PI;
759529d636SJames Wright   const CeedScalar x0[3]     = {center[0] + .25 * lx * cos(theta + time), center[1] + .25 * ly * sin(theta + time), 0.5 * lz};
769529d636SJames Wright 
779529d636SJames Wright   const CeedScalar x = X[0], y = X[1], z = dim == 2 ? 0. : X[2];
789529d636SJames Wright 
799529d636SJames Wright   CeedScalar r = 0.;
809529d636SJames Wright   switch (context->initial_condition_type) {
819529d636SJames Wright     case ADVECTIONIC_BUBBLE_SPHERE:
829529d636SJames Wright     case ADVECTIONIC_BUBBLE_CYLINDER:
839529d636SJames Wright       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]) + Square(z - x0[2]));
849529d636SJames Wright       break;
859529d636SJames Wright     case ADVECTIONIC_COSINE_HILL:
869529d636SJames Wright       r = sqrt(Square(x - center[0]) + Square(y - center[1]));
879529d636SJames Wright       break;
889529d636SJames Wright     case ADVECTIONIC_SKEW:
899529d636SJames Wright       break;
909529d636SJames Wright   }
919529d636SJames Wright 
929529d636SJames Wright   switch (context->wind_type) {
939529d636SJames Wright     case WIND_ROTATION:
949529d636SJames Wright       q[0] = 1.;
959529d636SJames Wright       q[1] = -(y - center[1]);
969529d636SJames Wright       q[2] = (x - center[0]);
979529d636SJames Wright       q[3] = 0;
989529d636SJames Wright       break;
999529d636SJames Wright     case WIND_TRANSLATION:
1009529d636SJames Wright       q[0] = 1.;
1019529d636SJames Wright       q[1] = wind[0];
1029529d636SJames Wright       q[2] = wind[1];
1039529d636SJames Wright       q[3] = dim == 2 ? 0. : wind[2];
1049529d636SJames Wright       break;
1059529d636SJames Wright     default:
1069529d636SJames Wright       return 1;
1079529d636SJames Wright   }
1089529d636SJames Wright 
1099529d636SJames Wright   switch (context->initial_condition_type) {
1109529d636SJames Wright     case ADVECTIONIC_BUBBLE_SPHERE:
1119529d636SJames Wright     case ADVECTIONIC_BUBBLE_CYLINDER:
1129529d636SJames Wright       switch (context->bubble_continuity_type) {
1139529d636SJames Wright         // original continuous, smooth shape
1149529d636SJames Wright         case BUBBLE_CONTINUITY_SMOOTH:
1159529d636SJames Wright           q[4] = r <= rc ? (1. - r / rc) : 0.;
1169529d636SJames Wright           break;
1179529d636SJames Wright         // discontinuous, sharp back half shape
1189529d636SJames Wright         case BUBBLE_CONTINUITY_BACK_SHARP:
1199529d636SJames Wright           q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) : 0.;
1209529d636SJames Wright           break;
1219529d636SJames Wright         // attempt to define a finite thickness that will get resolved under grid refinement
1229529d636SJames Wright         case BUBBLE_CONTINUITY_THICK:
1239529d636SJames Wright           q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) * fmin(1.0, (center[1] - y) / 1.25) : 0.;
1249529d636SJames Wright           break;
1259529d636SJames Wright         case BUBBLE_CONTINUITY_COSINE:
1269529d636SJames Wright           q[4] = r <= rc ? .5 + .5 * cos(r * M_PI / rc) : 0;
1279529d636SJames Wright           break;
1289529d636SJames Wright       }
1299529d636SJames Wright       break;
1309529d636SJames Wright     case ADVECTIONIC_COSINE_HILL: {
1319529d636SJames Wright       CeedScalar half_width = context->lx / 2;
1329529d636SJames Wright       q[4]                  = r > half_width ? 0. : cos(2 * M_PI * r / half_width + M_PI) + 1.;
1339529d636SJames Wright     } break;
1349529d636SJames Wright     case ADVECTIONIC_SKEW: {
1359529d636SJames Wright       CeedScalar       skewed_barrier[3]  = {wind[0], wind[1], 0};
1369529d636SJames Wright       CeedScalar       inflow_to_point[3] = {x - context->lx / 2, y, 0};
1379529d636SJames Wright       CeedScalar       cross_product[3]   = {0};
1389529d636SJames Wright       const CeedScalar boundary_threshold = 20 * CEED_EPSILON;
1399529d636SJames Wright       Cross3(skewed_barrier, inflow_to_point, cross_product);
1409529d636SJames Wright 
1419529d636SJames Wright       q[4] = cross_product[2] > boundary_threshold ? 0 : 1;
1429529d636SJames Wright       if ((x < boundary_threshold && wind[0] < boundary_threshold) ||                // outflow at -x boundary
1439529d636SJames Wright           (y < boundary_threshold && wind[1] < boundary_threshold) ||                // outflow at -y boundary
1449529d636SJames Wright           (x > context->lx - boundary_threshold && wind[0] > boundary_threshold) ||  // outflow at +x boundary
1459529d636SJames Wright           (y > context->ly - boundary_threshold && wind[1] > boundary_threshold)     // outflow at +y boundary
1469529d636SJames Wright       ) {
1479529d636SJames Wright         q[4] = 0;
1489529d636SJames Wright       }
1499529d636SJames Wright     } break;
1509529d636SJames Wright   }
1519529d636SJames Wright   return 0;
1529529d636SJames Wright }
1539529d636SJames Wright 
1549529d636SJames Wright // *****************************************************************************
155a515125bSLeila Ghaffari // This QFunction sets the initial conditions for 3D advection
156a515125bSLeila Ghaffari // *****************************************************************************
1572b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
158a515125bSLeila Ghaffari   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
159a515125bSLeila Ghaffari   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
160a515125bSLeila Ghaffari 
1613d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
162a515125bSLeila Ghaffari     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
163139613f2SLeila Ghaffari     CeedScalar       q[5] = {0.};
164a515125bSLeila Ghaffari 
1650b3a1fabSJames Wright     Exact_AdvectionGeneric(3, 0., x, 5, q, ctx);
166a515125bSLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
1670b3a1fabSJames Wright   }
168a515125bSLeila Ghaffari   return 0;
169a515125bSLeila Ghaffari }
170a515125bSLeila Ghaffari 
171a515125bSLeila Ghaffari // *****************************************************************************
1729529d636SJames Wright // This QFunction sets the initial conditions for 2D advection
173a515125bSLeila Ghaffari // *****************************************************************************
1749529d636SJames Wright CEED_QFUNCTION(ICsAdvection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1759529d636SJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1769529d636SJames Wright   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
1779529d636SJames Wright   const SetupContextAdv context    = (SetupContextAdv)ctx;
1789529d636SJames Wright 
1799529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1809529d636SJames Wright     const CeedScalar x[]  = {X[0][i], X[1][i]};
1819529d636SJames Wright     CeedScalar       q[5] = {0.};
1829529d636SJames Wright 
1839529d636SJames Wright     Exact_AdvectionGeneric(2, context->time, x, 5, q, ctx);
1849529d636SJames Wright     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
1859529d636SJames Wright   }
186a515125bSLeila Ghaffari   return 0;
187a515125bSLeila Ghaffari }
188a515125bSLeila Ghaffari 
1899529d636SJames Wright CEED_QFUNCTION_HELPER void QdataUnpack_ND(CeedInt N, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar *dXdx) {
19085efd435SJames Wright   // Cannot directly use QdataUnpack* helper functions due to SYCL online compiler incompatabilities
1919529d636SJames Wright   switch (N) {
1929529d636SJames Wright     case 2:
19385efd435SJames Wright       StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
19485efd435SJames Wright       StoredValuesUnpack(Q, i, 1, 4, q_data, dXdx);
1959529d636SJames Wright       break;
1969529d636SJames Wright     case 3:
19785efd435SJames Wright       StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
19885efd435SJames Wright       StoredValuesUnpack(Q, i, 1, 9, q_data, dXdx);
1999529d636SJames Wright       break;
2009529d636SJames Wright   }
2019529d636SJames Wright }
2029529d636SJames Wright 
2039529d636SJames Wright CEED_QFUNCTION_HELPER int QdataBoundaryUnpack_ND(CeedInt N, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar *dXdx,
2049529d636SJames Wright                                                  CeedScalar *normal) {
20585efd435SJames Wright   // Cannot directly use QdataBoundaryUnpack* helper functions due to SYCL online compiler incompatabilities
2069529d636SJames Wright   switch (N) {
2079529d636SJames Wright     case 2:
20885efd435SJames Wright       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
20985efd435SJames Wright       if (normal) StoredValuesUnpack(Q, i, 1, 2, q_data, normal);
2109529d636SJames Wright       break;
2119529d636SJames Wright     case 3:
21285efd435SJames Wright       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
21385efd435SJames Wright       if (normal) StoredValuesUnpack(Q, i, 1, 3, q_data, normal);
21485efd435SJames Wright       if (dXdx) StoredValuesUnpack(Q, i, 4, 6, q_data, (CeedScalar *)dXdx);
2159529d636SJames Wright       break;
2169529d636SJames Wright   }
2179529d636SJames Wright   return CEED_ERROR_SUCCESS;
2189529d636SJames Wright }
2199529d636SJames Wright 
2209529d636SJames Wright CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_ND(CeedInt N, CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s,
2219529d636SJames Wright                                                                  StateVariable state_var, const CeedScalar *grad_q, const CeedScalar *dXdx,
2229529d636SJames Wright                                                                  State *grad_s) {
2239529d636SJames Wright   switch (N) {
2249529d636SJames Wright     case 2: {
2259529d636SJames Wright       for (CeedInt k = 0; k < 2; k++) {
2269529d636SJames Wright         CeedScalar dqi[5];
2279529d636SJames Wright         for (CeedInt j = 0; j < 5; j++) {
2289529d636SJames Wright           dqi[j] = grad_q[(Q * 5) * 0 + Q * j + i] * dXdx[0 * N + k] + grad_q[(Q * 5) * 1 + Q * j + i] * dXdx[1 * N + k];
2299529d636SJames Wright         }
2309529d636SJames Wright         grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
2319529d636SJames Wright       }
2329529d636SJames Wright       CeedScalar U[5] = {0.};
2339529d636SJames Wright       grad_s[2]       = StateFromU(gas, U);
2349529d636SJames Wright     } break;
2359529d636SJames Wright     case 3:
23685efd435SJames Wright       // Cannot directly use StatePhysicalGradientFromReference helper functions due to SYCL online compiler incompatabilities
23785efd435SJames Wright       for (CeedInt k = 0; k < 3; k++) {
23885efd435SJames Wright         CeedScalar dqi[5];
23985efd435SJames Wright         for (CeedInt j = 0; j < 5; j++) {
24085efd435SJames Wright           dqi[j] = grad_q[(Q * 5) * 0 + Q * j + i] * dXdx[0 * N + k] + grad_q[(Q * 5) * 1 + Q * j + i] * dXdx[1 * N + k] +
24185efd435SJames Wright                    grad_q[(Q * 5) * 2 + Q * j + i] * dXdx[2 * N + k];
24285efd435SJames Wright         }
24385efd435SJames Wright         grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
24485efd435SJames Wright       }
2459529d636SJames Wright       break;
2469529d636SJames Wright   }
2479529d636SJames Wright }
2489529d636SJames Wright 
249a78efa86SJames Wright // @brief Calculate the stabilization constant \tau
250a78efa86SJames Wright CEED_QFUNCTION_HELPER CeedScalar Tau(AdvectionContext context, const State s, const CeedScalar *dXdx, CeedInt dim) {
251a78efa86SJames Wright   switch (context->stabilization_tau) {
252a78efa86SJames Wright     case STAB_TAU_CTAU: {
253a78efa86SJames Wright       CeedScalar uX[3] = {0.};
254a78efa86SJames Wright 
255a78efa86SJames Wright       MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
256a78efa86SJames Wright       return context->CtauS / sqrt(DotN(uX, uX, dim));
257a78efa86SJames Wright     } break;
258a78efa86SJames Wright     case STAB_TAU_ADVDIFF_SHAKIB: {
259a78efa86SJames Wright       CeedScalar gijd_mat[9] = {0.}, gij_uj[3] = {0.};
260a78efa86SJames Wright 
261a78efa86SJames Wright       MatMatN(dXdx, dXdx, dim, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat);
262a78efa86SJames Wright       MatVecNM(gijd_mat, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, gij_uj);
263a78efa86SJames Wright       return 1 / sqrt(Square(2 * context->Ctau_t / context->dt) + DotN(s.Y.velocity, gij_uj, dim) * context->Ctau_a);
264a78efa86SJames Wright     } break;
265a78efa86SJames Wright     default:
266a78efa86SJames Wright       return 0.;
267a78efa86SJames Wright   }
268a78efa86SJames Wright }
269a78efa86SJames Wright 
2709529d636SJames Wright // *****************************************************************************
2719529d636SJames Wright // This QFunction implements Advection for implicit time stepping method
2729529d636SJames Wright // *****************************************************************************
2739529d636SJames Wright CEED_QFUNCTION_HELPER void IFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
2749529d636SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2759529d636SJames Wright   const CeedScalar(*grad_q)            = in[1];
2769529d636SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
2779529d636SJames Wright   const CeedScalar(*q_data)            = in[3];
2789529d636SJames Wright 
2799529d636SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
2809529d636SJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
2819529d636SJames Wright   CeedScalar *jac_data               = out[2];
2829529d636SJames Wright 
2839529d636SJames Wright   AdvectionContext                 context   = (AdvectionContext)ctx;
2849529d636SJames Wright   const CeedScalar                 zeros[14] = {0.};
2859529d636SJames Wright   NewtonianIdealGasContext         gas;
2869529d636SJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
2879529d636SJames Wright   gas                                         = &gas_struct;
2889529d636SJames Wright 
2899529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2909529d636SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
2919529d636SJames Wright     const State      s     = StateFromU(gas, qi);
2929529d636SJames Wright 
2939529d636SJames Wright     CeedScalar wdetJ, dXdx[9];
2949529d636SJames Wright     QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx);
2959529d636SJames Wright     State grad_s[3];
2969529d636SJames Wright     StatePhysicalGradientFromReference_ND(dim, Q, i, gas, s, STATEVAR_CONSERVATIVE, grad_q, dXdx, grad_s);
2979529d636SJames Wright 
2989529d636SJames Wright     const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total};
2999529d636SJames Wright 
3009529d636SJames Wright     for (CeedInt f = 0; f < 4; f++) {
3019529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0;  // No Change in density or momentum
3029529d636SJames Wright       v[f][i] = wdetJ * q_dot[f][i];                          // K Mass/transient term
3039529d636SJames Wright     }
3049529d636SJames Wright 
3059529d636SJames Wright     CeedScalar div_u = 0;
3069529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) {
3079529d636SJames Wright       for (CeedInt k = 0; k < dim; k++) {
3089529d636SJames Wright         div_u += grad_s[k].Y.velocity[j];
3099529d636SJames Wright       }
3109529d636SJames Wright     }
3119529d636SJames Wright     CeedScalar strong_conv = s.U.E_total * div_u + DotN(s.Y.velocity, Grad_E, dim);
3129529d636SJames Wright     CeedScalar strong_res  = q_dot[4][i] + strong_conv;
3139529d636SJames Wright 
3149529d636SJames Wright     v[4][i] = wdetJ * q_dot[4][i];  // transient part (ALWAYS)
3159529d636SJames Wright 
3169529d636SJames Wright     CeedScalar uX[3] = {0.};
3179529d636SJames Wright     MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
3189529d636SJames Wright 
3199529d636SJames Wright     if (context->strong_form) {  // Strong Galerkin convection term: v div(E u)
3209529d636SJames Wright       v[4][i] += wdetJ * strong_conv;
3219529d636SJames Wright     } else {  // Weak Galerkin convection term: -dv \cdot (E u)
3229529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = -wdetJ * s.U.E_total * uX[j];
3239529d636SJames Wright     }
3249529d636SJames Wright 
325c8d249deSJames Wright     {  // Diffusion
326c8d249deSJames Wright       CeedScalar Fe[3], Fe_dXdx[3] = {0.};
327c8d249deSJames Wright 
328c8d249deSJames Wright       for (CeedInt i = 0; i < dim; i++) Fe[i] = -context->diffusion_coeff * grad_s[i].U.E_total;
329c8d249deSJames Wright       MatVecNM(dXdx, Fe, dim, dim, CEED_NOTRANSPOSE, Fe_dXdx);
330c8d249deSJames Wright       for (CeedInt k = 0; k < dim; k++) grad_v[k][4][i] -= wdetJ * Fe_dXdx[k];
331c8d249deSJames Wright     }
332c8d249deSJames Wright 
333a78efa86SJames Wright     const CeedScalar TauS = Tau(context, s, dXdx, dim);
3349529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) {
3359529d636SJames Wright         case STAB_NONE:
3369529d636SJames Wright           break;
3379529d636SJames Wright         case STAB_SU:
3389529d636SJames Wright           grad_v[j][4][i] += wdetJ * TauS * strong_conv * uX[j];
3399529d636SJames Wright           break;
3409529d636SJames Wright         case STAB_SUPG:
3419529d636SJames Wright           grad_v[j][4][i] += wdetJ * TauS * strong_res * uX[j];
3429529d636SJames Wright           break;
3439529d636SJames Wright       }
3449529d636SJames Wright     StoredValuesPack(Q, i, 0, 14, zeros, jac_data);
3459529d636SJames Wright   }
3469529d636SJames Wright }
3479529d636SJames Wright 
3482b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
349bd4b5413SJames Wright   IFunction_AdvectionGeneric(ctx, Q, in, out, 3);
350a515125bSLeila Ghaffari   return 0;
351a515125bSLeila Ghaffari }
352a515125bSLeila Ghaffari 
3539529d636SJames Wright CEED_QFUNCTION(IFunction_Advection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3549529d636SJames Wright   IFunction_AdvectionGeneric(ctx, Q, in, out, 2);
3559529d636SJames Wright   return 0;
3569529d636SJames Wright }
3579529d636SJames Wright 
358a78efa86SJames Wright CEED_QFUNCTION_HELPER void MassFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
359a78efa86SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
360a78efa86SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[1];
361a78efa86SJames Wright   const CeedScalar(*q_data)            = in[2];
362a78efa86SJames Wright 
363a78efa86SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
364a78efa86SJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
365a78efa86SJames Wright 
366a78efa86SJames Wright   AdvectionContext                 context    = (AdvectionContext)ctx;
367a78efa86SJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
368a78efa86SJames Wright   NewtonianIdealGasContext         gas        = &gas_struct;
369a78efa86SJames Wright 
370a78efa86SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
371a78efa86SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
372a78efa86SJames Wright     const State      s     = StateFromU(gas, qi);
373a78efa86SJames Wright     CeedScalar       wdetJ, dXdx[9];
374a78efa86SJames Wright     QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx);
375a78efa86SJames Wright 
376a78efa86SJames Wright     for (CeedInt f = 0; f < 4; f++) {
377a78efa86SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0;  // No Change in density or momentum
378a78efa86SJames Wright       v[f][i] = wdetJ * q_dot[f][i];                          // K Mass/transient term
379a78efa86SJames Wright     }
380a78efa86SJames Wright 
381a78efa86SJames Wright     // Unstabilized mass term
382a78efa86SJames Wright     v[4][i] = wdetJ * q_dot[4][i];
383a78efa86SJames Wright 
384a78efa86SJames Wright     // Stabilized mass term
385a78efa86SJames Wright     CeedScalar uX[3] = {0.};
386a78efa86SJames Wright     MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
387a78efa86SJames Wright     const CeedScalar TauS = Tau(context, s, dXdx, dim);
388a78efa86SJames Wright     for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) {
389a78efa86SJames Wright         case STAB_NONE:
390a78efa86SJames Wright         case STAB_SU:
391a78efa86SJames Wright           grad_v[j][4][i] = 0;
392a78efa86SJames Wright           break;  // These should be run with the unstabilized mass matrix anyways
393a78efa86SJames Wright         case STAB_SUPG:
394a78efa86SJames Wright           grad_v[j][4][i] = wdetJ * TauS * q_dot[4][i] * uX[j];
395a78efa86SJames Wright           break;
396a78efa86SJames Wright       }
397a78efa86SJames Wright   }
398a78efa86SJames Wright }
399a78efa86SJames Wright 
400a78efa86SJames Wright CEED_QFUNCTION(MassFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
401a78efa86SJames Wright   MassFunction_AdvectionGeneric(ctx, Q, in, out, 3);
402a78efa86SJames Wright   return 0;
403a78efa86SJames Wright }
404a78efa86SJames Wright 
405a78efa86SJames Wright CEED_QFUNCTION(MassFunction_Advection2D)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
406a78efa86SJames Wright   MassFunction_AdvectionGeneric(ctx, Q, in, out, 2);
407a78efa86SJames Wright   return 0;
408a78efa86SJames Wright }
409a78efa86SJames Wright 
4109529d636SJames Wright // *****************************************************************************
4119529d636SJames Wright // This QFunction implements Advection for explicit time stepping method
4129529d636SJames Wright // *****************************************************************************
4139529d636SJames Wright CEED_QFUNCTION_HELPER void RHSFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
4149529d636SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
4159529d636SJames Wright   const CeedScalar(*grad_q)        = in[1];
4169529d636SJames Wright   const CeedScalar(*q_data)        = in[2];
4179529d636SJames Wright 
4189529d636SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
4199529d636SJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
4209529d636SJames Wright 
4219529d636SJames Wright   AdvectionContext                 context    = (AdvectionContext)ctx;
4229529d636SJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
423a78efa86SJames Wright   NewtonianIdealGasContext         gas        = &gas_struct;
4249529d636SJames Wright 
4259529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
4269529d636SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
4279529d636SJames Wright     const State      s     = StateFromU(gas, qi);
4289529d636SJames Wright 
4299529d636SJames Wright     CeedScalar wdetJ, dXdx[9];
4309529d636SJames Wright     QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx);
4319529d636SJames Wright     State grad_s[3];
4329529d636SJames Wright     StatePhysicalGradientFromReference_ND(dim, Q, i, gas, s, STATEVAR_CONSERVATIVE, grad_q, dXdx, grad_s);
4339529d636SJames Wright 
4349529d636SJames Wright     const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total};
4359529d636SJames Wright 
4369529d636SJames Wright     for (CeedInt f = 0; f < 4; f++) {
4379529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0;  // No Change in density or momentum
4389529d636SJames Wright       v[f][i] = 0.;
4399529d636SJames Wright     }
4409529d636SJames Wright 
4419529d636SJames Wright     CeedScalar div_u = 0;
4429529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) {
4439529d636SJames Wright       for (CeedInt k = 0; k < dim; k++) {
4449529d636SJames Wright         div_u += grad_s[k].Y.velocity[j];
4459529d636SJames Wright       }
4469529d636SJames Wright     }
4479529d636SJames Wright     CeedScalar strong_conv = s.U.E_total * div_u + DotN(s.Y.velocity, Grad_E, dim);
4489529d636SJames Wright 
4499529d636SJames Wright     CeedScalar uX[3] = {0.};
4509529d636SJames Wright     MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
4519529d636SJames Wright 
4529529d636SJames Wright     if (context->strong_form) {  // Strong Galerkin convection term: v div(E u)
4539529d636SJames Wright       v[4][i] = -wdetJ * strong_conv;
4549529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = 0;
4559529d636SJames Wright     } else {  // Weak Galerkin convection term: -dv \cdot (E u)
4569529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = wdetJ * s.U.E_total * uX[j];
4579529d636SJames Wright       v[4][i] = 0.;
4589529d636SJames Wright     }
4599529d636SJames Wright 
460c8d249deSJames Wright     {  // Diffusion
461c8d249deSJames Wright       CeedScalar Fe[3], Fe_dXdx[3] = {0.};
462c8d249deSJames Wright 
463c8d249deSJames Wright       for (CeedInt i = 0; i < dim; i++) Fe[i] = -context->diffusion_coeff * grad_s[i].U.E_total;
464c8d249deSJames Wright       MatVecNM(dXdx, Fe, dim, dim, CEED_NOTRANSPOSE, Fe_dXdx);
465c8d249deSJames Wright       for (CeedInt k = 0; k < dim; k++) grad_v[k][4][i] += wdetJ * Fe_dXdx[k];
466c8d249deSJames Wright     }
467c8d249deSJames Wright 
468a78efa86SJames Wright     const CeedScalar TauS = Tau(context, s, dXdx, dim);
4699529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) {
4709529d636SJames Wright         case STAB_NONE:
4719529d636SJames Wright           break;
4729529d636SJames Wright         case STAB_SU:
4739529d636SJames Wright         case STAB_SUPG:
4749d860eefSJames Wright           grad_v[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
4759529d636SJames Wright           break;
4769529d636SJames Wright       }
4779529d636SJames Wright   }
4789529d636SJames Wright }
4799529d636SJames Wright 
4809529d636SJames Wright CEED_QFUNCTION(RHS_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4819529d636SJames Wright   RHSFunction_AdvectionGeneric(ctx, Q, in, out, 3);
4829529d636SJames Wright   return 0;
4839529d636SJames Wright }
4849529d636SJames Wright 
4859529d636SJames Wright CEED_QFUNCTION(RHS_Advection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4869529d636SJames Wright   RHSFunction_AdvectionGeneric(ctx, Q, in, out, 2);
4879529d636SJames Wright   return 0;
4889529d636SJames Wright }
4899529d636SJames Wright 
4909529d636SJames Wright // *****************************************************************************
4919529d636SJames Wright // This QFunction implements consistent outflow and inflow BCs
4929529d636SJames Wright //      for advection
4939529d636SJames Wright //
4949529d636SJames Wright //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
4959529d636SJames Wright //    sign(dot(wind, normal)) > 0 : outflow BCs
4969529d636SJames Wright //    sign(dot(wind, normal)) < 0 : inflow BCs
4979529d636SJames Wright //
4989529d636SJames Wright //  Outflow BCs:
4999529d636SJames Wright //    The validity of the weak form of the governing equations is extended to the outflow and the current values of E are applied.
5009529d636SJames Wright //
5019529d636SJames Wright //  Inflow BCs:
5029529d636SJames Wright //    A prescribed Total Energy (E_wind) is applied weakly.
5039529d636SJames Wright // *****************************************************************************
5049529d636SJames Wright CEED_QFUNCTION(Advection_InOutFlowGeneric)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
5059529d636SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
5069529d636SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
5079529d636SJames Wright 
5089529d636SJames Wright   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
5099529d636SJames Wright   AdvectionContext context     = (AdvectionContext)ctx;
5109529d636SJames Wright   const CeedScalar E_wind      = context->E_wind;
5119529d636SJames Wright   const CeedScalar strong_form = context->strong_form;
5129529d636SJames Wright   const bool       is_implicit = context->implicit;
5139529d636SJames Wright 
5149529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
5159529d636SJames Wright     const CeedScalar rho  = q[0][i];
5169529d636SJames Wright     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
5179529d636SJames Wright     const CeedScalar E    = q[4][i];
5189529d636SJames Wright 
519*78e8b7daSJames Wright     CeedScalar wdetJb, normal[3];
520*78e8b7daSJames Wright     QdataBoundaryUnpack_ND(dim, Q, i, q_data_sur, &wdetJb, NULL, normal);
5219529d636SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
5229529d636SJames Wright 
523*78e8b7daSJames Wright     const CeedScalar u_normal = DotN(normal, u, dim);
5249529d636SJames Wright 
5259529d636SJames Wright     // No Change in density or momentum
5269529d636SJames Wright     for (CeedInt j = 0; j < 4; j++) {
5279529d636SJames Wright       v[j][i] = 0;
5289529d636SJames Wright     }
5299529d636SJames Wright     // Implementing in/outflow BCs
5309529d636SJames Wright     if (u_normal > 0) {  // outflow
5319529d636SJames Wright       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
5329529d636SJames Wright     } else {  // inflow
5339529d636SJames Wright       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
5349529d636SJames Wright     }
5359529d636SJames Wright   }
5369529d636SJames Wright   return 0;
5379529d636SJames Wright }
5389529d636SJames Wright 
5392b916ea7SJeremy L Thompson CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5408dba1efaSJames Wright   Advection_InOutFlowGeneric(ctx, Q, in, out, 3);
541a515125bSLeila Ghaffari   return 0;
542a515125bSLeila Ghaffari }
543a515125bSLeila Ghaffari 
5449529d636SJames Wright CEED_QFUNCTION(Advection2d_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5459529d636SJames Wright   Advection_InOutFlowGeneric(ctx, Q, in, out, 2);
5469529d636SJames Wright   return 0;
5479529d636SJames Wright }
548