xref: /libCEED/examples/fluids/qfunctions/advection.h (revision 29ea4e1095af0be46b4a5cfdf830ac9197977b48)
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 
17700ae941SJames Wright #include "../qfunctions/advection_types.h"
18700ae941SJames Wright #include "../qfunctions/stabilization_types.h"
198756a6ccSJames Wright #include "utils.h"
208756a6ccSJames Wright 
2197baf651SJames Wright typedef struct SetupContextAdv_ *SetupContextAdv;
2297baf651SJames Wright struct SetupContextAdv_ {
2377841947SLeila Ghaffari   CeedScalar           rc;
2477841947SLeila Ghaffari   CeedScalar           lx;
2577841947SLeila Ghaffari   CeedScalar           ly;
2677841947SLeila Ghaffari   CeedScalar           lz;
2777841947SLeila Ghaffari   CeedScalar           wind[3];
2877841947SLeila Ghaffari   CeedScalar           time;
29700ae941SJames Wright   WindType             wind_type;
30700ae941SJames Wright   BubbleType           bubble_type;
31700ae941SJames Wright   BubbleContinuityType bubble_continuity_type;
3277841947SLeila Ghaffari };
3377841947SLeila Ghaffari 
3477841947SLeila Ghaffari // *****************************************************************************
3577841947SLeila Ghaffari // This QFunction sets the initial conditions and the boundary conditions
3677841947SLeila Ghaffari //   for two test cases: ROTATION and TRANSLATION
3777841947SLeila Ghaffari //
3877841947SLeila Ghaffari // -- ROTATION (default)
3977841947SLeila Ghaffari //      Initial Conditions:
4077841947SLeila Ghaffari //        Mass Density:
4177841947SLeila Ghaffari //          Constant mass density of 1.0
4277841947SLeila Ghaffari //        Momentum Density:
4377841947SLeila Ghaffari //          Rotational field in x,y
4477841947SLeila Ghaffari //        Energy Density:
4577841947SLeila Ghaffari //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
4677841947SLeila Ghaffari //            increases to (1.-r/rc), then 0. everywhere else
4777841947SLeila Ghaffari //
4877841947SLeila Ghaffari //      Boundary Conditions:
4977841947SLeila Ghaffari //        Mass Density:
5077841947SLeila Ghaffari //          0.0 flux
5177841947SLeila Ghaffari //        Momentum Density:
5277841947SLeila Ghaffari //          0.0
5377841947SLeila Ghaffari //        Energy Density:
5477841947SLeila Ghaffari //          0.0 flux
5577841947SLeila Ghaffari //
5677841947SLeila Ghaffari // -- TRANSLATION
5777841947SLeila Ghaffari //      Initial Conditions:
5877841947SLeila Ghaffari //        Mass Density:
5977841947SLeila Ghaffari //          Constant mass density of 1.0
6077841947SLeila Ghaffari //        Momentum Density:
6177841947SLeila Ghaffari //           Constant rectilinear field in x,y
6277841947SLeila Ghaffari //        Energy Density:
6377841947SLeila Ghaffari //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
6477841947SLeila Ghaffari //            increases to (1.-r/rc), then 0. everywhere else
6577841947SLeila Ghaffari //
6677841947SLeila Ghaffari //      Boundary Conditions:
6777841947SLeila Ghaffari //        Mass Density:
6877841947SLeila Ghaffari //          0.0 flux
6977841947SLeila Ghaffari //        Momentum Density:
7077841947SLeila Ghaffari //          0.0
7177841947SLeila Ghaffari //        Energy Density:
7277841947SLeila Ghaffari //          Inflow BCs:
7377841947SLeila Ghaffari //            E = E_wind
7477841947SLeila Ghaffari //          Outflow BCs:
7577841947SLeila Ghaffari //            E = E(boundary)
7677841947SLeila Ghaffari //          Both In/Outflow BCs for E are applied weakly in the
7777841947SLeila Ghaffari //            QFunction "Advection_Sur"
7877841947SLeila Ghaffari //
7977841947SLeila Ghaffari // *****************************************************************************
8077841947SLeila Ghaffari 
8177841947SLeila Ghaffari // *****************************************************************************
82ea61e9acSJeremy L Thompson // This helper function provides support for the exact, time-dependent solution (currently not implemented) and IC formulation for 3D advection
8377841947SLeila Ghaffari // *****************************************************************************
842b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedInt Exact_Advection(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
8597baf651SJames Wright   const SetupContextAdv context = (SetupContextAdv)ctx;
8677841947SLeila Ghaffari   const CeedScalar      rc      = context->rc;
8777841947SLeila Ghaffari   const CeedScalar      lx      = context->lx;
8877841947SLeila Ghaffari   const CeedScalar      ly      = context->ly;
8977841947SLeila Ghaffari   const CeedScalar      lz      = context->lz;
9077841947SLeila Ghaffari   const CeedScalar     *wind    = context->wind;
9177841947SLeila Ghaffari 
9277841947SLeila Ghaffari   // Setup
9377841947SLeila Ghaffari   const CeedScalar x0[3]     = {0.25 * lx, 0.5 * ly, 0.5 * lz};
9477841947SLeila Ghaffari   const CeedScalar center[3] = {0.5 * lx, 0.5 * ly, 0.5 * lz};
9577841947SLeila Ghaffari 
9677841947SLeila Ghaffari   // -- Coordinates
9777841947SLeila Ghaffari   const CeedScalar x = X[0];
9877841947SLeila Ghaffari   const CeedScalar y = X[1];
9977841947SLeila Ghaffari   const CeedScalar z = X[2];
10077841947SLeila Ghaffari 
10177841947SLeila Ghaffari   // -- Energy
10277841947SLeila Ghaffari   CeedScalar r = 0.;
10377841947SLeila Ghaffari   switch (context->bubble_type) {
104700ae941SJames Wright     case BUBBLE_SPHERE: {  // (dim=3)
1052b730f8bSJeremy L Thompson       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]) + Square(z - x0[2]));
10677841947SLeila Ghaffari     } break;
107700ae941SJames Wright     case BUBBLE_CYLINDER: {  // (dim=2)
108c32eb7cbSJed Brown       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]));
10977841947SLeila Ghaffari     } break;
11077841947SLeila Ghaffari   }
11177841947SLeila Ghaffari 
11277841947SLeila Ghaffari   // Initial Conditions
11377841947SLeila Ghaffari   switch (context->wind_type) {
114700ae941SJames Wright     case WIND_ROTATION:
11577841947SLeila Ghaffari       q[0] = 1.;
11677841947SLeila Ghaffari       q[1] = -(y - center[1]);
11777841947SLeila Ghaffari       q[2] = (x - center[0]);
11877841947SLeila Ghaffari       q[3] = 0;
11977841947SLeila Ghaffari       break;
120700ae941SJames Wright     case WIND_TRANSLATION:
12177841947SLeila Ghaffari       q[0] = 1.;
12277841947SLeila Ghaffari       q[1] = wind[0];
12377841947SLeila Ghaffari       q[2] = wind[1];
12477841947SLeila Ghaffari       q[3] = wind[2];
12577841947SLeila Ghaffari       break;
12677841947SLeila Ghaffari   }
12777841947SLeila Ghaffari 
12877841947SLeila Ghaffari   switch (context->bubble_continuity_type) {
12977841947SLeila Ghaffari     // original continuous, smooth shape
130700ae941SJames Wright     case BUBBLE_CONTINUITY_SMOOTH: {
13177841947SLeila Ghaffari       q[4] = r <= rc ? (1. - r / rc) : 0.;
13277841947SLeila Ghaffari     } break;
13377841947SLeila Ghaffari     // discontinuous, sharp back half shape
134700ae941SJames Wright     case BUBBLE_CONTINUITY_BACK_SHARP: {
13577841947SLeila Ghaffari       q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) : 0.;
13677841947SLeila Ghaffari     } break;
13777841947SLeila Ghaffari     // attempt to define a finite thickness that will get resolved under grid refinement
138700ae941SJames Wright     case BUBBLE_CONTINUITY_THICK: {
1392b730f8bSJeremy L Thompson       q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) * fmin(1.0, (center[1] - y) / 1.25) : 0.;
14077841947SLeila Ghaffari     } break;
14177841947SLeila Ghaffari   }
14277841947SLeila Ghaffari   return 0;
14377841947SLeila Ghaffari }
14477841947SLeila Ghaffari 
14577841947SLeila Ghaffari // *****************************************************************************
14677841947SLeila Ghaffari // This QFunction sets the initial conditions for 3D advection
14777841947SLeila Ghaffari // *****************************************************************************
1482b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
14977841947SLeila Ghaffari   // Inputs
15077841947SLeila Ghaffari   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
15177841947SLeila Ghaffari   // Outputs
15277841947SLeila Ghaffari   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
15377841947SLeila Ghaffari 
15477841947SLeila Ghaffari   // Quadrature Point Loop
15546603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
15677841947SLeila Ghaffari     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
157e6225c47SLeila Ghaffari     CeedScalar       q[5] = {0.};
15877841947SLeila Ghaffari 
15977841947SLeila Ghaffari     Exact_Advection(3, 0., x, 5, q, ctx);
16077841947SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
16177841947SLeila Ghaffari   }  // End of Quadrature Point Loop
16277841947SLeila Ghaffari 
16377841947SLeila Ghaffari   // Return
16477841947SLeila Ghaffari   return 0;
16577841947SLeila Ghaffari }
16677841947SLeila Ghaffari 
16777841947SLeila Ghaffari // *****************************************************************************
16877841947SLeila Ghaffari // This QFunction implements the following formulation of the advection equation
16977841947SLeila Ghaffari //
17077841947SLeila Ghaffari // This is 3D advection given in two formulations based upon the weak form.
17177841947SLeila Ghaffari //
17277841947SLeila Ghaffari // State Variables: q = ( rho, U1, U2, U3, E )
17377841947SLeila Ghaffari //   rho - Mass Density
17477841947SLeila Ghaffari //   Ui  - Momentum Density    ,  Ui = rho ui
17577841947SLeila Ghaffari //   E   - Total Energy Density
17677841947SLeila Ghaffari //
17777841947SLeila Ghaffari // Advection Equation:
17877841947SLeila Ghaffari //   dE/dt + div( E u ) = 0
17977841947SLeila Ghaffari // *****************************************************************************
1802b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
18177841947SLeila Ghaffari   // Inputs
18246603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
18346603fc5SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
184f3e15844SJames Wright   const CeedScalar(*q_data)            = in[2];
18577841947SLeila Ghaffari 
18677841947SLeila Ghaffari   // Outputs
18746603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
18846603fc5SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
18977841947SLeila Ghaffari 
19077841947SLeila Ghaffari   // Context
19177841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
19277841947SLeila Ghaffari   const CeedScalar CtauS       = context->CtauS;
19377841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
19477841947SLeila Ghaffari 
19577841947SLeila Ghaffari   // Quadrature Point Loop
19646603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
19777841947SLeila Ghaffari     // Setup
19877841947SLeila Ghaffari     // -- Interp in
19977841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
2002b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
20177841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
20277841947SLeila Ghaffari     // -- Grad in
2032b730f8bSJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
2042b730f8bSJeremy L Thompson     const CeedScalar du[3][3] = {
2052b730f8bSJeremy 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},
2062b730f8bSJeremy 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},
2072b730f8bSJeremy 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}
20877841947SLeila Ghaffari     };
2092b730f8bSJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
210f3e15844SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
211f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
21277841947SLeila Ghaffari     // The Physics
21377841947SLeila Ghaffari     // Note with the order that du was filled and the order that dXdx was filled
21477841947SLeila Ghaffari     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k})
21577841947SLeila Ghaffari     //   dXdx[k][j] = dX_K / dx_j
21677841947SLeila Ghaffari     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
21777841947SLeila Ghaffari     //   x_j and u_j are jth  physical position and velocity components
21877841947SLeila Ghaffari 
21977841947SLeila Ghaffari     // No Change in density or momentum
22077841947SLeila Ghaffari     for (CeedInt f = 0; f < 4; f++) {
2212b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
22277841947SLeila Ghaffari       v[f][i] = 0;
22377841947SLeila Ghaffari     }
22477841947SLeila Ghaffari 
22577841947SLeila Ghaffari     // -- Total Energy
22677841947SLeila Ghaffari     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
22777841947SLeila Ghaffari     // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
22877841947SLeila Ghaffari     CeedScalar div_u = 0, u_dot_grad_E = 0;
22977841947SLeila Ghaffari     for (CeedInt j = 0; j < 3; j++) {
23077841947SLeila Ghaffari       CeedScalar dEdx_j = 0;
23177841947SLeila Ghaffari       for (CeedInt k = 0; k < 3; k++) {
23277841947SLeila Ghaffari         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
23377841947SLeila Ghaffari         dEdx_j += dE[k] * dXdx[k][j];
23477841947SLeila Ghaffari       }
23577841947SLeila Ghaffari       u_dot_grad_E += u[j] * dEdx_j;
23677841947SLeila Ghaffari     }
23777841947SLeila Ghaffari     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
23877841947SLeila Ghaffari 
23977841947SLeila Ghaffari     // Weak Galerkin convection term: dv \cdot (E u)
2402b730f8bSJeremy 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]);
24177841947SLeila Ghaffari     v[4][i] = 0;
24277841947SLeila Ghaffari 
24377841947SLeila Ghaffari     // Strong Galerkin convection term: - v div(E u)
24477841947SLeila Ghaffari     v[4][i] = -strong_form * wdetJ * strong_conv;
24577841947SLeila Ghaffari 
24677841947SLeila Ghaffari     // Stabilization requires a measure of element transit time in the velocity
24777841947SLeila Ghaffari     //   field u.
24877841947SLeila Ghaffari     CeedScalar uX[3];
2492b730f8bSJeremy 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];
25077841947SLeila Ghaffari     const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]);
2512b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
25277841947SLeila Ghaffari   }  // End Quadrature Point Loop
25377841947SLeila Ghaffari 
25477841947SLeila Ghaffari   return 0;
25577841947SLeila Ghaffari }
25677841947SLeila Ghaffari 
25777841947SLeila Ghaffari // *****************************************************************************
258ea61e9acSJeremy L Thompson // This QFunction implements 3D (mentioned above) with implicit time stepping method
25977841947SLeila Ghaffari // *****************************************************************************
2602b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
26177841947SLeila Ghaffari   // Inputs
26246603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
26346603fc5SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
26446603fc5SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
265f3e15844SJames Wright   const CeedScalar(*q_data)            = in[3];
26646603fc5SJames Wright 
26777841947SLeila Ghaffari   // Outputs
26846603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
26946603fc5SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
270*29ea4e10SJames Wright   CeedScalar *jac_data           = out[2];
27146603fc5SJames Wright 
27277841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
27377841947SLeila Ghaffari   const CeedScalar CtauS       = context->CtauS;
27477841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
275*29ea4e10SJames Wright   const CeedScalar zeros[14]   = {0.};
27677841947SLeila Ghaffari 
27777841947SLeila Ghaffari   // Quadrature Point Loop
27846603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
27977841947SLeila Ghaffari     // Setup
28077841947SLeila Ghaffari     // -- Interp in
28177841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
2822b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
28377841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
28477841947SLeila Ghaffari     // -- Grad in
2852b730f8bSJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
2862b730f8bSJeremy L Thompson     const CeedScalar du[3][3] = {
2872b730f8bSJeremy 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},
2882b730f8bSJeremy 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},
2892b730f8bSJeremy 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}
29077841947SLeila Ghaffari     };
2912b730f8bSJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
292f3e15844SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
293f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
29477841947SLeila Ghaffari     // The Physics
29577841947SLeila Ghaffari     // Note with the order that du was filled and the order that dXdx was filled
29677841947SLeila Ghaffari     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k} )
29777841947SLeila Ghaffari     //   dXdx[k][j] = dX_K / dx_j
29877841947SLeila Ghaffari     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
29977841947SLeila Ghaffari     //   x_j and u_j are jth  physical position and velocity components
30077841947SLeila Ghaffari 
30177841947SLeila Ghaffari     // No Change in density or momentum
30277841947SLeila Ghaffari     for (CeedInt f = 0; f < 4; f++) {
3032b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
30477841947SLeila Ghaffari       v[f][i] = wdetJ * q_dot[f][i];  // K Mass/transient term
30577841947SLeila Ghaffari     }
30677841947SLeila Ghaffari 
30777841947SLeila Ghaffari     // -- Total Energy
30877841947SLeila Ghaffari     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
30977841947SLeila Ghaffari     //   or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
31077841947SLeila Ghaffari     CeedScalar div_u = 0, u_dot_grad_E = 0;
31177841947SLeila Ghaffari     for (CeedInt j = 0; j < 3; j++) {
31277841947SLeila Ghaffari       CeedScalar dEdx_j = 0;
31377841947SLeila Ghaffari       for (CeedInt k = 0; k < 3; k++) {
31477841947SLeila Ghaffari         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
31577841947SLeila Ghaffari         dEdx_j += dE[k] * dXdx[k][j];
31677841947SLeila Ghaffari       }
31777841947SLeila Ghaffari       u_dot_grad_E += u[j] * dEdx_j;
31877841947SLeila Ghaffari     }
31977841947SLeila Ghaffari     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
32077841947SLeila Ghaffari     CeedScalar strong_res  = q_dot[4][i] + strong_conv;
32177841947SLeila Ghaffari 
32277841947SLeila Ghaffari     v[4][i] = wdetJ * q_dot[4][i];  // transient part (ALWAYS)
32377841947SLeila Ghaffari 
32477841947SLeila Ghaffari     // Weak Galerkin convection term: -dv \cdot (E u)
3252b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] = -wdetJ * (1 - strong_form) * E * (u[0] * dXdx[j][0] + u[1] * dXdx[j][1] + u[2] * dXdx[j][2]);
32677841947SLeila Ghaffari 
32777841947SLeila Ghaffari     // Strong Galerkin convection term: v div(E u)
32877841947SLeila Ghaffari     v[4][i] += wdetJ * strong_form * strong_conv;
32977841947SLeila Ghaffari 
33077841947SLeila Ghaffari     // Stabilization requires a measure of element transit time in the velocity
33177841947SLeila Ghaffari     //   field u.
33277841947SLeila Ghaffari     CeedScalar uX[3];
3332b730f8bSJeremy 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];
33477841947SLeila Ghaffari     const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]);
33577841947SLeila Ghaffari 
3362b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) switch (context->stabilization) {
337700ae941SJames Wright         case STAB_NONE:
33877841947SLeila Ghaffari           break;
339700ae941SJames Wright         case STAB_SU:
340700ae941SJames Wright           dv[j][4][i] += wdetJ * TauS * strong_conv * uX[j];
34177841947SLeila Ghaffari           break;
342700ae941SJames Wright         case STAB_SUPG:
343700ae941SJames Wright           dv[j][4][i] += wdetJ * TauS * strong_res * uX[j];
34477841947SLeila Ghaffari           break;
34577841947SLeila Ghaffari       }
346*29ea4e10SJames Wright     StoredValuesPack(Q, i, 0, 14, zeros, jac_data);
34777841947SLeila Ghaffari   }  // End Quadrature Point Loop
34877841947SLeila Ghaffari 
34977841947SLeila Ghaffari   return 0;
35077841947SLeila Ghaffari }
35177841947SLeila Ghaffari 
35277841947SLeila Ghaffari // *****************************************************************************
35377841947SLeila Ghaffari // This QFunction implements consistent outflow and inflow BCs
35477841947SLeila Ghaffari //      for 3D advection
35577841947SLeila Ghaffari //
35677841947SLeila Ghaffari //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
35777841947SLeila Ghaffari //    sign(dot(wind, normal)) > 0 : outflow BCs
35877841947SLeila Ghaffari //    sign(dot(wind, normal)) < 0 : inflow BCs
35977841947SLeila Ghaffari //
36077841947SLeila Ghaffari //  Outflow BCs:
361ea61e9acSJeremy 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.
36277841947SLeila Ghaffari //
36377841947SLeila Ghaffari //  Inflow BCs:
36477841947SLeila Ghaffari //    A prescribed Total Energy (E_wind) is applied weakly.
36577841947SLeila Ghaffari // *****************************************************************************
3662b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
36777841947SLeila Ghaffari   // Inputs
36846603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
369f3e15844SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
37046603fc5SJames Wright 
37177841947SLeila Ghaffari   // Outputs
37277841947SLeila Ghaffari   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
37377841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
37477841947SLeila Ghaffari   const CeedScalar E_wind      = context->E_wind;
37577841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
376f3e15844SJames Wright   const bool       is_implicit = context->implicit;
37777841947SLeila Ghaffari 
37877841947SLeila Ghaffari   // Quadrature Point Loop
37946603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
38077841947SLeila Ghaffari     // Setup
38177841947SLeila Ghaffari     // -- Interp in
38277841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
3832b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
38477841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
38577841947SLeila Ghaffari 
386f3e15844SJames Wright     CeedScalar wdetJb, norm[3];
387f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
388f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
38977841947SLeila Ghaffari 
39077841947SLeila Ghaffari     // Normal velocity
39177841947SLeila Ghaffari     const CeedScalar u_normal = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2];
39277841947SLeila Ghaffari 
39377841947SLeila Ghaffari     // No Change in density or momentum
39477841947SLeila Ghaffari     for (CeedInt j = 0; j < 4; j++) {
39577841947SLeila Ghaffari       v[j][i] = 0;
39677841947SLeila Ghaffari     }
39777841947SLeila Ghaffari     // Implementing in/outflow BCs
39877841947SLeila Ghaffari     if (u_normal > 0) {  // outflow
39977841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
40077841947SLeila Ghaffari     } else {  // inflow
40177841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
40277841947SLeila Ghaffari     }
40377841947SLeila Ghaffari   }  // End Quadrature Point Loop
40477841947SLeila Ghaffari   return 0;
40577841947SLeila Ghaffari }
40677841947SLeila Ghaffari // *****************************************************************************
40777841947SLeila Ghaffari 
40877841947SLeila Ghaffari #endif  // advection_h
409