xref: /libCEED/examples/fluids/qfunctions/advection.h (revision 8f4d89c8df549c32b50fce05e7e0d564ef69ad05)
13d8e8822SJeremy L Thompson // Copyright (c) 2017-2022, 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
1077841947SLeila Ghaffari 
1177841947SLeila Ghaffari #ifndef advection_h
1277841947SLeila Ghaffari #define advection_h
1377841947SLeila Ghaffari 
14ba6664aeSJames Wright #include <ceed.h>
15c9c2c079SJeremy L Thompson #include <math.h>
1677841947SLeila Ghaffari 
17c44b1c7dSJames Wright #include "advection_types.h"
18*8f4d89c8SJames Wright #include "newtonian_state.h"
19*8f4d89c8SJames Wright #include "newtonian_types.h"
20c44b1c7dSJames Wright #include "stabilization_types.h"
218756a6ccSJames Wright #include "utils.h"
228756a6ccSJames Wright 
2397baf651SJames Wright typedef struct SetupContextAdv_ *SetupContextAdv;
2497baf651SJames Wright struct SetupContextAdv_ {
2577841947SLeila Ghaffari   CeedScalar           rc;
2677841947SLeila Ghaffari   CeedScalar           lx;
2777841947SLeila Ghaffari   CeedScalar           ly;
2877841947SLeila Ghaffari   CeedScalar           lz;
2977841947SLeila Ghaffari   CeedScalar           wind[3];
3077841947SLeila Ghaffari   CeedScalar           time;
31700ae941SJames Wright   WindType             wind_type;
327b77ddfdSJames Wright   AdvectionICType      initial_condition_type;
33700ae941SJames Wright   BubbleContinuityType bubble_continuity_type;
3477841947SLeila Ghaffari };
3577841947SLeila Ghaffari 
3677841947SLeila Ghaffari // *****************************************************************************
3777841947SLeila Ghaffari // This QFunction sets the initial conditions and the boundary conditions
3877841947SLeila Ghaffari //   for two test cases: ROTATION and TRANSLATION
3977841947SLeila Ghaffari //
4077841947SLeila Ghaffari // -- ROTATION (default)
4177841947SLeila Ghaffari //      Initial Conditions:
4277841947SLeila Ghaffari //        Mass Density:
4377841947SLeila Ghaffari //          Constant mass density of 1.0
4477841947SLeila Ghaffari //        Momentum Density:
4577841947SLeila Ghaffari //          Rotational field in x,y
4677841947SLeila Ghaffari //        Energy Density:
4777841947SLeila Ghaffari //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
4877841947SLeila Ghaffari //            increases to (1.-r/rc), then 0. everywhere else
4977841947SLeila Ghaffari //
5077841947SLeila Ghaffari //      Boundary Conditions:
5177841947SLeila Ghaffari //        Mass Density:
5277841947SLeila Ghaffari //          0.0 flux
5377841947SLeila Ghaffari //        Momentum Density:
5477841947SLeila Ghaffari //          0.0
5577841947SLeila Ghaffari //        Energy Density:
5677841947SLeila Ghaffari //          0.0 flux
5777841947SLeila Ghaffari //
5877841947SLeila Ghaffari // -- TRANSLATION
5977841947SLeila Ghaffari //      Initial Conditions:
6077841947SLeila Ghaffari //        Mass Density:
6177841947SLeila Ghaffari //          Constant mass density of 1.0
6277841947SLeila Ghaffari //        Momentum Density:
6377841947SLeila Ghaffari //           Constant rectilinear field in x,y
6477841947SLeila Ghaffari //        Energy Density:
6577841947SLeila Ghaffari //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
6677841947SLeila Ghaffari //            increases to (1.-r/rc), then 0. everywhere else
6777841947SLeila Ghaffari //
6877841947SLeila Ghaffari //      Boundary Conditions:
6977841947SLeila Ghaffari //        Mass Density:
7077841947SLeila Ghaffari //          0.0 flux
7177841947SLeila Ghaffari //        Momentum Density:
7277841947SLeila Ghaffari //          0.0
7377841947SLeila Ghaffari //        Energy Density:
7477841947SLeila Ghaffari //          Inflow BCs:
7577841947SLeila Ghaffari //            E = E_wind
7677841947SLeila Ghaffari //          Outflow BCs:
7777841947SLeila Ghaffari //            E = E(boundary)
7877841947SLeila Ghaffari //          Both In/Outflow BCs for E are applied weakly in the
7977841947SLeila Ghaffari //            QFunction "Advection_Sur"
8077841947SLeila Ghaffari //
8177841947SLeila Ghaffari // *****************************************************************************
8277841947SLeila Ghaffari 
8377841947SLeila Ghaffari // *****************************************************************************
84ea61e9acSJeremy L Thompson // This helper function provides support for the exact, time-dependent solution (currently not implemented) and IC formulation for 3D advection
8577841947SLeila Ghaffari // *****************************************************************************
862b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedInt Exact_Advection(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
8797baf651SJames Wright   const SetupContextAdv context = (SetupContextAdv)ctx;
8877841947SLeila Ghaffari   const CeedScalar      rc      = context->rc;
8977841947SLeila Ghaffari   const CeedScalar      lx      = context->lx;
9077841947SLeila Ghaffari   const CeedScalar      ly      = context->ly;
9177841947SLeila Ghaffari   const CeedScalar      lz      = context->lz;
9277841947SLeila Ghaffari   const CeedScalar     *wind    = context->wind;
9377841947SLeila Ghaffari 
9477841947SLeila Ghaffari   // Setup
9577841947SLeila Ghaffari   const CeedScalar x0[3]     = {0.25 * lx, 0.5 * ly, 0.5 * lz};
9677841947SLeila Ghaffari   const CeedScalar center[3] = {0.5 * lx, 0.5 * ly, 0.5 * lz};
9777841947SLeila Ghaffari 
9877841947SLeila Ghaffari   // -- Coordinates
9977841947SLeila Ghaffari   const CeedScalar x = X[0];
10077841947SLeila Ghaffari   const CeedScalar y = X[1];
10177841947SLeila Ghaffari   const CeedScalar z = X[2];
10277841947SLeila Ghaffari 
10377841947SLeila Ghaffari   // -- Energy
10477841947SLeila Ghaffari   CeedScalar r = 0.;
1057b77ddfdSJames Wright   switch (context->initial_condition_type) {
1067b77ddfdSJames Wright     case ADVECTIONIC_BUBBLE_SPHERE:  // (dim=3)
1072b730f8bSJeremy L Thompson       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]) + Square(z - x0[2]));
108c44b1c7dSJames Wright       break;
1097b77ddfdSJames Wright     case ADVECTIONIC_BUBBLE_CYLINDER:  // (dim=2)
110c32eb7cbSJed Brown       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]));
111c44b1c7dSJames Wright       break;
1127b77ddfdSJames Wright     case ADVECTIONIC_COSINE_HILL:
113c44b1c7dSJames Wright       r = sqrt(Square(x - center[0]) + Square(y - center[1]));
114c44b1c7dSJames Wright       break;
1157b77ddfdSJames Wright     case ADVECTIONIC_SKEW:
116c44b1c7dSJames Wright       break;
11777841947SLeila Ghaffari   }
11877841947SLeila Ghaffari 
11977841947SLeila Ghaffari   // Initial Conditions
120c44b1c7dSJames Wright   CeedScalar wind_scaling = 1.;
12177841947SLeila Ghaffari   switch (context->wind_type) {
122700ae941SJames Wright     case WIND_ROTATION:
12377841947SLeila Ghaffari       q[0] = 1.;
124c44b1c7dSJames Wright       q[1] = -wind_scaling * (y - center[1]);
125c44b1c7dSJames Wright       q[2] = wind_scaling * (x - center[0]);
12677841947SLeila Ghaffari       q[3] = 0;
12777841947SLeila Ghaffari       break;
128700ae941SJames Wright     case WIND_TRANSLATION:
12977841947SLeila Ghaffari       q[0] = 1.;
13077841947SLeila Ghaffari       q[1] = wind[0];
13177841947SLeila Ghaffari       q[2] = wind[1];
13277841947SLeila Ghaffari       q[3] = wind[2];
13377841947SLeila Ghaffari       break;
13477841947SLeila Ghaffari   }
13577841947SLeila Ghaffari 
1367b77ddfdSJames Wright   switch (context->initial_condition_type) {
1377b77ddfdSJames Wright     case ADVECTIONIC_BUBBLE_SPHERE:
1387b77ddfdSJames Wright     case ADVECTIONIC_BUBBLE_CYLINDER:
13977841947SLeila Ghaffari       switch (context->bubble_continuity_type) {
14077841947SLeila Ghaffari         // original continuous, smooth shape
141c44b1c7dSJames Wright         case BUBBLE_CONTINUITY_SMOOTH:
14277841947SLeila Ghaffari           q[4] = r <= rc ? (1. - r / rc) : 0.;
143c44b1c7dSJames Wright           break;
14477841947SLeila Ghaffari         // discontinuous, sharp back half shape
145c44b1c7dSJames Wright         case BUBBLE_CONTINUITY_BACK_SHARP:
14677841947SLeila Ghaffari           q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) : 0.;
147c44b1c7dSJames Wright           break;
14877841947SLeila Ghaffari         // attempt to define a finite thickness that will get resolved under grid refinement
149c44b1c7dSJames Wright         case BUBBLE_CONTINUITY_THICK:
1502b730f8bSJeremy L Thompson           q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) * fmin(1.0, (center[1] - y) / 1.25) : 0.;
151c44b1c7dSJames Wright           break;
152c44b1c7dSJames Wright       }
153c44b1c7dSJames Wright       break;
1547b77ddfdSJames Wright     case ADVECTIONIC_COSINE_HILL: {
155c44b1c7dSJames Wright       CeedScalar half_width = context->lx / 2;
156c44b1c7dSJames Wright       q[4]                  = r > half_width ? 0. : cos(2 * M_PI * r / half_width + M_PI) + 1.;
157c44b1c7dSJames Wright     } break;
1587b77ddfdSJames Wright     case ADVECTIONIC_SKEW: {
159c44b1c7dSJames Wright       CeedScalar       skewed_barrier[3]  = {wind[0], wind[1], 0};
160c44b1c7dSJames Wright       CeedScalar       inflow_to_point[3] = {x - context->lx / 2, y, 0};
161c44b1c7dSJames Wright       CeedScalar       cross_product[3]   = {0};
162a43bcf8eSJames Wright       const CeedScalar boundary_threshold = 20 * CEED_EPSILON;
163c44b1c7dSJames Wright       Cross3(skewed_barrier, inflow_to_point, cross_product);
164c44b1c7dSJames Wright 
165a43bcf8eSJames Wright       q[4] = cross_product[2] > boundary_threshold ? 0 : 1;
166a43bcf8eSJames Wright       if ((x < boundary_threshold && wind[0] < boundary_threshold) ||                // outflow at -x boundary
167a43bcf8eSJames Wright           (y < boundary_threshold && wind[1] < boundary_threshold) ||                // outflow at -y boundary
168a43bcf8eSJames Wright           (x > context->lx - boundary_threshold && wind[0] > boundary_threshold) ||  // outflow at +x boundary
169a43bcf8eSJames Wright           (y > context->ly - boundary_threshold && wind[1] > boundary_threshold)     // outflow at +y boundary
170c44b1c7dSJames Wright       ) {
171c44b1c7dSJames Wright         q[4] = 0;
172c44b1c7dSJames Wright       }
17377841947SLeila Ghaffari     } break;
17477841947SLeila Ghaffari   }
175c44b1c7dSJames Wright 
17677841947SLeila Ghaffari   return 0;
17777841947SLeila Ghaffari }
17877841947SLeila Ghaffari 
17977841947SLeila Ghaffari // *****************************************************************************
18077841947SLeila Ghaffari // This QFunction sets the initial conditions for 3D advection
18177841947SLeila Ghaffari // *****************************************************************************
1822b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
18377841947SLeila Ghaffari   // Inputs
18477841947SLeila Ghaffari   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
18577841947SLeila Ghaffari   // Outputs
18677841947SLeila Ghaffari   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
18777841947SLeila Ghaffari 
18877841947SLeila Ghaffari   // Quadrature Point Loop
18946603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
19077841947SLeila Ghaffari     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
191e6225c47SLeila Ghaffari     CeedScalar       q[5] = {0.};
19277841947SLeila Ghaffari 
19377841947SLeila Ghaffari     Exact_Advection(3, 0., x, 5, q, ctx);
19477841947SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
19577841947SLeila Ghaffari   }  // End of Quadrature Point Loop
19677841947SLeila Ghaffari 
19777841947SLeila Ghaffari   // Return
19877841947SLeila Ghaffari   return 0;
19977841947SLeila Ghaffari }
20077841947SLeila Ghaffari 
20177841947SLeila Ghaffari // *****************************************************************************
20277841947SLeila Ghaffari // This QFunction implements the following formulation of the advection equation
20377841947SLeila Ghaffari //
20477841947SLeila Ghaffari // This is 3D advection given in two formulations based upon the weak form.
20577841947SLeila Ghaffari //
20677841947SLeila Ghaffari // State Variables: q = ( rho, U1, U2, U3, E )
20777841947SLeila Ghaffari //   rho - Mass Density
20877841947SLeila Ghaffari //   Ui  - Momentum Density    ,  Ui = rho ui
20977841947SLeila Ghaffari //   E   - Total Energy Density
21077841947SLeila Ghaffari //
21177841947SLeila Ghaffari // Advection Equation:
21277841947SLeila Ghaffari //   dE/dt + div( E u ) = 0
21377841947SLeila Ghaffari // *****************************************************************************
2142b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
21577841947SLeila Ghaffari   // Inputs
21646603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
21746603fc5SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
218f3e15844SJames Wright   const CeedScalar(*q_data)            = in[2];
21977841947SLeila Ghaffari 
22077841947SLeila Ghaffari   // Outputs
22146603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
22246603fc5SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
22377841947SLeila Ghaffari 
22477841947SLeila Ghaffari   // Context
22577841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
22677841947SLeila Ghaffari   const CeedScalar CtauS       = context->CtauS;
22777841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
22877841947SLeila Ghaffari 
22977841947SLeila Ghaffari   // Quadrature Point Loop
23046603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
23177841947SLeila Ghaffari     // Setup
23277841947SLeila Ghaffari     // -- Interp in
23377841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
2342b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
23577841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
23677841947SLeila Ghaffari     // -- Grad in
2372b730f8bSJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
2382b730f8bSJeremy L Thompson     const CeedScalar du[3][3] = {
2392b730f8bSJeremy L Thompson         {(dq[0][1][i] - drho[0] * u[0]) / rho, (dq[1][1][i] - drho[1] * u[0]) / rho, (dq[2][1][i] - drho[2] * u[0]) / rho},
2402b730f8bSJeremy L Thompson         {(dq[0][2][i] - drho[0] * u[1]) / rho, (dq[1][2][i] - drho[1] * u[1]) / rho, (dq[2][2][i] - drho[2] * u[1]) / rho},
2412b730f8bSJeremy L Thompson         {(dq[0][3][i] - drho[0] * u[2]) / rho, (dq[1][3][i] - drho[1] * u[2]) / rho, (dq[2][3][i] - drho[2] * u[2]) / rho}
24277841947SLeila Ghaffari     };
2432b730f8bSJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
244f3e15844SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
245f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
24677841947SLeila Ghaffari     // The Physics
24777841947SLeila Ghaffari     // Note with the order that du was filled and the order that dXdx was filled
24877841947SLeila Ghaffari     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k})
24977841947SLeila Ghaffari     //   dXdx[k][j] = dX_K / dx_j
25077841947SLeila Ghaffari     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
25177841947SLeila Ghaffari     //   x_j and u_j are jth  physical position and velocity components
25277841947SLeila Ghaffari 
25377841947SLeila Ghaffari     // No Change in density or momentum
25477841947SLeila Ghaffari     for (CeedInt f = 0; f < 4; f++) {
2552b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
25677841947SLeila Ghaffari       v[f][i] = 0;
25777841947SLeila Ghaffari     }
25877841947SLeila Ghaffari 
25977841947SLeila Ghaffari     // -- Total Energy
26077841947SLeila Ghaffari     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
26177841947SLeila Ghaffari     // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
26277841947SLeila Ghaffari     CeedScalar div_u = 0, u_dot_grad_E = 0;
26377841947SLeila Ghaffari     for (CeedInt j = 0; j < 3; j++) {
26477841947SLeila Ghaffari       CeedScalar dEdx_j = 0;
26577841947SLeila Ghaffari       for (CeedInt k = 0; k < 3; k++) {
26677841947SLeila Ghaffari         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
26777841947SLeila Ghaffari         dEdx_j += dE[k] * dXdx[k][j];
26877841947SLeila Ghaffari       }
26977841947SLeila Ghaffari       u_dot_grad_E += u[j] * dEdx_j;
27077841947SLeila Ghaffari     }
27177841947SLeila Ghaffari     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
27277841947SLeila Ghaffari 
27377841947SLeila Ghaffari     // Weak Galerkin convection term: dv \cdot (E u)
2742b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] = (1 - strong_form) * wdetJ * E * (u[0] * dXdx[j][0] + u[1] * dXdx[j][1] + u[2] * dXdx[j][2]);
27577841947SLeila Ghaffari     v[4][i] = 0;
27677841947SLeila Ghaffari 
27777841947SLeila Ghaffari     // Strong Galerkin convection term: - v div(E u)
27877841947SLeila Ghaffari     v[4][i] = -strong_form * wdetJ * strong_conv;
27977841947SLeila Ghaffari 
28077841947SLeila Ghaffari     // Stabilization requires a measure of element transit time in the velocity
28177841947SLeila Ghaffari     //   field u.
28277841947SLeila Ghaffari     CeedScalar uX[3];
2832b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) uX[j] = dXdx[j][0] * u[0] + dXdx[j][1] * u[1] + dXdx[j][2] * u[2];
2844bd6ffc9SJames Wright     const CeedScalar TauS = CtauS / sqrt(Dot3(uX, uX));
2852b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
28677841947SLeila Ghaffari   }  // End Quadrature Point Loop
28777841947SLeila Ghaffari 
28877841947SLeila Ghaffari   return 0;
28977841947SLeila Ghaffari }
29077841947SLeila Ghaffari 
29177841947SLeila Ghaffari // *****************************************************************************
292ea61e9acSJeremy L Thompson // This QFunction implements 3D (mentioned above) with implicit time stepping method
29377841947SLeila Ghaffari // *****************************************************************************
2942b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
29546603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
296*8f4d89c8SJames Wright   const CeedScalar(*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
29746603fc5SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[2];
298f3e15844SJames Wright   const CeedScalar(*q_data)                = in[3];
29946603fc5SJames Wright 
30046603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
301*8f4d89c8SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
30229ea4e10SJames Wright   CeedScalar *jac_data               = out[2];
30346603fc5SJames Wright 
30477841947SLeila Ghaffari   AdvectionContext                 context     = (AdvectionContext)ctx;
30577841947SLeila Ghaffari   const CeedScalar                 CtauS       = context->CtauS;
30677841947SLeila Ghaffari   const CeedScalar                 strong_form = context->strong_form;
30729ea4e10SJames Wright   const CeedScalar                 zeros[14]   = {0.};
308*8f4d89c8SJames Wright   NewtonianIdealGasContext         gas;
309*8f4d89c8SJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
310*8f4d89c8SJames Wright   gas                                         = &gas_struct;
31177841947SLeila Ghaffari 
31246603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
313*8f4d89c8SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
314*8f4d89c8SJames Wright     const State      s     = StateFromU(gas, qi);
315*8f4d89c8SJames Wright 
316f3e15844SJames Wright     CeedScalar wdetJ, dXdx[3][3];
317f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
318*8f4d89c8SJames Wright     State grad_s[3];
319*8f4d89c8SJames Wright     StatePhysicalGradientFromReference(Q, i, gas, s, STATEVAR_CONSERVATIVE, (CeedScalar *)Grad_q, dXdx, grad_s);
32077841947SLeila Ghaffari 
321*8f4d89c8SJames Wright     const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total};
322*8f4d89c8SJames Wright 
32377841947SLeila Ghaffari     for (CeedInt f = 0; f < 4; f++) {
324*8f4d89c8SJames Wright       for (CeedInt j = 0; j < 3; j++) Grad_v[j][f][i] = 0;  // No Change in density or momentum
32577841947SLeila Ghaffari       v[f][i] = wdetJ * q_dot[f][i];                        // K Mass/transient term
32677841947SLeila Ghaffari     }
32777841947SLeila Ghaffari 
328*8f4d89c8SJames Wright     CeedScalar div_u = 0;
32977841947SLeila Ghaffari     for (CeedInt j = 0; j < 3; j++) {
33077841947SLeila Ghaffari       for (CeedInt k = 0; k < 3; k++) {
331*8f4d89c8SJames Wright         div_u += grad_s[k].Y.velocity[j];
33277841947SLeila Ghaffari       }
33377841947SLeila Ghaffari     }
334*8f4d89c8SJames Wright     CeedScalar strong_conv = s.U.E_total * div_u + Dot3(s.Y.velocity, Grad_E);
33577841947SLeila Ghaffari     CeedScalar strong_res  = q_dot[4][i] + strong_conv;
33677841947SLeila Ghaffari 
33777841947SLeila Ghaffari     v[4][i] = wdetJ * q_dot[4][i];  // transient part (ALWAYS)
33877841947SLeila Ghaffari 
339*8f4d89c8SJames Wright     if (strong_form) {  // Strong Galerkin convection term: v div(E u)
340*8f4d89c8SJames Wright       v[4][i] += wdetJ * strong_conv;
341*8f4d89c8SJames Wright     } else {  // Weak Galerkin convection term: -dv \cdot (E u)
342*8f4d89c8SJames Wright       for (CeedInt j = 0; j < 3; j++)
343*8f4d89c8SJames Wright         Grad_v[j][4][i] = -wdetJ * s.U.E_total * (s.Y.velocity[0] * dXdx[j][0] + s.Y.velocity[1] * dXdx[j][1] + s.Y.velocity[2] * dXdx[j][2]);
344*8f4d89c8SJames Wright     }
34577841947SLeila Ghaffari 
346*8f4d89c8SJames Wright     // Stabilization requires a measure of element transit time in the velocity field u.
347*8f4d89c8SJames Wright     CeedScalar uX[3] = {0.};
348*8f4d89c8SJames Wright     MatVec3(dXdx, s.Y.velocity, CEED_NOTRANSPOSE, uX);
349*8f4d89c8SJames Wright     const CeedScalar TauS = CtauS / sqrt(Dot3(uX, uX));
35077841947SLeila Ghaffari 
3512b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) switch (context->stabilization) {
352700ae941SJames Wright         case STAB_NONE:
35377841947SLeila Ghaffari           break;
354700ae941SJames Wright         case STAB_SU:
355*8f4d89c8SJames Wright           Grad_v[j][4][i] += wdetJ * TauS * strong_conv * uX[j];
35677841947SLeila Ghaffari           break;
357700ae941SJames Wright         case STAB_SUPG:
358*8f4d89c8SJames Wright           Grad_v[j][4][i] += wdetJ * TauS * strong_res * uX[j];
35977841947SLeila Ghaffari           break;
36077841947SLeila Ghaffari       }
36129ea4e10SJames Wright     StoredValuesPack(Q, i, 0, 14, zeros, jac_data);
362*8f4d89c8SJames Wright   }
36377841947SLeila Ghaffari   return 0;
36477841947SLeila Ghaffari }
36577841947SLeila Ghaffari 
36677841947SLeila Ghaffari // *****************************************************************************
36777841947SLeila Ghaffari // This QFunction implements consistent outflow and inflow BCs
36877841947SLeila Ghaffari //      for 3D advection
36977841947SLeila Ghaffari //
37077841947SLeila Ghaffari //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
37177841947SLeila Ghaffari //    sign(dot(wind, normal)) > 0 : outflow BCs
37277841947SLeila Ghaffari //    sign(dot(wind, normal)) < 0 : inflow BCs
37377841947SLeila Ghaffari //
37477841947SLeila Ghaffari //  Outflow BCs:
375ea61e9acSJeremy L Thompson //    The validity of the weak form of the governing equations is extended to the outflow and the current values of E are applied.
37677841947SLeila Ghaffari //
37777841947SLeila Ghaffari //  Inflow BCs:
37877841947SLeila Ghaffari //    A prescribed Total Energy (E_wind) is applied weakly.
37977841947SLeila Ghaffari // *****************************************************************************
3802b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
38177841947SLeila Ghaffari   // Inputs
38246603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
383f3e15844SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
38446603fc5SJames Wright 
38577841947SLeila Ghaffari   // Outputs
38677841947SLeila Ghaffari   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
38777841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
38877841947SLeila Ghaffari   const CeedScalar E_wind      = context->E_wind;
38977841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
390f3e15844SJames Wright   const bool       is_implicit = context->implicit;
39177841947SLeila Ghaffari 
39277841947SLeila Ghaffari   // Quadrature Point Loop
39346603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
39477841947SLeila Ghaffari     // Setup
39577841947SLeila Ghaffari     // -- Interp in
39677841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
3972b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
39877841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
39977841947SLeila Ghaffari 
400f3e15844SJames Wright     CeedScalar wdetJb, norm[3];
401f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
402f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
40377841947SLeila Ghaffari 
40477841947SLeila Ghaffari     // Normal velocity
40577841947SLeila Ghaffari     const CeedScalar u_normal = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2];
40677841947SLeila Ghaffari 
40777841947SLeila Ghaffari     // No Change in density or momentum
40877841947SLeila Ghaffari     for (CeedInt j = 0; j < 4; j++) {
40977841947SLeila Ghaffari       v[j][i] = 0;
41077841947SLeila Ghaffari     }
41177841947SLeila Ghaffari     // Implementing in/outflow BCs
41277841947SLeila Ghaffari     if (u_normal > 0) {  // outflow
41377841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
41477841947SLeila Ghaffari     } else {  // inflow
41577841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
41677841947SLeila Ghaffari     }
41777841947SLeila Ghaffari   }  // End Quadrature Point Loop
41877841947SLeila Ghaffari   return 0;
41977841947SLeila Ghaffari }
42077841947SLeila Ghaffari // *****************************************************************************
42177841947SLeila Ghaffari 
42277841947SLeila Ghaffari #endif  // advection_h
423