xref: /libCEED/examples/fluids/qfunctions/advection.h (revision 46603fc57e28d79cde01b07e9ca450b5fd78aed4)
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 
1797baf651SJames Wright typedef struct SetupContextAdv_ *SetupContextAdv;
1897baf651SJames Wright struct SetupContextAdv_ {
1977841947SLeila Ghaffari   CeedScalar rc;
2077841947SLeila Ghaffari   CeedScalar lx;
2177841947SLeila Ghaffari   CeedScalar ly;
2277841947SLeila Ghaffari   CeedScalar lz;
2377841947SLeila Ghaffari   CeedScalar wind[3];
2477841947SLeila Ghaffari   CeedScalar time;
2577841947SLeila Ghaffari   int        wind_type;               // See WindType: 0=ROTATION, 1=TRANSLATION
2677841947SLeila Ghaffari   int        bubble_type;             // See BubbleType: 0=SPHERE, 1=CYLINDER
2777841947SLeila Ghaffari   int        bubble_continuity_type;  // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK
2877841947SLeila Ghaffari };
2977841947SLeila Ghaffari 
3077841947SLeila Ghaffari typedef struct AdvectionContext_ *AdvectionContext;
3177841947SLeila Ghaffari struct AdvectionContext_ {
3277841947SLeila Ghaffari   CeedScalar CtauS;
3377841947SLeila Ghaffari   CeedScalar strong_form;
3477841947SLeila Ghaffari   CeedScalar E_wind;
3577841947SLeila Ghaffari   bool       implicit;
3677841947SLeila Ghaffari   int        stabilization;  // See StabilizationType: 0=none, 1=SU, 2=SUPG
3777841947SLeila Ghaffari };
3877841947SLeila Ghaffari 
39c32eb7cbSJed Brown CEED_QFUNCTION_HELPER CeedScalar Square(CeedScalar x) { return x * x; }
40c32eb7cbSJed Brown 
4177841947SLeila Ghaffari // *****************************************************************************
4277841947SLeila Ghaffari // This QFunction sets the initial conditions and the boundary conditions
4377841947SLeila Ghaffari //   for two test cases: ROTATION and TRANSLATION
4477841947SLeila Ghaffari //
4577841947SLeila Ghaffari // -- ROTATION (default)
4677841947SLeila Ghaffari //      Initial Conditions:
4777841947SLeila Ghaffari //        Mass Density:
4877841947SLeila Ghaffari //          Constant mass density of 1.0
4977841947SLeila Ghaffari //        Momentum Density:
5077841947SLeila Ghaffari //          Rotational field in x,y
5177841947SLeila Ghaffari //        Energy Density:
5277841947SLeila Ghaffari //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
5377841947SLeila Ghaffari //            increases to (1.-r/rc), then 0. everywhere else
5477841947SLeila Ghaffari //
5577841947SLeila Ghaffari //      Boundary Conditions:
5677841947SLeila Ghaffari //        Mass Density:
5777841947SLeila Ghaffari //          0.0 flux
5877841947SLeila Ghaffari //        Momentum Density:
5977841947SLeila Ghaffari //          0.0
6077841947SLeila Ghaffari //        Energy Density:
6177841947SLeila Ghaffari //          0.0 flux
6277841947SLeila Ghaffari //
6377841947SLeila Ghaffari // -- TRANSLATION
6477841947SLeila Ghaffari //      Initial Conditions:
6577841947SLeila Ghaffari //        Mass Density:
6677841947SLeila Ghaffari //          Constant mass density of 1.0
6777841947SLeila Ghaffari //        Momentum Density:
6877841947SLeila Ghaffari //           Constant rectilinear field in x,y
6977841947SLeila Ghaffari //        Energy Density:
7077841947SLeila Ghaffari //          Maximum of 1. x0 decreasing linearly to 0. as radial distance
7177841947SLeila Ghaffari //            increases to (1.-r/rc), then 0. everywhere else
7277841947SLeila Ghaffari //
7377841947SLeila Ghaffari //      Boundary Conditions:
7477841947SLeila Ghaffari //        Mass Density:
7577841947SLeila Ghaffari //          0.0 flux
7677841947SLeila Ghaffari //        Momentum Density:
7777841947SLeila Ghaffari //          0.0
7877841947SLeila Ghaffari //        Energy Density:
7977841947SLeila Ghaffari //          Inflow BCs:
8077841947SLeila Ghaffari //            E = E_wind
8177841947SLeila Ghaffari //          Outflow BCs:
8277841947SLeila Ghaffari //            E = E(boundary)
8377841947SLeila Ghaffari //          Both In/Outflow BCs for E are applied weakly in the
8477841947SLeila Ghaffari //            QFunction "Advection_Sur"
8577841947SLeila Ghaffari //
8677841947SLeila Ghaffari // *****************************************************************************
8777841947SLeila Ghaffari 
8877841947SLeila Ghaffari // *****************************************************************************
8977841947SLeila Ghaffari // This helper function provides support for the exact, time-dependent solution
9077841947SLeila Ghaffari //   (currently not implemented) and IC formulation for 3D advection
9177841947SLeila Ghaffari // *****************************************************************************
922b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedInt Exact_Advection(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
9397baf651SJames Wright   const SetupContextAdv context = (SetupContextAdv)ctx;
9477841947SLeila Ghaffari   const CeedScalar      rc      = context->rc;
9577841947SLeila Ghaffari   const CeedScalar      lx      = context->lx;
9677841947SLeila Ghaffari   const CeedScalar      ly      = context->ly;
9777841947SLeila Ghaffari   const CeedScalar      lz      = context->lz;
9877841947SLeila Ghaffari   const CeedScalar     *wind    = context->wind;
9977841947SLeila Ghaffari 
10077841947SLeila Ghaffari   // Setup
10177841947SLeila Ghaffari   const CeedScalar x0[3]     = {0.25 * lx, 0.5 * ly, 0.5 * lz};
10277841947SLeila Ghaffari   const CeedScalar center[3] = {0.5 * lx, 0.5 * ly, 0.5 * lz};
10377841947SLeila Ghaffari 
10477841947SLeila Ghaffari   // -- Coordinates
10577841947SLeila Ghaffari   const CeedScalar x = X[0];
10677841947SLeila Ghaffari   const CeedScalar y = X[1];
10777841947SLeila Ghaffari   const CeedScalar z = X[2];
10877841947SLeila Ghaffari 
10977841947SLeila Ghaffari   // -- Energy
11077841947SLeila Ghaffari   CeedScalar r = 0.;
11177841947SLeila Ghaffari   switch (context->bubble_type) {
11277841947SLeila Ghaffari     //  original sphere
11377841947SLeila Ghaffari     case 0: {  // (dim=3)
1142b730f8bSJeremy L Thompson       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]) + Square(z - x0[2]));
11577841947SLeila Ghaffari     } break;
11677841947SLeila Ghaffari     // cylinder (needs periodicity to work properly)
11777841947SLeila Ghaffari     case 1: {  // (dim=2)
118c32eb7cbSJed Brown       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]));
11977841947SLeila Ghaffari     } break;
12077841947SLeila Ghaffari   }
12177841947SLeila Ghaffari 
12277841947SLeila Ghaffari   // Initial Conditions
12377841947SLeila Ghaffari   switch (context->wind_type) {
12477841947SLeila Ghaffari     case 0:  // Rotation
12577841947SLeila Ghaffari       q[0] = 1.;
12677841947SLeila Ghaffari       q[1] = -(y - center[1]);
12777841947SLeila Ghaffari       q[2] = (x - center[0]);
12877841947SLeila Ghaffari       q[3] = 0;
12977841947SLeila Ghaffari       break;
13077841947SLeila Ghaffari     case 1:  // Translation
13177841947SLeila Ghaffari       q[0] = 1.;
13277841947SLeila Ghaffari       q[1] = wind[0];
13377841947SLeila Ghaffari       q[2] = wind[1];
13477841947SLeila Ghaffari       q[3] = wind[2];
13577841947SLeila Ghaffari       break;
13677841947SLeila Ghaffari   }
13777841947SLeila Ghaffari 
13877841947SLeila Ghaffari   switch (context->bubble_continuity_type) {
13977841947SLeila Ghaffari     // original continuous, smooth shape
14077841947SLeila Ghaffari     case 0: {
14177841947SLeila Ghaffari       q[4] = r <= rc ? (1. - r / rc) : 0.;
14277841947SLeila Ghaffari     } break;
14377841947SLeila Ghaffari     // discontinuous, sharp back half shape
14477841947SLeila Ghaffari     case 1: {
14577841947SLeila Ghaffari       q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) : 0.;
14677841947SLeila Ghaffari     } break;
14777841947SLeila Ghaffari     // attempt to define a finite thickness that will get resolved under grid refinement
14877841947SLeila Ghaffari     case 2: {
1492b730f8bSJeremy L Thompson       q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) * fmin(1.0, (center[1] - y) / 1.25) : 0.;
15077841947SLeila Ghaffari     } break;
15177841947SLeila Ghaffari   }
15277841947SLeila Ghaffari   return 0;
15377841947SLeila Ghaffari }
15477841947SLeila Ghaffari 
15577841947SLeila Ghaffari // *****************************************************************************
15677841947SLeila Ghaffari // This QFunction sets the initial conditions for 3D advection
15777841947SLeila Ghaffari // *****************************************************************************
1582b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
15977841947SLeila Ghaffari   // Inputs
16077841947SLeila Ghaffari   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
16177841947SLeila Ghaffari   // Outputs
16277841947SLeila Ghaffari   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
16377841947SLeila Ghaffari 
16477841947SLeila Ghaffari   // Quadrature Point Loop
165*46603fc5SJames 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 
16977841947SLeila Ghaffari     Exact_Advection(3, 0., x, 5, q, ctx);
17077841947SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
17177841947SLeila Ghaffari   }  // End of Quadrature Point Loop
17277841947SLeila Ghaffari 
17377841947SLeila Ghaffari   // Return
17477841947SLeila Ghaffari   return 0;
17577841947SLeila Ghaffari }
17677841947SLeila Ghaffari 
17777841947SLeila Ghaffari // *****************************************************************************
17877841947SLeila Ghaffari // This QFunction implements the following formulation of the advection equation
17977841947SLeila Ghaffari //
18077841947SLeila Ghaffari // This is 3D advection given in two formulations based upon the weak form.
18177841947SLeila Ghaffari //
18277841947SLeila Ghaffari // State Variables: q = ( rho, U1, U2, U3, E )
18377841947SLeila Ghaffari //   rho - Mass Density
18477841947SLeila Ghaffari //   Ui  - Momentum Density    ,  Ui = rho ui
18577841947SLeila Ghaffari //   E   - Total Energy Density
18677841947SLeila Ghaffari //
18777841947SLeila Ghaffari // Advection Equation:
18877841947SLeila Ghaffari //   dE/dt + div( E u ) = 0
18977841947SLeila Ghaffari //
19077841947SLeila Ghaffari // *****************************************************************************
1912b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
19277841947SLeila Ghaffari   // Inputs
193*46603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
194*46603fc5SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
195*46603fc5SJames Wright   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
19677841947SLeila Ghaffari 
19777841947SLeila Ghaffari   // Outputs
198*46603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
199*46603fc5SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
20077841947SLeila Ghaffari 
20177841947SLeila Ghaffari   // Context
20277841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
20377841947SLeila Ghaffari   const CeedScalar CtauS       = context->CtauS;
20477841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
20577841947SLeila Ghaffari 
20677841947SLeila Ghaffari   // Quadrature Point Loop
207*46603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
20877841947SLeila Ghaffari     // Setup
20977841947SLeila Ghaffari     // -- Interp in
21077841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
2112b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
21277841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
21377841947SLeila Ghaffari     // -- Grad in
2142b730f8bSJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
2152b730f8bSJeremy L Thompson     const CeedScalar du[3][3] = {
2162b730f8bSJeremy 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},
2172b730f8bSJeremy 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},
2182b730f8bSJeremy 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}
21977841947SLeila Ghaffari     };
2202b730f8bSJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
22177841947SLeila Ghaffari     // -- Interp-to-Interp q_data
22277841947SLeila Ghaffari     const CeedScalar wdetJ = q_data[0][i];
22377841947SLeila Ghaffari     // -- Interp-to-Grad q_data
22477841947SLeila Ghaffari     // ---- Inverse of change of coordinate matrix: X_i,j
2252b730f8bSJeremy L Thompson     const CeedScalar dXdx[3][3] = {
2262b730f8bSJeremy L Thompson         {q_data[1][i], q_data[2][i], q_data[3][i]},
2272b730f8bSJeremy L Thompson         {q_data[4][i], q_data[5][i], q_data[6][i]},
2282b730f8bSJeremy L Thompson         {q_data[7][i], q_data[8][i], q_data[9][i]}
22977841947SLeila Ghaffari     };
23077841947SLeila Ghaffari     // The Physics
23177841947SLeila Ghaffari     // Note with the order that du was filled and the order that dXdx was filled
23277841947SLeila Ghaffari     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k})
23377841947SLeila Ghaffari     //   dXdx[k][j] = dX_K / dx_j
23477841947SLeila Ghaffari     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
23577841947SLeila Ghaffari     //   x_j and u_j are jth  physical position and velocity components
23677841947SLeila Ghaffari 
23777841947SLeila Ghaffari     // No Change in density or momentum
23877841947SLeila Ghaffari     for (CeedInt f = 0; f < 4; f++) {
2392b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
24077841947SLeila Ghaffari       v[f][i] = 0;
24177841947SLeila Ghaffari     }
24277841947SLeila Ghaffari 
24377841947SLeila Ghaffari     // -- Total Energy
24477841947SLeila Ghaffari     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
24577841947SLeila Ghaffari     // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
24677841947SLeila Ghaffari     CeedScalar div_u = 0, u_dot_grad_E = 0;
24777841947SLeila Ghaffari     for (CeedInt j = 0; j < 3; j++) {
24877841947SLeila Ghaffari       CeedScalar dEdx_j = 0;
24977841947SLeila Ghaffari       for (CeedInt k = 0; k < 3; k++) {
25077841947SLeila Ghaffari         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
25177841947SLeila Ghaffari         dEdx_j += dE[k] * dXdx[k][j];
25277841947SLeila Ghaffari       }
25377841947SLeila Ghaffari       u_dot_grad_E += u[j] * dEdx_j;
25477841947SLeila Ghaffari     }
25577841947SLeila Ghaffari     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
25677841947SLeila Ghaffari 
25777841947SLeila Ghaffari     // Weak Galerkin convection term: dv \cdot (E u)
2582b730f8bSJeremy 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]);
25977841947SLeila Ghaffari     v[4][i] = 0;
26077841947SLeila Ghaffari 
26177841947SLeila Ghaffari     // Strong Galerkin convection term: - v div(E u)
26277841947SLeila Ghaffari     v[4][i] = -strong_form * wdetJ * strong_conv;
26377841947SLeila Ghaffari 
26477841947SLeila Ghaffari     // Stabilization requires a measure of element transit time in the velocity
26577841947SLeila Ghaffari     //   field u.
26677841947SLeila Ghaffari     CeedScalar uX[3];
2672b730f8bSJeremy 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];
26877841947SLeila Ghaffari     const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]);
2692b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
27077841947SLeila Ghaffari   }  // End Quadrature Point Loop
27177841947SLeila Ghaffari 
27277841947SLeila Ghaffari   return 0;
27377841947SLeila Ghaffari }
27477841947SLeila Ghaffari 
27577841947SLeila Ghaffari // *****************************************************************************
27677841947SLeila Ghaffari // This QFunction implements 3D (mentioned above) with
27777841947SLeila Ghaffari //   implicit time stepping method
27877841947SLeila Ghaffari //
27977841947SLeila Ghaffari // *****************************************************************************
2802b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
28177841947SLeila Ghaffari   // Inputs
282*46603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
283*46603fc5SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
284*46603fc5SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[2];
285*46603fc5SJames Wright   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
286*46603fc5SJames Wright 
28777841947SLeila Ghaffari   // Outputs
288*46603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
289*46603fc5SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
290*46603fc5SJames Wright 
29177841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
29277841947SLeila Ghaffari   const CeedScalar CtauS       = context->CtauS;
29377841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
29477841947SLeila Ghaffari 
29577841947SLeila Ghaffari   // Quadrature Point Loop
296*46603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
29777841947SLeila Ghaffari     // Setup
29877841947SLeila Ghaffari     // -- Interp in
29977841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
3002b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
30177841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
30277841947SLeila Ghaffari     // -- Grad in
3032b730f8bSJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
3042b730f8bSJeremy L Thompson     const CeedScalar du[3][3] = {
3052b730f8bSJeremy 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},
3062b730f8bSJeremy 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},
3072b730f8bSJeremy 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}
30877841947SLeila Ghaffari     };
3092b730f8bSJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
31077841947SLeila Ghaffari     // -- Interp-to-Interp q_data
31177841947SLeila Ghaffari     const CeedScalar wdetJ = q_data[0][i];
31277841947SLeila Ghaffari     // -- Interp-to-Grad q_data
31377841947SLeila Ghaffari     // ---- Inverse of change of coordinate matrix: X_i,j
3142b730f8bSJeremy L Thompson     const CeedScalar dXdx[3][3] = {
3152b730f8bSJeremy L Thompson         {q_data[1][i], q_data[2][i], q_data[3][i]},
3162b730f8bSJeremy L Thompson         {q_data[4][i], q_data[5][i], q_data[6][i]},
3172b730f8bSJeremy L Thompson         {q_data[7][i], q_data[8][i], q_data[9][i]}
31877841947SLeila Ghaffari     };
31977841947SLeila Ghaffari     // The Physics
32077841947SLeila Ghaffari     // Note with the order that du was filled and the order that dXdx was filled
32177841947SLeila Ghaffari     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k} )
32277841947SLeila Ghaffari     //   dXdx[k][j] = dX_K / dx_j
32377841947SLeila Ghaffari     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
32477841947SLeila Ghaffari     //   x_j and u_j are jth  physical position and velocity components
32577841947SLeila Ghaffari 
32677841947SLeila Ghaffari     // No Change in density or momentum
32777841947SLeila Ghaffari     for (CeedInt f = 0; f < 4; f++) {
3282b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
32977841947SLeila Ghaffari       v[f][i] = wdetJ * q_dot[f][i];  // K Mass/transient term
33077841947SLeila Ghaffari     }
33177841947SLeila Ghaffari 
33277841947SLeila Ghaffari     // -- Total Energy
33377841947SLeila Ghaffari     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
33477841947SLeila Ghaffari     //   or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
33577841947SLeila Ghaffari     CeedScalar div_u = 0, u_dot_grad_E = 0;
33677841947SLeila Ghaffari     for (CeedInt j = 0; j < 3; j++) {
33777841947SLeila Ghaffari       CeedScalar dEdx_j = 0;
33877841947SLeila Ghaffari       for (CeedInt k = 0; k < 3; k++) {
33977841947SLeila Ghaffari         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
34077841947SLeila Ghaffari         dEdx_j += dE[k] * dXdx[k][j];
34177841947SLeila Ghaffari       }
34277841947SLeila Ghaffari       u_dot_grad_E += u[j] * dEdx_j;
34377841947SLeila Ghaffari     }
34477841947SLeila Ghaffari     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
34577841947SLeila Ghaffari     CeedScalar strong_res  = q_dot[4][i] + strong_conv;
34677841947SLeila Ghaffari 
34777841947SLeila Ghaffari     v[4][i] = wdetJ * q_dot[4][i];  // transient part (ALWAYS)
34877841947SLeila Ghaffari 
34977841947SLeila Ghaffari     // Weak Galerkin convection term: -dv \cdot (E u)
3502b730f8bSJeremy 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]);
35177841947SLeila Ghaffari 
35277841947SLeila Ghaffari     // Strong Galerkin convection term: v div(E u)
35377841947SLeila Ghaffari     v[4][i] += wdetJ * strong_form * strong_conv;
35477841947SLeila Ghaffari 
35577841947SLeila Ghaffari     // Stabilization requires a measure of element transit time in the velocity
35677841947SLeila Ghaffari     //   field u.
35777841947SLeila Ghaffari     CeedScalar uX[3];
3582b730f8bSJeremy 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];
35977841947SLeila Ghaffari     const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]);
36077841947SLeila Ghaffari 
3612b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) switch (context->stabilization) {
36277841947SLeila Ghaffari         case 0:
36377841947SLeila Ghaffari           break;
3642b730f8bSJeremy L Thompson         case 1:
3652b730f8bSJeremy L Thompson           dv[j][4][i] += wdetJ * TauS * strong_conv * uX[j];  // SU
36677841947SLeila Ghaffari           break;
3672b730f8bSJeremy L Thompson         case 2:
3682b730f8bSJeremy L Thompson           dv[j][4][i] += wdetJ * TauS * strong_res * uX[j];  // SUPG
36977841947SLeila Ghaffari           break;
37077841947SLeila Ghaffari       }
37177841947SLeila Ghaffari   }  // End Quadrature Point Loop
37277841947SLeila Ghaffari 
37377841947SLeila Ghaffari   return 0;
37477841947SLeila Ghaffari }
37577841947SLeila Ghaffari 
37677841947SLeila Ghaffari // *****************************************************************************
37777841947SLeila Ghaffari // This QFunction implements consistent outflow and inflow BCs
37877841947SLeila Ghaffari //      for 3D advection
37977841947SLeila Ghaffari //
38077841947SLeila Ghaffari //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
38177841947SLeila Ghaffari //    sign(dot(wind, normal)) > 0 : outflow BCs
38277841947SLeila Ghaffari //    sign(dot(wind, normal)) < 0 : inflow BCs
38377841947SLeila Ghaffari //
38477841947SLeila Ghaffari //  Outflow BCs:
38577841947SLeila Ghaffari //    The validity of the weak form of the governing equations is extended
38677841947SLeila Ghaffari //    to the outflow and the current values of E are applied.
38777841947SLeila Ghaffari //
38877841947SLeila Ghaffari //  Inflow BCs:
38977841947SLeila Ghaffari //    A prescribed Total Energy (E_wind) is applied weakly.
39077841947SLeila Ghaffari //
39177841947SLeila Ghaffari // *****************************************************************************
3922b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
39377841947SLeila Ghaffari   // Inputs
394*46603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0];
395*46603fc5SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
396*46603fc5SJames Wright 
39777841947SLeila Ghaffari   // Outputs
39877841947SLeila Ghaffari   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
39977841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
40077841947SLeila Ghaffari   const CeedScalar E_wind      = context->E_wind;
40177841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
40277841947SLeila Ghaffari   const bool       implicit    = context->implicit;
40377841947SLeila Ghaffari 
40477841947SLeila Ghaffari   // Quadrature Point Loop
405*46603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
40677841947SLeila Ghaffari     // Setup
40777841947SLeila Ghaffari     // -- Interp in
40877841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
4092b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
41077841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
41177841947SLeila Ghaffari 
41277841947SLeila Ghaffari     // -- Interp-to-Interp q_data
41377841947SLeila Ghaffari     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
41477841947SLeila Ghaffari     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
41577841947SLeila Ghaffari     // We can effect this by swapping the sign on this weight
41677841947SLeila Ghaffari     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
41777841947SLeila Ghaffari 
41877841947SLeila Ghaffari     // ---- Normal vectors
4192b730f8bSJeremy L Thompson     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
42077841947SLeila Ghaffari     // Normal velocity
42177841947SLeila Ghaffari     const CeedScalar u_normal = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2];
42277841947SLeila Ghaffari 
42377841947SLeila Ghaffari     // No Change in density or momentum
42477841947SLeila Ghaffari     for (CeedInt j = 0; j < 4; j++) {
42577841947SLeila Ghaffari       v[j][i] = 0;
42677841947SLeila Ghaffari     }
42777841947SLeila Ghaffari     // Implementing in/outflow BCs
42877841947SLeila Ghaffari     if (u_normal > 0) {  // outflow
42977841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
43077841947SLeila Ghaffari     } else {  // inflow
43177841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
43277841947SLeila Ghaffari     }
43377841947SLeila Ghaffari   }  // End Quadrature Point Loop
43477841947SLeila Ghaffari   return 0;
43577841947SLeila Ghaffari }
43677841947SLeila Ghaffari // *****************************************************************************
43777841947SLeila Ghaffari 
43877841947SLeila Ghaffari #endif  // advection_h
439