xref: /honee/qfunctions/advection.h (revision a78efa869d5e60cbf1c38b3b4d784572ed49ea21)
1dc936754SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
2727da7e7SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3a515125bSLeila Ghaffari //
4727da7e7SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5a515125bSLeila Ghaffari //
6727da7e7SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7a515125bSLeila Ghaffari 
8a515125bSLeila Ghaffari /// @file
9a515125bSLeila Ghaffari /// Advection initial condition and operator for Navier-Stokes example using PETSc
10493642f1SJames Wright #include <ceed.h>
11d0cce58aSJeremy L Thompson #include <math.h>
12a515125bSLeila Ghaffari 
13e88b842aSJames Wright #include "advection_types.h"
14ce192147SJames Wright #include "newtonian_state.h"
15ce192147SJames Wright #include "newtonian_types.h"
16e88b842aSJames Wright #include "stabilization_types.h"
171a74fa30SJames Wright #include "utils.h"
181a74fa30SJames Wright 
19a515125bSLeila Ghaffari // *****************************************************************************
209529d636SJames Wright // This QFunction sets the initial conditions and the boundary conditions
219529d636SJames Wright //   for two test cases: ROTATION and TRANSLATION
229529d636SJames Wright //
239529d636SJames Wright // -- ROTATION (default)
249529d636SJames Wright //      Initial Conditions:
259529d636SJames Wright //        Mass Density:
269529d636SJames Wright //          Constant mass density of 1.0
279529d636SJames Wright //        Momentum Density:
289529d636SJames Wright //          Rotational field in x,y
299529d636SJames Wright //        Energy Density:
309529d636SJames Wright //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
319529d636SJames Wright //            increases to (1.-r/rc), then 0. everywhere else
329529d636SJames Wright //
339529d636SJames Wright //      Boundary Conditions:
349529d636SJames Wright //        Mass Density:
359529d636SJames Wright //          0.0 flux
369529d636SJames Wright //        Momentum Density:
379529d636SJames Wright //          0.0
389529d636SJames Wright //        Energy Density:
399529d636SJames Wright //          0.0 flux
409529d636SJames Wright //
419529d636SJames Wright // -- TRANSLATION
429529d636SJames Wright //      Initial Conditions:
439529d636SJames Wright //        Mass Density:
449529d636SJames Wright //          Constant mass density of 1.0
459529d636SJames Wright //        Momentum Density:
469529d636SJames Wright //           Constant rectilinear field in x,y
479529d636SJames Wright //        Energy Density:
489529d636SJames Wright //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
499529d636SJames Wright //            increases to (1.-r/rc), then 0. everywhere else
509529d636SJames Wright //
519529d636SJames Wright //      Boundary Conditions:
529529d636SJames Wright //        Mass Density:
539529d636SJames Wright //          0.0 flux
549529d636SJames Wright //        Momentum Density:
559529d636SJames Wright //          0.0
569529d636SJames Wright //        Energy Density:
579529d636SJames Wright //          Inflow BCs:
589529d636SJames Wright //            E = E_wind
599529d636SJames Wright //          Outflow BCs:
609529d636SJames Wright //            E = E(boundary)
619529d636SJames Wright //          Both In/Outflow BCs for E are applied weakly in the
629529d636SJames Wright //            QFunction "Advection2d_Sur"
639529d636SJames Wright //
649529d636SJames Wright // *****************************************************************************
659529d636SJames Wright 
669529d636SJames Wright // *****************************************************************************
679529d636SJames Wright // This helper function provides the exact, time-dependent solution and IC formulation for 2D advection
689529d636SJames Wright // *****************************************************************************
699529d636SJames Wright CEED_QFUNCTION_HELPER CeedInt Exact_AdvectionGeneric(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
709529d636SJames Wright   const SetupContextAdv context = (SetupContextAdv)ctx;
719529d636SJames Wright   const CeedScalar      rc      = context->rc;
729529d636SJames Wright   const CeedScalar      lx      = context->lx;
739529d636SJames Wright   const CeedScalar      ly      = context->ly;
749529d636SJames Wright   const CeedScalar      lz      = dim == 2 ? 0. : context->lz;
759529d636SJames Wright   const CeedScalar     *wind    = context->wind;
769529d636SJames Wright 
779529d636SJames Wright   const CeedScalar center[3] = {0.5 * lx, 0.5 * ly, 0.5 * lz};
789529d636SJames Wright   const CeedScalar theta     = dim == 2 ? M_PI / 3 : M_PI;
799529d636SJames Wright   const CeedScalar x0[3]     = {center[0] + .25 * lx * cos(theta + time), center[1] + .25 * ly * sin(theta + time), 0.5 * lz};
809529d636SJames Wright 
819529d636SJames Wright   const CeedScalar x = X[0], y = X[1], z = dim == 2 ? 0. : X[2];
829529d636SJames Wright 
839529d636SJames Wright   CeedScalar r = 0.;
849529d636SJames Wright   switch (context->initial_condition_type) {
859529d636SJames Wright     case ADVECTIONIC_BUBBLE_SPHERE:
869529d636SJames Wright     case ADVECTIONIC_BUBBLE_CYLINDER:
879529d636SJames Wright       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]) + Square(z - x0[2]));
889529d636SJames Wright       break;
899529d636SJames Wright     case ADVECTIONIC_COSINE_HILL:
909529d636SJames Wright       r = sqrt(Square(x - center[0]) + Square(y - center[1]));
919529d636SJames Wright       break;
929529d636SJames Wright     case ADVECTIONIC_SKEW:
939529d636SJames Wright       break;
949529d636SJames Wright   }
959529d636SJames Wright 
969529d636SJames Wright   switch (context->wind_type) {
979529d636SJames Wright     case WIND_ROTATION:
989529d636SJames Wright       q[0] = 1.;
999529d636SJames Wright       q[1] = -(y - center[1]);
1009529d636SJames Wright       q[2] = (x - center[0]);
1019529d636SJames Wright       q[3] = 0;
1029529d636SJames Wright       break;
1039529d636SJames Wright     case WIND_TRANSLATION:
1049529d636SJames Wright       q[0] = 1.;
1059529d636SJames Wright       q[1] = wind[0];
1069529d636SJames Wright       q[2] = wind[1];
1079529d636SJames Wright       q[3] = dim == 2 ? 0. : wind[2];
1089529d636SJames Wright       break;
1099529d636SJames Wright     default:
1109529d636SJames Wright       return 1;
1119529d636SJames Wright   }
1129529d636SJames Wright 
1139529d636SJames Wright   switch (context->initial_condition_type) {
1149529d636SJames Wright     case ADVECTIONIC_BUBBLE_SPHERE:
1159529d636SJames Wright     case ADVECTIONIC_BUBBLE_CYLINDER:
1169529d636SJames Wright       switch (context->bubble_continuity_type) {
1179529d636SJames Wright         // original continuous, smooth shape
1189529d636SJames Wright         case BUBBLE_CONTINUITY_SMOOTH:
1199529d636SJames Wright           q[4] = r <= rc ? (1. - r / rc) : 0.;
1209529d636SJames Wright           break;
1219529d636SJames Wright         // discontinuous, sharp back half shape
1229529d636SJames Wright         case BUBBLE_CONTINUITY_BACK_SHARP:
1239529d636SJames Wright           q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) : 0.;
1249529d636SJames Wright           break;
1259529d636SJames Wright         // attempt to define a finite thickness that will get resolved under grid refinement
1269529d636SJames Wright         case BUBBLE_CONTINUITY_THICK:
1279529d636SJames Wright           q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) * fmin(1.0, (center[1] - y) / 1.25) : 0.;
1289529d636SJames Wright           break;
1299529d636SJames Wright         case BUBBLE_CONTINUITY_COSINE:
1309529d636SJames Wright           q[4] = r <= rc ? .5 + .5 * cos(r * M_PI / rc) : 0;
1319529d636SJames Wright           break;
1329529d636SJames Wright       }
1339529d636SJames Wright       break;
1349529d636SJames Wright     case ADVECTIONIC_COSINE_HILL: {
1359529d636SJames Wright       CeedScalar half_width = context->lx / 2;
1369529d636SJames Wright       q[4]                  = r > half_width ? 0. : cos(2 * M_PI * r / half_width + M_PI) + 1.;
1379529d636SJames Wright     } break;
1389529d636SJames Wright     case ADVECTIONIC_SKEW: {
1399529d636SJames Wright       CeedScalar       skewed_barrier[3]  = {wind[0], wind[1], 0};
1409529d636SJames Wright       CeedScalar       inflow_to_point[3] = {x - context->lx / 2, y, 0};
1419529d636SJames Wright       CeedScalar       cross_product[3]   = {0};
1429529d636SJames Wright       const CeedScalar boundary_threshold = 20 * CEED_EPSILON;
1439529d636SJames Wright       Cross3(skewed_barrier, inflow_to_point, cross_product);
1449529d636SJames Wright 
1459529d636SJames Wright       q[4] = cross_product[2] > boundary_threshold ? 0 : 1;
1469529d636SJames Wright       if ((x < boundary_threshold && wind[0] < boundary_threshold) ||                // outflow at -x boundary
1479529d636SJames Wright           (y < boundary_threshold && wind[1] < boundary_threshold) ||                // outflow at -y boundary
1489529d636SJames Wright           (x > context->lx - boundary_threshold && wind[0] > boundary_threshold) ||  // outflow at +x boundary
1499529d636SJames Wright           (y > context->ly - boundary_threshold && wind[1] > boundary_threshold)     // outflow at +y boundary
1509529d636SJames Wright       ) {
1519529d636SJames Wright         q[4] = 0;
1529529d636SJames Wright       }
1539529d636SJames Wright     } break;
1549529d636SJames Wright   }
1559529d636SJames Wright   return 0;
1569529d636SJames Wright }
1579529d636SJames Wright 
1589529d636SJames Wright // *****************************************************************************
159a515125bSLeila Ghaffari // This QFunction sets the initial conditions for 3D advection
160a515125bSLeila Ghaffari // *****************************************************************************
1612b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
162a515125bSLeila Ghaffari   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
163a515125bSLeila Ghaffari   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
164a515125bSLeila Ghaffari 
1653d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
166a515125bSLeila Ghaffari     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
167139613f2SLeila Ghaffari     CeedScalar       q[5] = {0.};
168a515125bSLeila Ghaffari 
1690b3a1fabSJames Wright     Exact_AdvectionGeneric(3, 0., x, 5, q, ctx);
170a515125bSLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
1710b3a1fabSJames Wright   }
172a515125bSLeila Ghaffari   return 0;
173a515125bSLeila Ghaffari }
174a515125bSLeila Ghaffari 
175a515125bSLeila Ghaffari // *****************************************************************************
1769529d636SJames Wright // This QFunction sets the initial conditions for 2D advection
177a515125bSLeila Ghaffari // *****************************************************************************
1789529d636SJames Wright CEED_QFUNCTION(ICsAdvection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1799529d636SJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1809529d636SJames Wright   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
1819529d636SJames Wright   const SetupContextAdv context    = (SetupContextAdv)ctx;
1829529d636SJames Wright 
1839529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1849529d636SJames Wright     const CeedScalar x[]  = {X[0][i], X[1][i]};
1859529d636SJames Wright     CeedScalar       q[5] = {0.};
1869529d636SJames Wright 
1879529d636SJames Wright     Exact_AdvectionGeneric(2, context->time, x, 5, q, ctx);
1889529d636SJames Wright     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
1899529d636SJames Wright   }
190a515125bSLeila Ghaffari   return 0;
191a515125bSLeila Ghaffari }
192a515125bSLeila Ghaffari 
1939529d636SJames Wright CEED_QFUNCTION_HELPER void QdataUnpack_ND(CeedInt N, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar *dXdx) {
19485efd435SJames Wright   // Cannot directly use QdataUnpack* helper functions due to SYCL online compiler incompatabilities
1959529d636SJames Wright   switch (N) {
1969529d636SJames Wright     case 2:
19785efd435SJames Wright       StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
19885efd435SJames Wright       StoredValuesUnpack(Q, i, 1, 4, q_data, dXdx);
1999529d636SJames Wright       break;
2009529d636SJames Wright     case 3:
20185efd435SJames Wright       StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
20285efd435SJames Wright       StoredValuesUnpack(Q, i, 1, 9, q_data, dXdx);
2039529d636SJames Wright       break;
2049529d636SJames Wright   }
2059529d636SJames Wright }
2069529d636SJames Wright 
2079529d636SJames Wright CEED_QFUNCTION_HELPER int QdataBoundaryUnpack_ND(CeedInt N, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar *dXdx,
2089529d636SJames Wright                                                  CeedScalar *normal) {
20985efd435SJames Wright   // Cannot directly use QdataBoundaryUnpack* helper functions due to SYCL online compiler incompatabilities
2109529d636SJames Wright   switch (N) {
2119529d636SJames Wright     case 2:
21285efd435SJames Wright       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
21385efd435SJames Wright       if (normal) StoredValuesUnpack(Q, i, 1, 2, q_data, normal);
2149529d636SJames Wright       break;
2159529d636SJames Wright     case 3:
21685efd435SJames Wright       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
21785efd435SJames Wright       if (normal) StoredValuesUnpack(Q, i, 1, 3, q_data, normal);
21885efd435SJames Wright       if (dXdx) StoredValuesUnpack(Q, i, 4, 6, q_data, (CeedScalar *)dXdx);
2199529d636SJames Wright       break;
2209529d636SJames Wright   }
2219529d636SJames Wright   return CEED_ERROR_SUCCESS;
2229529d636SJames Wright }
2239529d636SJames Wright 
2249529d636SJames Wright CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_ND(CeedInt N, CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s,
2259529d636SJames Wright                                                                  StateVariable state_var, const CeedScalar *grad_q, const CeedScalar *dXdx,
2269529d636SJames Wright                                                                  State *grad_s) {
2279529d636SJames Wright   switch (N) {
2289529d636SJames Wright     case 2: {
2299529d636SJames Wright       for (CeedInt k = 0; k < 2; k++) {
2309529d636SJames Wright         CeedScalar dqi[5];
2319529d636SJames Wright         for (CeedInt j = 0; j < 5; j++) {
2329529d636SJames 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];
2339529d636SJames Wright         }
2349529d636SJames Wright         grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
2359529d636SJames Wright       }
2369529d636SJames Wright       CeedScalar U[5] = {0.};
2379529d636SJames Wright       grad_s[2]       = StateFromU(gas, U);
2389529d636SJames Wright     } break;
2399529d636SJames Wright     case 3:
24085efd435SJames Wright       // Cannot directly use StatePhysicalGradientFromReference helper functions due to SYCL online compiler incompatabilities
24185efd435SJames Wright       for (CeedInt k = 0; k < 3; k++) {
24285efd435SJames Wright         CeedScalar dqi[5];
24385efd435SJames Wright         for (CeedInt j = 0; j < 5; j++) {
24485efd435SJames 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] +
24585efd435SJames Wright                    grad_q[(Q * 5) * 2 + Q * j + i] * dXdx[2 * N + k];
24685efd435SJames Wright         }
24785efd435SJames Wright         grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
24885efd435SJames Wright       }
2499529d636SJames Wright       break;
2509529d636SJames Wright   }
2519529d636SJames Wright }
2529529d636SJames Wright 
253*a78efa86SJames Wright // @brief Calculate the stabilization constant \tau
254*a78efa86SJames Wright CEED_QFUNCTION_HELPER CeedScalar Tau(AdvectionContext context, const State s, const CeedScalar *dXdx, CeedInt dim) {
255*a78efa86SJames Wright   switch (context->stabilization_tau) {
256*a78efa86SJames Wright     case STAB_TAU_CTAU: {
257*a78efa86SJames Wright       CeedScalar uX[3] = {0.};
258*a78efa86SJames Wright 
259*a78efa86SJames Wright       MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
260*a78efa86SJames Wright       return context->CtauS / sqrt(DotN(uX, uX, dim));
261*a78efa86SJames Wright     } break;
262*a78efa86SJames Wright     case STAB_TAU_ADVDIFF_SHAKIB: {
263*a78efa86SJames Wright       CeedScalar gijd_mat[9] = {0.}, gij_uj[3] = {0.};
264*a78efa86SJames Wright 
265*a78efa86SJames Wright       MatMatN(dXdx, dXdx, dim, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat);
266*a78efa86SJames Wright       MatVecNM(gijd_mat, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, gij_uj);
267*a78efa86SJames Wright       return 1 / sqrt(Square(2 * context->Ctau_t / context->dt) + DotN(s.Y.velocity, gij_uj, dim) * context->Ctau_a);
268*a78efa86SJames Wright     } break;
269*a78efa86SJames Wright     default:
270*a78efa86SJames Wright       return 0.;
271*a78efa86SJames Wright   }
272*a78efa86SJames Wright }
273*a78efa86SJames Wright 
2749529d636SJames Wright // *****************************************************************************
2759529d636SJames Wright // This QFunction implements Advection for implicit time stepping method
2769529d636SJames Wright // *****************************************************************************
2779529d636SJames Wright CEED_QFUNCTION_HELPER void IFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
2789529d636SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2799529d636SJames Wright   const CeedScalar(*grad_q)            = in[1];
2809529d636SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
2819529d636SJames Wright   const CeedScalar(*q_data)            = in[3];
2829529d636SJames Wright 
2839529d636SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
2849529d636SJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
2859529d636SJames Wright   CeedScalar *jac_data               = out[2];
2869529d636SJames Wright 
2879529d636SJames Wright   AdvectionContext                 context   = (AdvectionContext)ctx;
2889529d636SJames Wright   const CeedScalar                 zeros[14] = {0.};
2899529d636SJames Wright   NewtonianIdealGasContext         gas;
2909529d636SJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
2919529d636SJames Wright   gas                                         = &gas_struct;
2929529d636SJames Wright 
2939529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2949529d636SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
2959529d636SJames Wright     const State      s     = StateFromU(gas, qi);
2969529d636SJames Wright 
2979529d636SJames Wright     CeedScalar wdetJ, dXdx[9];
2989529d636SJames Wright     QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx);
2999529d636SJames Wright     State grad_s[3];
3009529d636SJames Wright     StatePhysicalGradientFromReference_ND(dim, Q, i, gas, s, STATEVAR_CONSERVATIVE, grad_q, dXdx, grad_s);
3019529d636SJames Wright 
3029529d636SJames Wright     const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total};
3039529d636SJames Wright 
3049529d636SJames Wright     for (CeedInt f = 0; f < 4; f++) {
3059529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0;  // No Change in density or momentum
3069529d636SJames Wright       v[f][i] = wdetJ * q_dot[f][i];                          // K Mass/transient term
3079529d636SJames Wright     }
3089529d636SJames Wright 
3099529d636SJames Wright     CeedScalar div_u = 0;
3109529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) {
3119529d636SJames Wright       for (CeedInt k = 0; k < dim; k++) {
3129529d636SJames Wright         div_u += grad_s[k].Y.velocity[j];
3139529d636SJames Wright       }
3149529d636SJames Wright     }
3159529d636SJames Wright     CeedScalar strong_conv = s.U.E_total * div_u + DotN(s.Y.velocity, Grad_E, dim);
3169529d636SJames Wright     CeedScalar strong_res  = q_dot[4][i] + strong_conv;
3179529d636SJames Wright 
3189529d636SJames Wright     v[4][i] = wdetJ * q_dot[4][i];  // transient part (ALWAYS)
3199529d636SJames Wright 
3209529d636SJames Wright     CeedScalar uX[3] = {0.};
3219529d636SJames Wright     MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
3229529d636SJames Wright 
3239529d636SJames Wright     if (context->strong_form) {  // Strong Galerkin convection term: v div(E u)
3249529d636SJames Wright       v[4][i] += wdetJ * strong_conv;
3259529d636SJames Wright     } else {  // Weak Galerkin convection term: -dv \cdot (E u)
3269529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = -wdetJ * s.U.E_total * uX[j];
3279529d636SJames Wright     }
3289529d636SJames Wright 
329*a78efa86SJames Wright     const CeedScalar TauS = Tau(context, s, dXdx, dim);
3309529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) {
3319529d636SJames Wright         case STAB_NONE:
3329529d636SJames Wright           break;
3339529d636SJames Wright         case STAB_SU:
3349529d636SJames Wright           grad_v[j][4][i] += wdetJ * TauS * strong_conv * uX[j];
3359529d636SJames Wright           break;
3369529d636SJames Wright         case STAB_SUPG:
3379529d636SJames Wright           grad_v[j][4][i] += wdetJ * TauS * strong_res * uX[j];
3389529d636SJames Wright           break;
3399529d636SJames Wright       }
3409529d636SJames Wright     StoredValuesPack(Q, i, 0, 14, zeros, jac_data);
3419529d636SJames Wright   }
3429529d636SJames Wright }
3439529d636SJames Wright 
3442b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
345bd4b5413SJames Wright   IFunction_AdvectionGeneric(ctx, Q, in, out, 3);
346a515125bSLeila Ghaffari   return 0;
347a515125bSLeila Ghaffari }
348a515125bSLeila Ghaffari 
3499529d636SJames Wright CEED_QFUNCTION(IFunction_Advection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3509529d636SJames Wright   IFunction_AdvectionGeneric(ctx, Q, in, out, 2);
3519529d636SJames Wright   return 0;
3529529d636SJames Wright }
3539529d636SJames Wright 
354*a78efa86SJames Wright CEED_QFUNCTION_HELPER void MassFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
355*a78efa86SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
356*a78efa86SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[1];
357*a78efa86SJames Wright   const CeedScalar(*q_data)            = in[2];
358*a78efa86SJames Wright 
359*a78efa86SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
360*a78efa86SJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
361*a78efa86SJames Wright 
362*a78efa86SJames Wright   AdvectionContext                 context    = (AdvectionContext)ctx;
363*a78efa86SJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
364*a78efa86SJames Wright   NewtonianIdealGasContext         gas        = &gas_struct;
365*a78efa86SJames Wright 
366*a78efa86SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
367*a78efa86SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
368*a78efa86SJames Wright     const State      s     = StateFromU(gas, qi);
369*a78efa86SJames Wright     CeedScalar       wdetJ, dXdx[9];
370*a78efa86SJames Wright     QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx);
371*a78efa86SJames Wright 
372*a78efa86SJames Wright     for (CeedInt f = 0; f < 4; f++) {
373*a78efa86SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0;  // No Change in density or momentum
374*a78efa86SJames Wright       v[f][i] = wdetJ * q_dot[f][i];                          // K Mass/transient term
375*a78efa86SJames Wright     }
376*a78efa86SJames Wright 
377*a78efa86SJames Wright     // Unstabilized mass term
378*a78efa86SJames Wright     v[4][i] = wdetJ * q_dot[4][i];
379*a78efa86SJames Wright 
380*a78efa86SJames Wright     // Stabilized mass term
381*a78efa86SJames Wright     CeedScalar uX[3] = {0.};
382*a78efa86SJames Wright     MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
383*a78efa86SJames Wright     const CeedScalar TauS = Tau(context, s, dXdx, dim);
384*a78efa86SJames Wright     for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) {
385*a78efa86SJames Wright         case STAB_NONE:
386*a78efa86SJames Wright         case STAB_SU:
387*a78efa86SJames Wright           grad_v[j][4][i] = 0;
388*a78efa86SJames Wright           break;  // These should be run with the unstabilized mass matrix anyways
389*a78efa86SJames Wright         case STAB_SUPG:
390*a78efa86SJames Wright           grad_v[j][4][i] = wdetJ * TauS * q_dot[4][i] * uX[j];
391*a78efa86SJames Wright           break;
392*a78efa86SJames Wright       }
393*a78efa86SJames Wright   }
394*a78efa86SJames Wright }
395*a78efa86SJames Wright 
396*a78efa86SJames Wright CEED_QFUNCTION(MassFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
397*a78efa86SJames Wright   MassFunction_AdvectionGeneric(ctx, Q, in, out, 3);
398*a78efa86SJames Wright   return 0;
399*a78efa86SJames Wright }
400*a78efa86SJames Wright 
401*a78efa86SJames Wright CEED_QFUNCTION(MassFunction_Advection2D)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
402*a78efa86SJames Wright   MassFunction_AdvectionGeneric(ctx, Q, in, out, 2);
403*a78efa86SJames Wright   return 0;
404*a78efa86SJames Wright }
405*a78efa86SJames Wright 
4069529d636SJames Wright // *****************************************************************************
4079529d636SJames Wright // This QFunction implements Advection for explicit time stepping method
4089529d636SJames Wright // *****************************************************************************
4099529d636SJames Wright CEED_QFUNCTION_HELPER void RHSFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
4109529d636SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
4119529d636SJames Wright   const CeedScalar(*grad_q)        = in[1];
4129529d636SJames Wright   const CeedScalar(*q_data)        = in[2];
4139529d636SJames Wright 
4149529d636SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
4159529d636SJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
4169529d636SJames Wright 
4179529d636SJames Wright   AdvectionContext                 context    = (AdvectionContext)ctx;
4189529d636SJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
419*a78efa86SJames Wright   NewtonianIdealGasContext         gas        = &gas_struct;
4209529d636SJames Wright 
4219529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
4229529d636SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
4239529d636SJames Wright     const State      s     = StateFromU(gas, qi);
4249529d636SJames Wright 
4259529d636SJames Wright     CeedScalar wdetJ, dXdx[9];
4269529d636SJames Wright     QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx);
4279529d636SJames Wright     State grad_s[3];
4289529d636SJames Wright     StatePhysicalGradientFromReference_ND(dim, Q, i, gas, s, STATEVAR_CONSERVATIVE, grad_q, dXdx, grad_s);
4299529d636SJames Wright 
4309529d636SJames Wright     const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total};
4319529d636SJames Wright 
4329529d636SJames Wright     for (CeedInt f = 0; f < 4; f++) {
4339529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0;  // No Change in density or momentum
4349529d636SJames Wright       v[f][i] = 0.;
4359529d636SJames Wright     }
4369529d636SJames Wright 
4379529d636SJames Wright     CeedScalar div_u = 0;
4389529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) {
4399529d636SJames Wright       for (CeedInt k = 0; k < dim; k++) {
4409529d636SJames Wright         div_u += grad_s[k].Y.velocity[j];
4419529d636SJames Wright       }
4429529d636SJames Wright     }
4439529d636SJames Wright     CeedScalar strong_conv = s.U.E_total * div_u + DotN(s.Y.velocity, Grad_E, dim);
4449529d636SJames Wright 
4459529d636SJames Wright     CeedScalar uX[3] = {0.};
4469529d636SJames Wright     MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
4479529d636SJames Wright 
4489529d636SJames Wright     if (context->strong_form) {  // Strong Galerkin convection term: v div(E u)
4499529d636SJames Wright       v[4][i] = -wdetJ * strong_conv;
4509529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = 0;
4519529d636SJames Wright     } else {  // Weak Galerkin convection term: -dv \cdot (E u)
4529529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = wdetJ * s.U.E_total * uX[j];
4539529d636SJames Wright       v[4][i] = 0.;
4549529d636SJames Wright     }
4559529d636SJames Wright 
456*a78efa86SJames Wright     const CeedScalar TauS = Tau(context, s, dXdx, dim);
4579529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) {
4589529d636SJames Wright         case STAB_NONE:
4599529d636SJames Wright           break;
4609529d636SJames Wright         case STAB_SU:
4619529d636SJames Wright         case STAB_SUPG:
4629d860eefSJames Wright           grad_v[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
4639529d636SJames Wright           break;
4649529d636SJames Wright       }
4659529d636SJames Wright   }
4669529d636SJames Wright }
4679529d636SJames Wright 
4689529d636SJames Wright CEED_QFUNCTION(RHS_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4699529d636SJames Wright   RHSFunction_AdvectionGeneric(ctx, Q, in, out, 3);
4709529d636SJames Wright   return 0;
4719529d636SJames Wright }
4729529d636SJames Wright 
4739529d636SJames Wright CEED_QFUNCTION(RHS_Advection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4749529d636SJames Wright   RHSFunction_AdvectionGeneric(ctx, Q, in, out, 2);
4759529d636SJames Wright   return 0;
4769529d636SJames Wright }
4779529d636SJames Wright 
4789529d636SJames Wright // *****************************************************************************
4799529d636SJames Wright // This QFunction implements consistent outflow and inflow BCs
4809529d636SJames Wright //      for advection
4819529d636SJames Wright //
4829529d636SJames Wright //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
4839529d636SJames Wright //    sign(dot(wind, normal)) > 0 : outflow BCs
4849529d636SJames Wright //    sign(dot(wind, normal)) < 0 : inflow BCs
4859529d636SJames Wright //
4869529d636SJames Wright //  Outflow BCs:
4879529d636SJames Wright //    The validity of the weak form of the governing equations is extended to the outflow and the current values of E are applied.
4889529d636SJames Wright //
4899529d636SJames Wright //  Inflow BCs:
4909529d636SJames Wright //    A prescribed Total Energy (E_wind) is applied weakly.
4919529d636SJames Wright // *****************************************************************************
4929529d636SJames Wright CEED_QFUNCTION(Advection_InOutFlowGeneric)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
4939529d636SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
4949529d636SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
4959529d636SJames Wright 
4969529d636SJames Wright   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
4979529d636SJames Wright   AdvectionContext context     = (AdvectionContext)ctx;
4989529d636SJames Wright   const CeedScalar E_wind      = context->E_wind;
4999529d636SJames Wright   const CeedScalar strong_form = context->strong_form;
5009529d636SJames Wright   const bool       is_implicit = context->implicit;
5019529d636SJames Wright 
5029529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
5039529d636SJames Wright     const CeedScalar rho  = q[0][i];
5049529d636SJames Wright     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
5059529d636SJames Wright     const CeedScalar E    = q[4][i];
5069529d636SJames Wright 
5079529d636SJames Wright     CeedScalar wdetJb, norm[3];
5089529d636SJames Wright     QdataBoundaryUnpack_ND(dim, Q, i, q_data_sur, &wdetJb, NULL, norm);
5099529d636SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
5109529d636SJames Wright 
5119529d636SJames Wright     const CeedScalar u_normal = DotN(norm, u, dim);
5129529d636SJames Wright 
5139529d636SJames Wright     // No Change in density or momentum
5149529d636SJames Wright     for (CeedInt j = 0; j < 4; j++) {
5159529d636SJames Wright       v[j][i] = 0;
5169529d636SJames Wright     }
5179529d636SJames Wright     // Implementing in/outflow BCs
5189529d636SJames Wright     if (u_normal > 0) {  // outflow
5199529d636SJames Wright       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
5209529d636SJames Wright     } else {  // inflow
5219529d636SJames Wright       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
5229529d636SJames Wright     }
5239529d636SJames Wright   }
5249529d636SJames Wright   return 0;
5259529d636SJames Wright }
5269529d636SJames Wright 
5272b916ea7SJeremy L Thompson CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5288dba1efaSJames Wright   Advection_InOutFlowGeneric(ctx, Q, in, out, 3);
529a515125bSLeila Ghaffari   return 0;
530a515125bSLeila Ghaffari }
531a515125bSLeila Ghaffari 
5329529d636SJames Wright CEED_QFUNCTION(Advection2d_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5339529d636SJames Wright   Advection_InOutFlowGeneric(ctx, Q, in, out, 2);
5349529d636SJames Wright   return 0;
5359529d636SJames Wright }
536