xref: /honee/qfunctions/advection.h (revision 85efd435779a9fe8fc2f4b9b9925ef60d2ae745f)
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) {
194*85efd435SJames Wright   // Cannot directly use QdataUnpack* helper functions due to SYCL online compiler incompatabilities
1959529d636SJames Wright   switch (N) {
1969529d636SJames Wright     case 2:
197*85efd435SJames Wright       StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
198*85efd435SJames Wright       StoredValuesUnpack(Q, i, 1, 4, q_data, dXdx);
1999529d636SJames Wright       break;
2009529d636SJames Wright     case 3:
201*85efd435SJames Wright       StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
202*85efd435SJames 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) {
209*85efd435SJames Wright   // Cannot directly use QdataBoundaryUnpack* helper functions due to SYCL online compiler incompatabilities
2109529d636SJames Wright   switch (N) {
2119529d636SJames Wright     case 2:
212*85efd435SJames Wright       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
213*85efd435SJames Wright       if (normal) StoredValuesUnpack(Q, i, 1, 2, q_data, normal);
2149529d636SJames Wright       break;
2159529d636SJames Wright     case 3:
216*85efd435SJames Wright       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
217*85efd435SJames Wright       if (normal) StoredValuesUnpack(Q, i, 1, 3, q_data, normal);
218*85efd435SJames 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:
240*85efd435SJames Wright       // Cannot directly use StatePhysicalGradientFromReference helper functions due to SYCL online compiler incompatabilities
241*85efd435SJames Wright       for (CeedInt k = 0; k < 3; k++) {
242*85efd435SJames Wright         CeedScalar dqi[5];
243*85efd435SJames Wright         for (CeedInt j = 0; j < 5; j++) {
244*85efd435SJames 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] +
245*85efd435SJames Wright                    grad_q[(Q * 5) * 2 + Q * j + i] * dXdx[2 * N + k];
246*85efd435SJames Wright         }
247*85efd435SJames Wright         grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
248*85efd435SJames Wright       }
2499529d636SJames Wright       break;
2509529d636SJames Wright   }
2519529d636SJames Wright }
2529529d636SJames Wright 
2539529d636SJames Wright // *****************************************************************************
2549529d636SJames Wright // This QFunction implements Advection for implicit time stepping method
2559529d636SJames Wright // *****************************************************************************
2569529d636SJames Wright CEED_QFUNCTION_HELPER void IFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
2579529d636SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2589529d636SJames Wright   const CeedScalar(*grad_q)            = in[1];
2599529d636SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
2609529d636SJames Wright   const CeedScalar(*q_data)            = in[3];
2619529d636SJames Wright 
2629529d636SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
2639529d636SJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
2649529d636SJames Wright   CeedScalar *jac_data               = out[2];
2659529d636SJames Wright 
2669529d636SJames Wright   AdvectionContext                 context   = (AdvectionContext)ctx;
2679529d636SJames Wright   const CeedScalar                 CtauS     = context->CtauS;
2689529d636SJames Wright   const CeedScalar                 zeros[14] = {0.};
2699529d636SJames Wright   NewtonianIdealGasContext         gas;
2709529d636SJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
2719529d636SJames Wright   gas                                         = &gas_struct;
2729529d636SJames Wright 
2739529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
2749529d636SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
2759529d636SJames Wright     const State      s     = StateFromU(gas, qi);
2769529d636SJames Wright 
2779529d636SJames Wright     CeedScalar wdetJ, dXdx[9];
2789529d636SJames Wright     QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx);
2799529d636SJames Wright     State grad_s[3];
2809529d636SJames Wright     StatePhysicalGradientFromReference_ND(dim, Q, i, gas, s, STATEVAR_CONSERVATIVE, grad_q, dXdx, grad_s);
2819529d636SJames Wright 
2829529d636SJames Wright     const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total};
2839529d636SJames Wright 
2849529d636SJames Wright     for (CeedInt f = 0; f < 4; f++) {
2859529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0;  // No Change in density or momentum
2869529d636SJames Wright       v[f][i] = wdetJ * q_dot[f][i];                          // K Mass/transient term
2879529d636SJames Wright     }
2889529d636SJames Wright 
2899529d636SJames Wright     CeedScalar div_u = 0;
2909529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) {
2919529d636SJames Wright       for (CeedInt k = 0; k < dim; k++) {
2929529d636SJames Wright         div_u += grad_s[k].Y.velocity[j];
2939529d636SJames Wright       }
2949529d636SJames Wright     }
2959529d636SJames Wright     CeedScalar strong_conv = s.U.E_total * div_u + DotN(s.Y.velocity, Grad_E, dim);
2969529d636SJames Wright     CeedScalar strong_res  = q_dot[4][i] + strong_conv;
2979529d636SJames Wright 
2989529d636SJames Wright     v[4][i] = wdetJ * q_dot[4][i];  // transient part (ALWAYS)
2999529d636SJames Wright 
3009529d636SJames Wright     CeedScalar uX[3] = {0.};
3019529d636SJames Wright     MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
3029529d636SJames Wright 
3039529d636SJames Wright     if (context->strong_form) {  // Strong Galerkin convection term: v div(E u)
3049529d636SJames Wright       v[4][i] += wdetJ * strong_conv;
3059529d636SJames Wright     } else {  // Weak Galerkin convection term: -dv \cdot (E u)
3069529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = -wdetJ * s.U.E_total * uX[j];
3079529d636SJames Wright     }
3089529d636SJames Wright 
30957272ee0SJames Wright     CeedScalar TauS = 0;
31057272ee0SJames Wright     switch (context->stabilization_tau) {
31157272ee0SJames Wright       case STAB_TAU_CTAU:
31257272ee0SJames Wright         TauS = CtauS / sqrt(Dot3(uX, uX));
31357272ee0SJames Wright         break;
31457272ee0SJames Wright       case STAB_TAU_ADVDIFF_SHAKIB: {
31557272ee0SJames Wright         CeedScalar gijd_mat[9] = {0.}, gij_uj[3] = {0.};
31657272ee0SJames Wright         MatMatN(dXdx, dXdx, dim, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat);
31757272ee0SJames Wright 
31857272ee0SJames Wright         MatVecNM(gijd_mat, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, gij_uj);
31957272ee0SJames Wright         TauS = 1 / sqrt(Square(2 * context->Ctau_t / context->dt) + DotN(s.Y.velocity, gij_uj, dim) * context->Ctau_a);
32057272ee0SJames Wright       } break;
32157272ee0SJames Wright     }
32257272ee0SJames Wright 
3239529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) {
3249529d636SJames Wright         case STAB_NONE:
3259529d636SJames Wright           break;
3269529d636SJames Wright         case STAB_SU:
3279529d636SJames Wright           grad_v[j][4][i] += wdetJ * TauS * strong_conv * uX[j];
3289529d636SJames Wright           break;
3299529d636SJames Wright         case STAB_SUPG:
3309529d636SJames Wright           grad_v[j][4][i] += wdetJ * TauS * strong_res * uX[j];
3319529d636SJames Wright           break;
3329529d636SJames Wright       }
3339529d636SJames Wright     StoredValuesPack(Q, i, 0, 14, zeros, jac_data);
3349529d636SJames Wright   }
3359529d636SJames Wright }
3369529d636SJames Wright 
3372b916ea7SJeremy L Thompson CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
338bd4b5413SJames Wright   IFunction_AdvectionGeneric(ctx, Q, in, out, 3);
339a515125bSLeila Ghaffari   return 0;
340a515125bSLeila Ghaffari }
341a515125bSLeila Ghaffari 
3429529d636SJames Wright CEED_QFUNCTION(IFunction_Advection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
3439529d636SJames Wright   IFunction_AdvectionGeneric(ctx, Q, in, out, 2);
3449529d636SJames Wright   return 0;
3459529d636SJames Wright }
3469529d636SJames Wright 
3479529d636SJames Wright // *****************************************************************************
3489529d636SJames Wright // This QFunction implements Advection for explicit time stepping method
3499529d636SJames Wright // *****************************************************************************
3509529d636SJames Wright CEED_QFUNCTION_HELPER void RHSFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
3519529d636SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
3529529d636SJames Wright   const CeedScalar(*grad_q)        = in[1];
3539529d636SJames Wright   const CeedScalar(*q_data)        = in[2];
3549529d636SJames Wright 
3559529d636SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
3569529d636SJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
3579529d636SJames Wright 
3589529d636SJames Wright   AdvectionContext                 context = (AdvectionContext)ctx;
3599529d636SJames Wright   const CeedScalar                 CtauS   = context->CtauS;
3609529d636SJames Wright   NewtonianIdealGasContext         gas;
3619529d636SJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
3629529d636SJames Wright   gas                                         = &gas_struct;
3639529d636SJames Wright 
3649529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
3659529d636SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
3669529d636SJames Wright     const State      s     = StateFromU(gas, qi);
3679529d636SJames Wright 
3689529d636SJames Wright     CeedScalar wdetJ, dXdx[9];
3699529d636SJames Wright     QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx);
3709529d636SJames Wright     State grad_s[3];
3719529d636SJames Wright     StatePhysicalGradientFromReference_ND(dim, Q, i, gas, s, STATEVAR_CONSERVATIVE, grad_q, dXdx, grad_s);
3729529d636SJames Wright 
3739529d636SJames Wright     const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total};
3749529d636SJames Wright 
3759529d636SJames Wright     for (CeedInt f = 0; f < 4; f++) {
3769529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0;  // No Change in density or momentum
3779529d636SJames Wright       v[f][i] = 0.;
3789529d636SJames Wright     }
3799529d636SJames Wright 
3809529d636SJames Wright     CeedScalar div_u = 0;
3819529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) {
3829529d636SJames Wright       for (CeedInt k = 0; k < dim; k++) {
3839529d636SJames Wright         div_u += grad_s[k].Y.velocity[j];
3849529d636SJames Wright       }
3859529d636SJames Wright     }
3869529d636SJames Wright     CeedScalar strong_conv = s.U.E_total * div_u + DotN(s.Y.velocity, Grad_E, dim);
3879529d636SJames Wright 
3889529d636SJames Wright     CeedScalar uX[3] = {0.};
3899529d636SJames Wright     MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
3909529d636SJames Wright 
3919529d636SJames Wright     if (context->strong_form) {  // Strong Galerkin convection term: v div(E u)
3929529d636SJames Wright       v[4][i] = -wdetJ * strong_conv;
3939529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = 0;
3949529d636SJames Wright     } else {  // Weak Galerkin convection term: -dv \cdot (E u)
3959529d636SJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = wdetJ * s.U.E_total * uX[j];
3969529d636SJames Wright       v[4][i] = 0.;
3979529d636SJames Wright     }
3989529d636SJames Wright 
3999529d636SJames Wright     const CeedScalar TauS = CtauS / sqrt(Dot3(uX, uX));
4009529d636SJames Wright     for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) {
4019529d636SJames Wright         case STAB_NONE:
4029529d636SJames Wright           break;
4039529d636SJames Wright         case STAB_SU:
4049529d636SJames Wright         case STAB_SUPG:
4059d860eefSJames Wright           grad_v[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
4069529d636SJames Wright           break;
4079529d636SJames Wright       }
4089529d636SJames Wright   }
4099529d636SJames Wright }
4109529d636SJames Wright 
4119529d636SJames Wright CEED_QFUNCTION(RHS_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4129529d636SJames Wright   RHSFunction_AdvectionGeneric(ctx, Q, in, out, 3);
4139529d636SJames Wright   return 0;
4149529d636SJames Wright }
4159529d636SJames Wright 
4169529d636SJames Wright CEED_QFUNCTION(RHS_Advection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4179529d636SJames Wright   RHSFunction_AdvectionGeneric(ctx, Q, in, out, 2);
4189529d636SJames Wright   return 0;
4199529d636SJames Wright }
4209529d636SJames Wright 
4219529d636SJames Wright // *****************************************************************************
4229529d636SJames Wright // This QFunction implements consistent outflow and inflow BCs
4239529d636SJames Wright //      for advection
4249529d636SJames Wright //
4259529d636SJames Wright //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
4269529d636SJames Wright //    sign(dot(wind, normal)) > 0 : outflow BCs
4279529d636SJames Wright //    sign(dot(wind, normal)) < 0 : inflow BCs
4289529d636SJames Wright //
4299529d636SJames Wright //  Outflow BCs:
4309529d636SJames Wright //    The validity of the weak form of the governing equations is extended to the outflow and the current values of E are applied.
4319529d636SJames Wright //
4329529d636SJames Wright //  Inflow BCs:
4339529d636SJames Wright //    A prescribed Total Energy (E_wind) is applied weakly.
4349529d636SJames Wright // *****************************************************************************
4359529d636SJames Wright CEED_QFUNCTION(Advection_InOutFlowGeneric)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
4369529d636SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
4379529d636SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
4389529d636SJames Wright 
4399529d636SJames Wright   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
4409529d636SJames Wright   AdvectionContext context     = (AdvectionContext)ctx;
4419529d636SJames Wright   const CeedScalar E_wind      = context->E_wind;
4429529d636SJames Wright   const CeedScalar strong_form = context->strong_form;
4439529d636SJames Wright   const bool       is_implicit = context->implicit;
4449529d636SJames Wright 
4459529d636SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
4469529d636SJames Wright     const CeedScalar rho  = q[0][i];
4479529d636SJames Wright     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
4489529d636SJames Wright     const CeedScalar E    = q[4][i];
4499529d636SJames Wright 
4509529d636SJames Wright     CeedScalar wdetJb, norm[3];
4519529d636SJames Wright     QdataBoundaryUnpack_ND(dim, Q, i, q_data_sur, &wdetJb, NULL, norm);
4529529d636SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
4539529d636SJames Wright 
4549529d636SJames Wright     const CeedScalar u_normal = DotN(norm, u, dim);
4559529d636SJames Wright 
4569529d636SJames Wright     // No Change in density or momentum
4579529d636SJames Wright     for (CeedInt j = 0; j < 4; j++) {
4589529d636SJames Wright       v[j][i] = 0;
4599529d636SJames Wright     }
4609529d636SJames Wright     // Implementing in/outflow BCs
4619529d636SJames Wright     if (u_normal > 0) {  // outflow
4629529d636SJames Wright       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
4639529d636SJames Wright     } else {  // inflow
4649529d636SJames Wright       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
4659529d636SJames Wright     }
4669529d636SJames Wright   }
4679529d636SJames Wright   return 0;
4689529d636SJames Wright }
4699529d636SJames Wright 
4702b916ea7SJeremy L Thompson CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4718dba1efaSJames Wright   Advection_InOutFlowGeneric(ctx, Q, in, out, 3);
472a515125bSLeila Ghaffari   return 0;
473a515125bSLeila Ghaffari }
474a515125bSLeila Ghaffari 
4759529d636SJames Wright CEED_QFUNCTION(Advection2d_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4769529d636SJames Wright   Advection_InOutFlowGeneric(ctx, Q, in, out, 2);
4779529d636SJames Wright   return 0;
4789529d636SJames Wright }
479