xref: /libCEED/examples/fluids/qfunctions/advection.h (revision ea61e9ac44808524e4667c1525a05976f536c19c)
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 // *****************************************************************************
89*ea61e9acSJeremy L Thompson // This helper function provides support for the exact, time-dependent solution (currently not implemented) and IC formulation for 3D advection
9077841947SLeila Ghaffari // *****************************************************************************
912b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedInt Exact_Advection(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
9297baf651SJames Wright   const SetupContextAdv context = (SetupContextAdv)ctx;
9377841947SLeila Ghaffari   const CeedScalar      rc      = context->rc;
9477841947SLeila Ghaffari   const CeedScalar      lx      = context->lx;
9577841947SLeila Ghaffari   const CeedScalar      ly      = context->ly;
9677841947SLeila Ghaffari   const CeedScalar      lz      = context->lz;
9777841947SLeila Ghaffari   const CeedScalar     *wind    = context->wind;
9877841947SLeila Ghaffari 
9977841947SLeila Ghaffari   // Setup
10077841947SLeila Ghaffari   const CeedScalar x0[3]     = {0.25 * lx, 0.5 * ly, 0.5 * lz};
10177841947SLeila Ghaffari   const CeedScalar center[3] = {0.5 * lx, 0.5 * ly, 0.5 * lz};
10277841947SLeila Ghaffari 
10377841947SLeila Ghaffari   // -- Coordinates
10477841947SLeila Ghaffari   const CeedScalar x = X[0];
10577841947SLeila Ghaffari   const CeedScalar y = X[1];
10677841947SLeila Ghaffari   const CeedScalar z = X[2];
10777841947SLeila Ghaffari 
10877841947SLeila Ghaffari   // -- Energy
10977841947SLeila Ghaffari   CeedScalar r = 0.;
11077841947SLeila Ghaffari   switch (context->bubble_type) {
11177841947SLeila Ghaffari     //  original sphere
11277841947SLeila Ghaffari     case 0: {  // (dim=3)
1132b730f8bSJeremy L Thompson       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]) + Square(z - x0[2]));
11477841947SLeila Ghaffari     } break;
11577841947SLeila Ghaffari     // cylinder (needs periodicity to work properly)
11677841947SLeila Ghaffari     case 1: {  // (dim=2)
117c32eb7cbSJed Brown       r = sqrt(Square(x - x0[0]) + Square(y - x0[1]));
11877841947SLeila Ghaffari     } break;
11977841947SLeila Ghaffari   }
12077841947SLeila Ghaffari 
12177841947SLeila Ghaffari   // Initial Conditions
12277841947SLeila Ghaffari   switch (context->wind_type) {
12377841947SLeila Ghaffari     case 0:  // Rotation
12477841947SLeila Ghaffari       q[0] = 1.;
12577841947SLeila Ghaffari       q[1] = -(y - center[1]);
12677841947SLeila Ghaffari       q[2] = (x - center[0]);
12777841947SLeila Ghaffari       q[3] = 0;
12877841947SLeila Ghaffari       break;
12977841947SLeila Ghaffari     case 1:  // Translation
13077841947SLeila Ghaffari       q[0] = 1.;
13177841947SLeila Ghaffari       q[1] = wind[0];
13277841947SLeila Ghaffari       q[2] = wind[1];
13377841947SLeila Ghaffari       q[3] = wind[2];
13477841947SLeila Ghaffari       break;
13577841947SLeila Ghaffari   }
13677841947SLeila Ghaffari 
13777841947SLeila Ghaffari   switch (context->bubble_continuity_type) {
13877841947SLeila Ghaffari     // original continuous, smooth shape
13977841947SLeila Ghaffari     case 0: {
14077841947SLeila Ghaffari       q[4] = r <= rc ? (1. - r / rc) : 0.;
14177841947SLeila Ghaffari     } break;
14277841947SLeila Ghaffari     // discontinuous, sharp back half shape
14377841947SLeila Ghaffari     case 1: {
14477841947SLeila Ghaffari       q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) : 0.;
14577841947SLeila Ghaffari     } break;
14677841947SLeila Ghaffari     // attempt to define a finite thickness that will get resolved under grid refinement
14777841947SLeila Ghaffari     case 2: {
1482b730f8bSJeremy L Thompson       q[4] = ((r <= rc) && (y < center[1])) ? (1. - r / rc) * fmin(1.0, (center[1] - y) / 1.25) : 0.;
14977841947SLeila Ghaffari     } break;
15077841947SLeila Ghaffari   }
15177841947SLeila Ghaffari   return 0;
15277841947SLeila Ghaffari }
15377841947SLeila Ghaffari 
15477841947SLeila Ghaffari // *****************************************************************************
15577841947SLeila Ghaffari // This QFunction sets the initial conditions for 3D advection
15677841947SLeila Ghaffari // *****************************************************************************
1572b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
15877841947SLeila Ghaffari   // Inputs
15977841947SLeila Ghaffari   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
16077841947SLeila Ghaffari   // Outputs
16177841947SLeila Ghaffari   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
16277841947SLeila Ghaffari 
16377841947SLeila Ghaffari   // Quadrature Point Loop
16446603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
16577841947SLeila Ghaffari     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
166e6225c47SLeila Ghaffari     CeedScalar       q[5] = {0.};
16777841947SLeila Ghaffari 
16877841947SLeila Ghaffari     Exact_Advection(3, 0., x, 5, q, ctx);
16977841947SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
17077841947SLeila Ghaffari   }  // End of Quadrature Point Loop
17177841947SLeila Ghaffari 
17277841947SLeila Ghaffari   // Return
17377841947SLeila Ghaffari   return 0;
17477841947SLeila Ghaffari }
17577841947SLeila Ghaffari 
17677841947SLeila Ghaffari // *****************************************************************************
17777841947SLeila Ghaffari // This QFunction implements the following formulation of the advection equation
17877841947SLeila Ghaffari //
17977841947SLeila Ghaffari // This is 3D advection given in two formulations based upon the weak form.
18077841947SLeila Ghaffari //
18177841947SLeila Ghaffari // State Variables: q = ( rho, U1, U2, U3, E )
18277841947SLeila Ghaffari //   rho - Mass Density
18377841947SLeila Ghaffari //   Ui  - Momentum Density    ,  Ui = rho ui
18477841947SLeila Ghaffari //   E   - Total Energy Density
18577841947SLeila Ghaffari //
18677841947SLeila Ghaffari // Advection Equation:
18777841947SLeila Ghaffari //   dE/dt + div( E u ) = 0
18877841947SLeila Ghaffari // *****************************************************************************
1892b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
19077841947SLeila Ghaffari   // Inputs
19146603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
19246603fc5SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
19346603fc5SJames Wright   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
19477841947SLeila Ghaffari 
19577841947SLeila Ghaffari   // Outputs
19646603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
19746603fc5SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
19877841947SLeila Ghaffari 
19977841947SLeila Ghaffari   // Context
20077841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
20177841947SLeila Ghaffari   const CeedScalar CtauS       = context->CtauS;
20277841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
20377841947SLeila Ghaffari 
20477841947SLeila Ghaffari   // Quadrature Point Loop
20546603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
20677841947SLeila Ghaffari     // Setup
20777841947SLeila Ghaffari     // -- Interp in
20877841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
2092b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
21077841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
21177841947SLeila Ghaffari     // -- Grad in
2122b730f8bSJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
2132b730f8bSJeremy L Thompson     const CeedScalar du[3][3] = {
2142b730f8bSJeremy 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},
2152b730f8bSJeremy 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},
2162b730f8bSJeremy 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}
21777841947SLeila Ghaffari     };
2182b730f8bSJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
21977841947SLeila Ghaffari     // -- Interp-to-Interp q_data
22077841947SLeila Ghaffari     const CeedScalar wdetJ = q_data[0][i];
22177841947SLeila Ghaffari     // -- Interp-to-Grad q_data
22277841947SLeila Ghaffari     // ---- Inverse of change of coordinate matrix: X_i,j
2232b730f8bSJeremy L Thompson     const CeedScalar dXdx[3][3] = {
2242b730f8bSJeremy L Thompson         {q_data[1][i], q_data[2][i], q_data[3][i]},
2252b730f8bSJeremy L Thompson         {q_data[4][i], q_data[5][i], q_data[6][i]},
2262b730f8bSJeremy L Thompson         {q_data[7][i], q_data[8][i], q_data[9][i]}
22777841947SLeila Ghaffari     };
22877841947SLeila Ghaffari     // The Physics
22977841947SLeila Ghaffari     // Note with the order that du was filled and the order that dXdx was filled
23077841947SLeila Ghaffari     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k})
23177841947SLeila Ghaffari     //   dXdx[k][j] = dX_K / dx_j
23277841947SLeila Ghaffari     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
23377841947SLeila Ghaffari     //   x_j and u_j are jth  physical position and velocity components
23477841947SLeila Ghaffari 
23577841947SLeila Ghaffari     // No Change in density or momentum
23677841947SLeila Ghaffari     for (CeedInt f = 0; f < 4; f++) {
2372b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
23877841947SLeila Ghaffari       v[f][i] = 0;
23977841947SLeila Ghaffari     }
24077841947SLeila Ghaffari 
24177841947SLeila Ghaffari     // -- Total Energy
24277841947SLeila Ghaffari     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
24377841947SLeila Ghaffari     // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
24477841947SLeila Ghaffari     CeedScalar div_u = 0, u_dot_grad_E = 0;
24577841947SLeila Ghaffari     for (CeedInt j = 0; j < 3; j++) {
24677841947SLeila Ghaffari       CeedScalar dEdx_j = 0;
24777841947SLeila Ghaffari       for (CeedInt k = 0; k < 3; k++) {
24877841947SLeila Ghaffari         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
24977841947SLeila Ghaffari         dEdx_j += dE[k] * dXdx[k][j];
25077841947SLeila Ghaffari       }
25177841947SLeila Ghaffari       u_dot_grad_E += u[j] * dEdx_j;
25277841947SLeila Ghaffari     }
25377841947SLeila Ghaffari     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
25477841947SLeila Ghaffari 
25577841947SLeila Ghaffari     // Weak Galerkin convection term: dv \cdot (E u)
2562b730f8bSJeremy 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]);
25777841947SLeila Ghaffari     v[4][i] = 0;
25877841947SLeila Ghaffari 
25977841947SLeila Ghaffari     // Strong Galerkin convection term: - v div(E u)
26077841947SLeila Ghaffari     v[4][i] = -strong_form * wdetJ * strong_conv;
26177841947SLeila Ghaffari 
26277841947SLeila Ghaffari     // Stabilization requires a measure of element transit time in the velocity
26377841947SLeila Ghaffari     //   field u.
26477841947SLeila Ghaffari     CeedScalar uX[3];
2652b730f8bSJeremy 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];
26677841947SLeila Ghaffari     const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]);
2672b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
26877841947SLeila Ghaffari   }  // End Quadrature Point Loop
26977841947SLeila Ghaffari 
27077841947SLeila Ghaffari   return 0;
27177841947SLeila Ghaffari }
27277841947SLeila Ghaffari 
27377841947SLeila Ghaffari // *****************************************************************************
274*ea61e9acSJeremy L Thompson // This QFunction implements 3D (mentioned above) with implicit time stepping method
27577841947SLeila Ghaffari // *****************************************************************************
2762b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
27777841947SLeila Ghaffari   // Inputs
27846603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
27946603fc5SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
28046603fc5SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[2];
28146603fc5SJames Wright   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
28246603fc5SJames Wright 
28377841947SLeila Ghaffari   // Outputs
28446603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
28546603fc5SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
28646603fc5SJames Wright 
28777841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
28877841947SLeila Ghaffari   const CeedScalar CtauS       = context->CtauS;
28977841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
29077841947SLeila Ghaffari 
29177841947SLeila Ghaffari   // Quadrature Point Loop
29246603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
29377841947SLeila Ghaffari     // Setup
29477841947SLeila Ghaffari     // -- Interp in
29577841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
2962b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
29777841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
29877841947SLeila Ghaffari     // -- Grad in
2992b730f8bSJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
3002b730f8bSJeremy L Thompson     const CeedScalar du[3][3] = {
3012b730f8bSJeremy 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},
3022b730f8bSJeremy 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},
3032b730f8bSJeremy 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}
30477841947SLeila Ghaffari     };
3052b730f8bSJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
30677841947SLeila Ghaffari     // -- Interp-to-Interp q_data
30777841947SLeila Ghaffari     const CeedScalar wdetJ = q_data[0][i];
30877841947SLeila Ghaffari     // -- Interp-to-Grad q_data
30977841947SLeila Ghaffari     // ---- Inverse of change of coordinate matrix: X_i,j
3102b730f8bSJeremy L Thompson     const CeedScalar dXdx[3][3] = {
3112b730f8bSJeremy L Thompson         {q_data[1][i], q_data[2][i], q_data[3][i]},
3122b730f8bSJeremy L Thompson         {q_data[4][i], q_data[5][i], q_data[6][i]},
3132b730f8bSJeremy L Thompson         {q_data[7][i], q_data[8][i], q_data[9][i]}
31477841947SLeila Ghaffari     };
31577841947SLeila Ghaffari     // The Physics
31677841947SLeila Ghaffari     // Note with the order that du was filled and the order that dXdx was filled
31777841947SLeila Ghaffari     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k} )
31877841947SLeila Ghaffari     //   dXdx[k][j] = dX_K / dx_j
31977841947SLeila Ghaffari     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
32077841947SLeila Ghaffari     //   x_j and u_j are jth  physical position and velocity components
32177841947SLeila Ghaffari 
32277841947SLeila Ghaffari     // No Change in density or momentum
32377841947SLeila Ghaffari     for (CeedInt f = 0; f < 4; f++) {
3242b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
32577841947SLeila Ghaffari       v[f][i] = wdetJ * q_dot[f][i];  // K Mass/transient term
32677841947SLeila Ghaffari     }
32777841947SLeila Ghaffari 
32877841947SLeila Ghaffari     // -- Total Energy
32977841947SLeila Ghaffari     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
33077841947SLeila Ghaffari     //   or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
33177841947SLeila Ghaffari     CeedScalar div_u = 0, u_dot_grad_E = 0;
33277841947SLeila Ghaffari     for (CeedInt j = 0; j < 3; j++) {
33377841947SLeila Ghaffari       CeedScalar dEdx_j = 0;
33477841947SLeila Ghaffari       for (CeedInt k = 0; k < 3; k++) {
33577841947SLeila Ghaffari         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
33677841947SLeila Ghaffari         dEdx_j += dE[k] * dXdx[k][j];
33777841947SLeila Ghaffari       }
33877841947SLeila Ghaffari       u_dot_grad_E += u[j] * dEdx_j;
33977841947SLeila Ghaffari     }
34077841947SLeila Ghaffari     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
34177841947SLeila Ghaffari     CeedScalar strong_res  = q_dot[4][i] + strong_conv;
34277841947SLeila Ghaffari 
34377841947SLeila Ghaffari     v[4][i] = wdetJ * q_dot[4][i];  // transient part (ALWAYS)
34477841947SLeila Ghaffari 
34577841947SLeila Ghaffari     // Weak Galerkin convection term: -dv \cdot (E u)
3462b730f8bSJeremy 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]);
34777841947SLeila Ghaffari 
34877841947SLeila Ghaffari     // Strong Galerkin convection term: v div(E u)
34977841947SLeila Ghaffari     v[4][i] += wdetJ * strong_form * strong_conv;
35077841947SLeila Ghaffari 
35177841947SLeila Ghaffari     // Stabilization requires a measure of element transit time in the velocity
35277841947SLeila Ghaffari     //   field u.
35377841947SLeila Ghaffari     CeedScalar uX[3];
3542b730f8bSJeremy 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];
35577841947SLeila Ghaffari     const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]);
35677841947SLeila Ghaffari 
3572b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) switch (context->stabilization) {
35877841947SLeila Ghaffari         case 0:
35977841947SLeila Ghaffari           break;
3602b730f8bSJeremy L Thompson         case 1:
3612b730f8bSJeremy L Thompson           dv[j][4][i] += wdetJ * TauS * strong_conv * uX[j];  // SU
36277841947SLeila Ghaffari           break;
3632b730f8bSJeremy L Thompson         case 2:
3642b730f8bSJeremy L Thompson           dv[j][4][i] += wdetJ * TauS * strong_res * uX[j];  // SUPG
36577841947SLeila Ghaffari           break;
36677841947SLeila Ghaffari       }
36777841947SLeila Ghaffari   }  // End Quadrature Point Loop
36877841947SLeila Ghaffari 
36977841947SLeila Ghaffari   return 0;
37077841947SLeila Ghaffari }
37177841947SLeila Ghaffari 
37277841947SLeila Ghaffari // *****************************************************************************
37377841947SLeila Ghaffari // This QFunction implements consistent outflow and inflow BCs
37477841947SLeila Ghaffari //      for 3D advection
37577841947SLeila Ghaffari //
37677841947SLeila Ghaffari //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
37777841947SLeila Ghaffari //    sign(dot(wind, normal)) > 0 : outflow BCs
37877841947SLeila Ghaffari //    sign(dot(wind, normal)) < 0 : inflow BCs
37977841947SLeila Ghaffari //
38077841947SLeila Ghaffari //  Outflow BCs:
381*ea61e9acSJeremy 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.
38277841947SLeila Ghaffari //
38377841947SLeila Ghaffari //  Inflow BCs:
38477841947SLeila Ghaffari //    A prescribed Total Energy (E_wind) is applied weakly.
38577841947SLeila Ghaffari // *****************************************************************************
3862b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
38777841947SLeila Ghaffari   // Inputs
38846603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0];
38946603fc5SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
39046603fc5SJames Wright 
39177841947SLeila Ghaffari   // Outputs
39277841947SLeila Ghaffari   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
39377841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
39477841947SLeila Ghaffari   const CeedScalar E_wind      = context->E_wind;
39577841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
39677841947SLeila Ghaffari   const bool       implicit    = context->implicit;
39777841947SLeila Ghaffari 
39877841947SLeila Ghaffari   // Quadrature Point Loop
39946603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
40077841947SLeila Ghaffari     // Setup
40177841947SLeila Ghaffari     // -- Interp in
40277841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
4032b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
40477841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
40577841947SLeila Ghaffari 
40677841947SLeila Ghaffari     // -- Interp-to-Interp q_data
40777841947SLeila Ghaffari     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
40877841947SLeila Ghaffari     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
40977841947SLeila Ghaffari     // We can effect this by swapping the sign on this weight
41077841947SLeila Ghaffari     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
41177841947SLeila Ghaffari 
41277841947SLeila Ghaffari     // ---- Normal vectors
4132b730f8bSJeremy L Thompson     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
41477841947SLeila Ghaffari     // Normal velocity
41577841947SLeila Ghaffari     const CeedScalar u_normal = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2];
41677841947SLeila Ghaffari 
41777841947SLeila Ghaffari     // No Change in density or momentum
41877841947SLeila Ghaffari     for (CeedInt j = 0; j < 4; j++) {
41977841947SLeila Ghaffari       v[j][i] = 0;
42077841947SLeila Ghaffari     }
42177841947SLeila Ghaffari     // Implementing in/outflow BCs
42277841947SLeila Ghaffari     if (u_normal > 0) {  // outflow
42377841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
42477841947SLeila Ghaffari     } else {  // inflow
42577841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
42677841947SLeila Ghaffari     }
42777841947SLeila Ghaffari   }  // End Quadrature Point Loop
42877841947SLeila Ghaffari   return 0;
42977841947SLeila Ghaffari }
43077841947SLeila Ghaffari // *****************************************************************************
43177841947SLeila Ghaffari 
43277841947SLeila Ghaffari #endif  // advection_h
433