xref: /libCEED/examples/fluids/qfunctions/advection.h (revision b1559114819c3a66654031bab2fd27b6e4947405)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
377841947SLeila Ghaffari //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
577841947SLeila Ghaffari //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
777841947SLeila Ghaffari 
877841947SLeila Ghaffari /// @file
977841947SLeila Ghaffari /// Advection initial condition and operator for Navier-Stokes example using PETSc
10ba6664aeSJames Wright #include <ceed.h>
11c9c2c079SJeremy L Thompson #include <math.h>
1277841947SLeila Ghaffari 
13c44b1c7dSJames Wright #include "advection_types.h"
148f4d89c8SJames Wright #include "newtonian_state.h"
158f4d89c8SJames Wright #include "newtonian_types.h"
16c44b1c7dSJames Wright #include "stabilization_types.h"
178756a6ccSJames Wright #include "utils.h"
188756a6ccSJames Wright 
1977841947SLeila Ghaffari // *****************************************************************************
2093639ffbSJames Wright // This QFunction sets the initial conditions and the boundary conditions
2193639ffbSJames Wright //   for two test cases: ROTATION and TRANSLATION
2293639ffbSJames Wright //
2393639ffbSJames Wright // -- ROTATION (default)
2493639ffbSJames Wright //      Initial Conditions:
2593639ffbSJames Wright //        Mass Density:
2693639ffbSJames Wright //          Constant mass density of 1.0
2793639ffbSJames Wright //        Momentum Density:
2893639ffbSJames Wright //          Rotational field in x,y
2993639ffbSJames Wright //        Energy Density:
3093639ffbSJames Wright //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
3193639ffbSJames Wright //            increases to (1.-r/rc), then 0. everywhere else
3293639ffbSJames Wright //
3393639ffbSJames Wright //      Boundary Conditions:
3493639ffbSJames Wright //        Mass Density:
3593639ffbSJames Wright //          0.0 flux
3693639ffbSJames Wright //        Momentum Density:
3793639ffbSJames Wright //          0.0
3893639ffbSJames Wright //        Energy Density:
3993639ffbSJames Wright //          0.0 flux
4093639ffbSJames Wright //
4193639ffbSJames Wright // -- TRANSLATION
4293639ffbSJames Wright //      Initial Conditions:
4393639ffbSJames Wright //        Mass Density:
4493639ffbSJames Wright //          Constant mass density of 1.0
4593639ffbSJames Wright //        Momentum Density:
4693639ffbSJames Wright //           Constant rectilinear field in x,y
4793639ffbSJames Wright //        Energy Density:
4893639ffbSJames Wright //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
4993639ffbSJames Wright //            increases to (1.-r/rc), then 0. everywhere else
5093639ffbSJames Wright //
5193639ffbSJames Wright //      Boundary Conditions:
5293639ffbSJames Wright //        Mass Density:
5393639ffbSJames Wright //          0.0 flux
5493639ffbSJames Wright //        Momentum Density:
5593639ffbSJames Wright //          0.0
5693639ffbSJames Wright //        Energy Density:
5793639ffbSJames Wright //          Inflow BCs:
5893639ffbSJames Wright //            E = E_wind
5993639ffbSJames Wright //          Outflow BCs:
6093639ffbSJames Wright //            E = E(boundary)
6193639ffbSJames Wright //          Both In/Outflow BCs for E are applied weakly in the
6293639ffbSJames Wright //            QFunction "Advection2d_Sur"
6393639ffbSJames Wright //
6493639ffbSJames Wright // *****************************************************************************
6593639ffbSJames Wright 
6693639ffbSJames Wright // *****************************************************************************
6793639ffbSJames Wright // This helper function provides the exact, time-dependent solution and IC formulation for 2D advection
6893639ffbSJames Wright // *****************************************************************************
6993639ffbSJames Wright CEED_QFUNCTION_HELPER CeedInt Exact_AdvectionGeneric(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
7093639ffbSJames Wright   const SetupContextAdv context = (SetupContextAdv)ctx;
7193639ffbSJames Wright   const CeedScalar      rc      = context->rc;
7293639ffbSJames Wright   const CeedScalar      lx      = context->lx;
7393639ffbSJames Wright   const CeedScalar      ly      = context->ly;
7493639ffbSJames Wright   const CeedScalar      lz      = dim == 2 ? 0. : context->lz;
7593639ffbSJames Wright   const CeedScalar     *wind    = context->wind;
7693639ffbSJames Wright 
7793639ffbSJames Wright   const CeedScalar center[3] = {0.5 * lx, 0.5 * ly, 0.5 * lz};
7893639ffbSJames Wright   const CeedScalar theta     = dim == 2 ? M_PI / 3 : M_PI;
7993639ffbSJames Wright   const CeedScalar x0[3]     = {center[0] + .25 * lx * cos(theta + time), center[1] + .25 * ly * sin(theta + time), 0.5 * lz};
8093639ffbSJames Wright 
8193639ffbSJames Wright   const CeedScalar x = X[0], y = X[1], z = dim == 2 ? 0. : X[2];
8293639ffbSJames Wright 
8393639ffbSJames Wright   CeedScalar r = 0.;
8493639ffbSJames Wright   switch (context->initial_condition_type) {
8593639ffbSJames Wright     case ADVECTIONIC_BUBBLE_SPHERE:
8693639ffbSJames Wright     case ADVECTIONIC_BUBBLE_CYLINDER:
8793639ffbSJames Wright       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]) + Square(z - x0[2]));
8893639ffbSJames Wright       break;
8993639ffbSJames Wright     case ADVECTIONIC_COSINE_HILL:
9093639ffbSJames Wright       r = sqrt(Square(x - center[0]) + Square(y - center[1]));
9193639ffbSJames Wright       break;
9293639ffbSJames Wright     case ADVECTIONIC_SKEW:
9393639ffbSJames Wright       break;
9493639ffbSJames Wright   }
9593639ffbSJames Wright 
9693639ffbSJames Wright   switch (context->wind_type) {
9793639ffbSJames Wright     case WIND_ROTATION:
9893639ffbSJames Wright       q[0] = 1.;
9993639ffbSJames Wright       q[1] = -(y - center[1]);
10093639ffbSJames Wright       q[2] = (x - center[0]);
10193639ffbSJames Wright       q[3] = 0;
10293639ffbSJames Wright       break;
10393639ffbSJames Wright     case WIND_TRANSLATION:
10493639ffbSJames Wright       q[0] = 1.;
10593639ffbSJames Wright       q[1] = wind[0];
10693639ffbSJames Wright       q[2] = wind[1];
10793639ffbSJames Wright       q[3] = dim == 2 ? 0. : wind[2];
10893639ffbSJames Wright       break;
10993639ffbSJames Wright     default:
11093639ffbSJames Wright       return 1;
11193639ffbSJames Wright   }
11293639ffbSJames Wright 
11393639ffbSJames Wright   switch (context->initial_condition_type) {
11493639ffbSJames Wright     case ADVECTIONIC_BUBBLE_SPHERE:
11593639ffbSJames Wright     case ADVECTIONIC_BUBBLE_CYLINDER:
11693639ffbSJames Wright       switch (context->bubble_continuity_type) {
11793639ffbSJames Wright         // original continuous, smooth shape
11893639ffbSJames Wright         case BUBBLE_CONTINUITY_SMOOTH:
11993639ffbSJames Wright           q[4] = r <= rc ? (1. - r / rc) : 0.;
12093639ffbSJames Wright           break;
12193639ffbSJames Wright         // discontinuous, sharp back half shape
12293639ffbSJames Wright         case BUBBLE_CONTINUITY_BACK_SHARP:
12393639ffbSJames Wright           q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) : 0.;
12493639ffbSJames Wright           break;
12593639ffbSJames Wright         // attempt to define a finite thickness that will get resolved under grid refinement
12693639ffbSJames Wright         case BUBBLE_CONTINUITY_THICK:
12793639ffbSJames Wright           q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) * fmin(1.0, (center[1] - y) / 1.25) : 0.;
12893639ffbSJames Wright           break;
12993639ffbSJames Wright         case BUBBLE_CONTINUITY_COSINE:
13093639ffbSJames Wright           q[4] = r <= rc ? .5 + .5 * cos(r * M_PI / rc) : 0;
13193639ffbSJames Wright           break;
13293639ffbSJames Wright       }
13393639ffbSJames Wright       break;
13493639ffbSJames Wright     case ADVECTIONIC_COSINE_HILL: {
13593639ffbSJames Wright       CeedScalar half_width = context->lx / 2;
13693639ffbSJames Wright       q[4]                  = r > half_width ? 0. : cos(2 * M_PI * r / half_width + M_PI) + 1.;
13793639ffbSJames Wright     } break;
13893639ffbSJames Wright     case ADVECTIONIC_SKEW: {
13993639ffbSJames Wright       CeedScalar       skewed_barrier[3]  = {wind[0], wind[1], 0};
14093639ffbSJames Wright       CeedScalar       inflow_to_point[3] = {x - context->lx / 2, y, 0};
14193639ffbSJames Wright       CeedScalar       cross_product[3]   = {0};
14293639ffbSJames Wright       const CeedScalar boundary_threshold = 20 * CEED_EPSILON;
14393639ffbSJames Wright       Cross3(skewed_barrier, inflow_to_point, cross_product);
14493639ffbSJames Wright 
14593639ffbSJames Wright       q[4] = cross_product[2] > boundary_threshold ? 0 : 1;
14693639ffbSJames Wright       if ((x < boundary_threshold && wind[0] < boundary_threshold) ||                // outflow at -x boundary
14793639ffbSJames Wright           (y < boundary_threshold && wind[1] < boundary_threshold) ||                // outflow at -y boundary
14893639ffbSJames Wright           (x > context->lx - boundary_threshold && wind[0] > boundary_threshold) ||  // outflow at +x boundary
14993639ffbSJames Wright           (y > context->ly - boundary_threshold && wind[1] > boundary_threshold)     // outflow at +y boundary
15093639ffbSJames Wright       ) {
15193639ffbSJames Wright         q[4] = 0;
15293639ffbSJames Wright       }
15393639ffbSJames Wright     } break;
15493639ffbSJames Wright   }
15593639ffbSJames Wright   return 0;
15693639ffbSJames Wright }
15793639ffbSJames Wright 
15893639ffbSJames Wright // *****************************************************************************
15977841947SLeila Ghaffari // This QFunction sets the initial conditions for 3D advection
16077841947SLeila Ghaffari // *****************************************************************************
1612b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
16277841947SLeila Ghaffari   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
16377841947SLeila Ghaffari   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
16477841947SLeila Ghaffari 
16546603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
16677841947SLeila Ghaffari     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
167e6225c47SLeila Ghaffari     CeedScalar       q[5] = {0.};
16877841947SLeila Ghaffari 
16930e1b2c7SJames Wright     Exact_AdvectionGeneric(3, 0., x, 5, q, ctx);
17077841947SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
17130e1b2c7SJames Wright   }
17277841947SLeila Ghaffari   return 0;
17377841947SLeila Ghaffari }
17477841947SLeila Ghaffari 
17577841947SLeila Ghaffari // *****************************************************************************
17693639ffbSJames Wright // This QFunction sets the initial conditions for 2D advection
17777841947SLeila Ghaffari // *****************************************************************************
17893639ffbSJames Wright CEED_QFUNCTION(ICsAdvection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
17993639ffbSJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
18093639ffbSJames Wright   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
18193639ffbSJames Wright   const SetupContextAdv context    = (SetupContextAdv)ctx;
18293639ffbSJames Wright 
18393639ffbSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
18493639ffbSJames Wright     const CeedScalar x[]  = {X[0][i], X[1][i]};
18593639ffbSJames Wright     CeedScalar       q[5] = {0.};
18693639ffbSJames Wright 
18793639ffbSJames Wright     Exact_AdvectionGeneric(2, context->time, x, 5, q, ctx);
18893639ffbSJames Wright     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
18993639ffbSJames Wright   }
19077841947SLeila Ghaffari   return 0;
19177841947SLeila Ghaffari }
19277841947SLeila Ghaffari 
19393639ffbSJames Wright CEED_QFUNCTION_HELPER void QdataUnpack_ND(CeedInt N, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar *dXdx) {
194*b1559114SJames Wright   // Cannot directly use QdataUnpack* helper functions due to SYCL online compiler incompatabilities
19593639ffbSJames Wright   switch (N) {
19693639ffbSJames Wright     case 2:
197*b1559114SJames Wright       StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
198*b1559114SJames Wright       StoredValuesUnpack(Q, i, 1, 4, q_data, dXdx);
19993639ffbSJames Wright       break;
20093639ffbSJames Wright     case 3:
201*b1559114SJames Wright       StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
202*b1559114SJames Wright       StoredValuesUnpack(Q, i, 1, 9, q_data, dXdx);
20393639ffbSJames Wright       break;
20493639ffbSJames Wright   }
20593639ffbSJames Wright }
20693639ffbSJames Wright 
20793639ffbSJames Wright CEED_QFUNCTION_HELPER int QdataBoundaryUnpack_ND(CeedInt N, CeedInt Q, CeedInt i, const CeedScalar *q_data, CeedScalar *wdetJ, CeedScalar *dXdx,
20893639ffbSJames Wright                                                  CeedScalar *normal) {
209*b1559114SJames Wright   // Cannot directly use QdataBoundaryUnpack* helper functions due to SYCL online compiler incompatabilities
21093639ffbSJames Wright   switch (N) {
21193639ffbSJames Wright     case 2:
212*b1559114SJames Wright       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
213*b1559114SJames Wright       if (normal) StoredValuesUnpack(Q, i, 1, 2, q_data, normal);
21493639ffbSJames Wright       break;
21593639ffbSJames Wright     case 3:
216*b1559114SJames Wright       if (wdetJ) StoredValuesUnpack(Q, i, 0, 1, q_data, wdetJ);
217*b1559114SJames Wright       if (normal) StoredValuesUnpack(Q, i, 1, 3, q_data, normal);
218*b1559114SJames Wright       if (dXdx) StoredValuesUnpack(Q, i, 4, 6, q_data, (CeedScalar *)dXdx);
21993639ffbSJames Wright       break;
22093639ffbSJames Wright   }
22193639ffbSJames Wright   return CEED_ERROR_SUCCESS;
22293639ffbSJames Wright }
22393639ffbSJames Wright 
22493639ffbSJames Wright CEED_QFUNCTION_HELPER void StatePhysicalGradientFromReference_ND(CeedInt N, CeedInt Q, CeedInt i, NewtonianIdealGasContext gas, State s,
22593639ffbSJames Wright                                                                  StateVariable state_var, const CeedScalar *grad_q, const CeedScalar *dXdx,
22693639ffbSJames Wright                                                                  State *grad_s) {
22793639ffbSJames Wright   switch (N) {
22893639ffbSJames Wright     case 2: {
22993639ffbSJames Wright       for (CeedInt k = 0; k < 2; k++) {
23093639ffbSJames Wright         CeedScalar dqi[5];
23193639ffbSJames Wright         for (CeedInt j = 0; j < 5; j++) {
23293639ffbSJames 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];
23393639ffbSJames Wright         }
23493639ffbSJames Wright         grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
23593639ffbSJames Wright       }
23693639ffbSJames Wright       CeedScalar U[5] = {0.};
23793639ffbSJames Wright       grad_s[2]       = StateFromU(gas, U);
23893639ffbSJames Wright     } break;
23993639ffbSJames Wright     case 3:
240*b1559114SJames Wright       // Cannot directly use StatePhysicalGradientFromReference helper functions due to SYCL online compiler incompatabilities
241*b1559114SJames Wright       for (CeedInt k = 0; k < 3; k++) {
242*b1559114SJames Wright         CeedScalar dqi[5];
243*b1559114SJames Wright         for (CeedInt j = 0; j < 5; j++) {
244*b1559114SJames 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*b1559114SJames Wright                    grad_q[(Q * 5) * 2 + Q * j + i] * dXdx[2 * N + k];
246*b1559114SJames Wright         }
247*b1559114SJames Wright         grad_s[k] = StateFromQ_fwd(gas, s, dqi, state_var);
248*b1559114SJames Wright       }
24993639ffbSJames Wright       break;
25093639ffbSJames Wright   }
25193639ffbSJames Wright }
25293639ffbSJames Wright 
25393639ffbSJames Wright // *****************************************************************************
25493639ffbSJames Wright // This QFunction implements Advection for implicit time stepping method
25593639ffbSJames Wright // *****************************************************************************
25693639ffbSJames Wright CEED_QFUNCTION_HELPER void IFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
25793639ffbSJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
25893639ffbSJames Wright   const CeedScalar(*grad_q)            = in[1];
25993639ffbSJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
26093639ffbSJames Wright   const CeedScalar(*q_data)            = in[3];
26193639ffbSJames Wright 
26293639ffbSJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
26393639ffbSJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
26493639ffbSJames Wright   CeedScalar *jac_data               = out[2];
26593639ffbSJames Wright 
26693639ffbSJames Wright   AdvectionContext                 context   = (AdvectionContext)ctx;
26793639ffbSJames Wright   const CeedScalar                 CtauS     = context->CtauS;
26893639ffbSJames Wright   const CeedScalar                 zeros[14] = {0.};
26993639ffbSJames Wright   NewtonianIdealGasContext         gas;
27093639ffbSJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
27193639ffbSJames Wright   gas                                         = &gas_struct;
27293639ffbSJames Wright 
27393639ffbSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
27493639ffbSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
27593639ffbSJames Wright     const State      s     = StateFromU(gas, qi);
27693639ffbSJames Wright 
27793639ffbSJames Wright     CeedScalar wdetJ, dXdx[9];
27893639ffbSJames Wright     QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx);
27993639ffbSJames Wright     State grad_s[3];
28093639ffbSJames Wright     StatePhysicalGradientFromReference_ND(dim, Q, i, gas, s, STATEVAR_CONSERVATIVE, grad_q, dXdx, grad_s);
28193639ffbSJames Wright 
28293639ffbSJames Wright     const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total};
28393639ffbSJames Wright 
28493639ffbSJames Wright     for (CeedInt f = 0; f < 4; f++) {
28593639ffbSJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0;  // No Change in density or momentum
28693639ffbSJames Wright       v[f][i] = wdetJ * q_dot[f][i];                          // K Mass/transient term
28793639ffbSJames Wright     }
28893639ffbSJames Wright 
28993639ffbSJames Wright     CeedScalar div_u = 0;
29093639ffbSJames Wright     for (CeedInt j = 0; j < dim; j++) {
29193639ffbSJames Wright       for (CeedInt k = 0; k < dim; k++) {
29293639ffbSJames Wright         div_u += grad_s[k].Y.velocity[j];
29393639ffbSJames Wright       }
29493639ffbSJames Wright     }
29593639ffbSJames Wright     CeedScalar strong_conv = s.U.E_total * div_u + DotN(s.Y.velocity, Grad_E, dim);
29693639ffbSJames Wright     CeedScalar strong_res  = q_dot[4][i] + strong_conv;
29793639ffbSJames Wright 
29893639ffbSJames Wright     v[4][i] = wdetJ * q_dot[4][i];  // transient part (ALWAYS)
29993639ffbSJames Wright 
30093639ffbSJames Wright     CeedScalar uX[3] = {0.};
30193639ffbSJames Wright     MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
30293639ffbSJames Wright 
30393639ffbSJames Wright     if (context->strong_form) {  // Strong Galerkin convection term: v div(E u)
30493639ffbSJames Wright       v[4][i] += wdetJ * strong_conv;
30593639ffbSJames Wright     } else {  // Weak Galerkin convection term: -dv \cdot (E u)
30693639ffbSJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = -wdetJ * s.U.E_total * uX[j];
30793639ffbSJames Wright     }
30893639ffbSJames Wright 
309b18328c4SJames Wright     CeedScalar TauS = 0;
310b18328c4SJames Wright     switch (context->stabilization_tau) {
311b18328c4SJames Wright       case STAB_TAU_CTAU:
312b18328c4SJames Wright         TauS = CtauS / sqrt(Dot3(uX, uX));
313b18328c4SJames Wright         break;
314b18328c4SJames Wright       case STAB_TAU_ADVDIFF_SHAKIB: {
315b18328c4SJames Wright         CeedScalar gijd_mat[9] = {0.}, gij_uj[3] = {0.};
316b18328c4SJames Wright         MatMatN(dXdx, dXdx, dim, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat);
317b18328c4SJames Wright 
318b18328c4SJames Wright         MatVecNM(gijd_mat, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, gij_uj);
319b18328c4SJames Wright         TauS = 1 / sqrt(Square(2 * context->Ctau_t / context->dt) + DotN(s.Y.velocity, gij_uj, dim) * context->Ctau_a);
320b18328c4SJames Wright       } break;
321b18328c4SJames Wright     }
322b18328c4SJames Wright 
32393639ffbSJames Wright     for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) {
32493639ffbSJames Wright         case STAB_NONE:
32593639ffbSJames Wright           break;
32693639ffbSJames Wright         case STAB_SU:
32793639ffbSJames Wright           grad_v[j][4][i] += wdetJ * TauS * strong_conv * uX[j];
32893639ffbSJames Wright           break;
32993639ffbSJames Wright         case STAB_SUPG:
33093639ffbSJames Wright           grad_v[j][4][i] += wdetJ * TauS * strong_res * uX[j];
33193639ffbSJames Wright           break;
33293639ffbSJames Wright       }
33393639ffbSJames Wright     StoredValuesPack(Q, i, 0, 14, zeros, jac_data);
33493639ffbSJames Wright   }
33593639ffbSJames Wright }
33693639ffbSJames Wright 
3372b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
338372d1924SJames Wright   IFunction_AdvectionGeneric(ctx, Q, in, out, 3);
33977841947SLeila Ghaffari   return 0;
34077841947SLeila Ghaffari }
34177841947SLeila Ghaffari 
34293639ffbSJames Wright CEED_QFUNCTION(IFunction_Advection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
34393639ffbSJames Wright   IFunction_AdvectionGeneric(ctx, Q, in, out, 2);
34493639ffbSJames Wright   return 0;
34593639ffbSJames Wright }
34693639ffbSJames Wright 
34793639ffbSJames Wright // *****************************************************************************
34893639ffbSJames Wright // This QFunction implements Advection for explicit time stepping method
34993639ffbSJames Wright // *****************************************************************************
35093639ffbSJames Wright CEED_QFUNCTION_HELPER void RHSFunction_AdvectionGeneric(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
35193639ffbSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
35293639ffbSJames Wright   const CeedScalar(*grad_q)        = in[1];
35393639ffbSJames Wright   const CeedScalar(*q_data)        = in[2];
35493639ffbSJames Wright 
35593639ffbSJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
35693639ffbSJames Wright   CeedScalar(*grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
35793639ffbSJames Wright 
35893639ffbSJames Wright   AdvectionContext                 context = (AdvectionContext)ctx;
35993639ffbSJames Wright   const CeedScalar                 CtauS   = context->CtauS;
36093639ffbSJames Wright   NewtonianIdealGasContext         gas;
36193639ffbSJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
36293639ffbSJames Wright   gas                                         = &gas_struct;
36393639ffbSJames Wright 
36493639ffbSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
36593639ffbSJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
36693639ffbSJames Wright     const State      s     = StateFromU(gas, qi);
36793639ffbSJames Wright 
36893639ffbSJames Wright     CeedScalar wdetJ, dXdx[9];
36993639ffbSJames Wright     QdataUnpack_ND(dim, Q, i, q_data, &wdetJ, dXdx);
37093639ffbSJames Wright     State grad_s[3];
37193639ffbSJames Wright     StatePhysicalGradientFromReference_ND(dim, Q, i, gas, s, STATEVAR_CONSERVATIVE, grad_q, dXdx, grad_s);
37293639ffbSJames Wright 
37393639ffbSJames Wright     const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total};
37493639ffbSJames Wright 
37593639ffbSJames Wright     for (CeedInt f = 0; f < 4; f++) {
37693639ffbSJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][f][i] = 0;  // No Change in density or momentum
37793639ffbSJames Wright       v[f][i] = 0.;
37893639ffbSJames Wright     }
37993639ffbSJames Wright 
38093639ffbSJames Wright     CeedScalar div_u = 0;
38193639ffbSJames Wright     for (CeedInt j = 0; j < dim; j++) {
38293639ffbSJames Wright       for (CeedInt k = 0; k < dim; k++) {
38393639ffbSJames Wright         div_u += grad_s[k].Y.velocity[j];
38493639ffbSJames Wright       }
38593639ffbSJames Wright     }
38693639ffbSJames Wright     CeedScalar strong_conv = s.U.E_total * div_u + DotN(s.Y.velocity, Grad_E, dim);
38793639ffbSJames Wright 
38893639ffbSJames Wright     CeedScalar uX[3] = {0.};
38993639ffbSJames Wright     MatVecNM(dXdx, s.Y.velocity, dim, dim, CEED_NOTRANSPOSE, uX);
39093639ffbSJames Wright 
39193639ffbSJames Wright     if (context->strong_form) {  // Strong Galerkin convection term: v div(E u)
39293639ffbSJames Wright       v[4][i] = -wdetJ * strong_conv;
39393639ffbSJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = 0;
39493639ffbSJames Wright     } else {  // Weak Galerkin convection term: -dv \cdot (E u)
39593639ffbSJames Wright       for (CeedInt j = 0; j < dim; j++) grad_v[j][4][i] = wdetJ * s.U.E_total * uX[j];
39693639ffbSJames Wright       v[4][i] = 0.;
39793639ffbSJames Wright     }
39893639ffbSJames Wright 
39993639ffbSJames Wright     const CeedScalar TauS = CtauS / sqrt(Dot3(uX, uX));
40093639ffbSJames Wright     for (CeedInt j = 0; j < dim; j++) switch (context->stabilization) {
40193639ffbSJames Wright         case STAB_NONE:
40293639ffbSJames Wright           break;
40393639ffbSJames Wright         case STAB_SU:
40493639ffbSJames Wright         case STAB_SUPG:
405fa9a7b5dSJames Wright           grad_v[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
40693639ffbSJames Wright           break;
40793639ffbSJames Wright       }
40893639ffbSJames Wright   }
40993639ffbSJames Wright }
41093639ffbSJames Wright 
41193639ffbSJames Wright CEED_QFUNCTION(RHS_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
41293639ffbSJames Wright   RHSFunction_AdvectionGeneric(ctx, Q, in, out, 3);
41393639ffbSJames Wright   return 0;
41493639ffbSJames Wright }
41593639ffbSJames Wright 
41693639ffbSJames Wright CEED_QFUNCTION(RHS_Advection2d)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
41793639ffbSJames Wright   RHSFunction_AdvectionGeneric(ctx, Q, in, out, 2);
41893639ffbSJames Wright   return 0;
41993639ffbSJames Wright }
42093639ffbSJames Wright 
42193639ffbSJames Wright // *****************************************************************************
42293639ffbSJames Wright // This QFunction implements consistent outflow and inflow BCs
42393639ffbSJames Wright //      for advection
42493639ffbSJames Wright //
42593639ffbSJames Wright //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
42693639ffbSJames Wright //    sign(dot(wind, normal)) > 0 : outflow BCs
42793639ffbSJames Wright //    sign(dot(wind, normal)) < 0 : inflow BCs
42893639ffbSJames Wright //
42993639ffbSJames Wright //  Outflow BCs:
43093639ffbSJames Wright //    The validity of the weak form of the governing equations is extended to the outflow and the current values of E are applied.
43193639ffbSJames Wright //
43293639ffbSJames Wright //  Inflow BCs:
43393639ffbSJames Wright //    A prescribed Total Energy (E_wind) is applied weakly.
43493639ffbSJames Wright // *****************************************************************************
43593639ffbSJames Wright CEED_QFUNCTION(Advection_InOutFlowGeneric)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out, CeedInt dim) {
43693639ffbSJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
43793639ffbSJames Wright   const CeedScalar(*q_data_sur)    = in[2];
43893639ffbSJames Wright 
43993639ffbSJames Wright   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
44093639ffbSJames Wright   AdvectionContext context     = (AdvectionContext)ctx;
44193639ffbSJames Wright   const CeedScalar E_wind      = context->E_wind;
44293639ffbSJames Wright   const CeedScalar strong_form = context->strong_form;
44393639ffbSJames Wright   const bool       is_implicit = context->implicit;
44493639ffbSJames Wright 
44593639ffbSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
44693639ffbSJames Wright     const CeedScalar rho  = q[0][i];
44793639ffbSJames Wright     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
44893639ffbSJames Wright     const CeedScalar E    = q[4][i];
44993639ffbSJames Wright 
45093639ffbSJames Wright     CeedScalar wdetJb, norm[3];
45193639ffbSJames Wright     QdataBoundaryUnpack_ND(dim, Q, i, q_data_sur, &wdetJb, NULL, norm);
45293639ffbSJames Wright     wdetJb *= is_implicit ? -1. : 1.;
45393639ffbSJames Wright 
45493639ffbSJames Wright     const CeedScalar u_normal = DotN(norm, u, dim);
45593639ffbSJames Wright 
45693639ffbSJames Wright     // No Change in density or momentum
45793639ffbSJames Wright     for (CeedInt j = 0; j < 4; j++) {
45893639ffbSJames Wright       v[j][i] = 0;
45993639ffbSJames Wright     }
46093639ffbSJames Wright     // Implementing in/outflow BCs
46193639ffbSJames Wright     if (u_normal > 0) {  // outflow
46293639ffbSJames Wright       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
46393639ffbSJames Wright     } else {  // inflow
46493639ffbSJames Wright       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
46593639ffbSJames Wright     }
46693639ffbSJames Wright   }
46793639ffbSJames Wright   return 0;
46893639ffbSJames Wright }
46993639ffbSJames Wright 
4702b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
4714bdcf5bfSJames Wright   Advection_InOutFlowGeneric(ctx, Q, in, out, 3);
47277841947SLeila Ghaffari   return 0;
47377841947SLeila Ghaffari }
47477841947SLeila Ghaffari 
47593639ffbSJames Wright CEED_QFUNCTION(Advection2d_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
47693639ffbSJames Wright   Advection_InOutFlowGeneric(ctx, Q, in, out, 2);
47793639ffbSJames Wright   return 0;
47893639ffbSJames Wright }
479