xref: /libCEED/examples/fluids/qfunctions/advection.h (revision f3e15844553774b7483e8b688026466147268b6c)
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 
178756a6ccSJames Wright #include "utils.h"
188756a6ccSJames Wright 
1997baf651SJames Wright typedef struct SetupContextAdv_ *SetupContextAdv;
2097baf651SJames Wright struct SetupContextAdv_ {
2177841947SLeila Ghaffari   CeedScalar rc;
2277841947SLeila Ghaffari   CeedScalar lx;
2377841947SLeila Ghaffari   CeedScalar ly;
2477841947SLeila Ghaffari   CeedScalar lz;
2577841947SLeila Ghaffari   CeedScalar wind[3];
2677841947SLeila Ghaffari   CeedScalar time;
2777841947SLeila Ghaffari   int        wind_type;               // See WindType: 0=ROTATION, 1=TRANSLATION
2877841947SLeila Ghaffari   int        bubble_type;             // See BubbleType: 0=SPHERE, 1=CYLINDER
2977841947SLeila Ghaffari   int        bubble_continuity_type;  // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK
3077841947SLeila Ghaffari };
3177841947SLeila Ghaffari 
3277841947SLeila Ghaffari typedef struct AdvectionContext_ *AdvectionContext;
3377841947SLeila Ghaffari struct AdvectionContext_ {
3477841947SLeila Ghaffari   CeedScalar CtauS;
3577841947SLeila Ghaffari   CeedScalar strong_form;
3677841947SLeila Ghaffari   CeedScalar E_wind;
3777841947SLeila Ghaffari   bool       implicit;
3877841947SLeila Ghaffari   int        stabilization;  // See StabilizationType: 0=none, 1=SU, 2=SUPG
3977841947SLeila Ghaffari };
4077841947SLeila Ghaffari 
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 // *****************************************************************************
89ea61e9acSJeremy 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];
193*f3e15844SJames Wright   const CeedScalar(*q_data)            = 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]};
219*f3e15844SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
220*f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
22177841947SLeila Ghaffari     // The Physics
22277841947SLeila Ghaffari     // Note with the order that du was filled and the order that dXdx was filled
22377841947SLeila Ghaffari     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k})
22477841947SLeila Ghaffari     //   dXdx[k][j] = dX_K / dx_j
22577841947SLeila Ghaffari     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
22677841947SLeila Ghaffari     //   x_j and u_j are jth  physical position and velocity components
22777841947SLeila Ghaffari 
22877841947SLeila Ghaffari     // No Change in density or momentum
22977841947SLeila Ghaffari     for (CeedInt f = 0; f < 4; f++) {
2302b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
23177841947SLeila Ghaffari       v[f][i] = 0;
23277841947SLeila Ghaffari     }
23377841947SLeila Ghaffari 
23477841947SLeila Ghaffari     // -- Total Energy
23577841947SLeila Ghaffari     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
23677841947SLeila Ghaffari     // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
23777841947SLeila Ghaffari     CeedScalar div_u = 0, u_dot_grad_E = 0;
23877841947SLeila Ghaffari     for (CeedInt j = 0; j < 3; j++) {
23977841947SLeila Ghaffari       CeedScalar dEdx_j = 0;
24077841947SLeila Ghaffari       for (CeedInt k = 0; k < 3; k++) {
24177841947SLeila Ghaffari         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
24277841947SLeila Ghaffari         dEdx_j += dE[k] * dXdx[k][j];
24377841947SLeila Ghaffari       }
24477841947SLeila Ghaffari       u_dot_grad_E += u[j] * dEdx_j;
24577841947SLeila Ghaffari     }
24677841947SLeila Ghaffari     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
24777841947SLeila Ghaffari 
24877841947SLeila Ghaffari     // Weak Galerkin convection term: dv \cdot (E u)
2492b730f8bSJeremy 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]);
25077841947SLeila Ghaffari     v[4][i] = 0;
25177841947SLeila Ghaffari 
25277841947SLeila Ghaffari     // Strong Galerkin convection term: - v div(E u)
25377841947SLeila Ghaffari     v[4][i] = -strong_form * wdetJ * strong_conv;
25477841947SLeila Ghaffari 
25577841947SLeila Ghaffari     // Stabilization requires a measure of element transit time in the velocity
25677841947SLeila Ghaffari     //   field u.
25777841947SLeila Ghaffari     CeedScalar uX[3];
2582b730f8bSJeremy 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];
25977841947SLeila Ghaffari     const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]);
2602b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
26177841947SLeila Ghaffari   }  // End Quadrature Point Loop
26277841947SLeila Ghaffari 
26377841947SLeila Ghaffari   return 0;
26477841947SLeila Ghaffari }
26577841947SLeila Ghaffari 
26677841947SLeila Ghaffari // *****************************************************************************
267ea61e9acSJeremy L Thompson // This QFunction implements 3D (mentioned above) with implicit time stepping method
26877841947SLeila Ghaffari // *****************************************************************************
2692b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
27077841947SLeila Ghaffari   // Inputs
27146603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
27246603fc5SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
27346603fc5SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
274*f3e15844SJames Wright   const CeedScalar(*q_data)            = in[3];
27546603fc5SJames Wright 
27677841947SLeila Ghaffari   // Outputs
27746603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
27846603fc5SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
27946603fc5SJames Wright 
28077841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
28177841947SLeila Ghaffari   const CeedScalar CtauS       = context->CtauS;
28277841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
28377841947SLeila Ghaffari 
28477841947SLeila Ghaffari   // Quadrature Point Loop
28546603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
28677841947SLeila Ghaffari     // Setup
28777841947SLeila Ghaffari     // -- Interp in
28877841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
2892b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
29077841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
29177841947SLeila Ghaffari     // -- Grad in
2922b730f8bSJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
2932b730f8bSJeremy L Thompson     const CeedScalar du[3][3] = {
2942b730f8bSJeremy 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},
2952b730f8bSJeremy 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},
2962b730f8bSJeremy 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}
29777841947SLeila Ghaffari     };
2982b730f8bSJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
299*f3e15844SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
300*f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
30177841947SLeila Ghaffari     // The Physics
30277841947SLeila Ghaffari     // Note with the order that du was filled and the order that dXdx was filled
30377841947SLeila Ghaffari     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k} )
30477841947SLeila Ghaffari     //   dXdx[k][j] = dX_K / dx_j
30577841947SLeila Ghaffari     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
30677841947SLeila Ghaffari     //   x_j and u_j are jth  physical position and velocity components
30777841947SLeila Ghaffari 
30877841947SLeila Ghaffari     // No Change in density or momentum
30977841947SLeila Ghaffari     for (CeedInt f = 0; f < 4; f++) {
3102b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
31177841947SLeila Ghaffari       v[f][i] = wdetJ * q_dot[f][i];  // K Mass/transient term
31277841947SLeila Ghaffari     }
31377841947SLeila Ghaffari 
31477841947SLeila Ghaffari     // -- Total Energy
31577841947SLeila Ghaffari     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
31677841947SLeila Ghaffari     //   or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
31777841947SLeila Ghaffari     CeedScalar div_u = 0, u_dot_grad_E = 0;
31877841947SLeila Ghaffari     for (CeedInt j = 0; j < 3; j++) {
31977841947SLeila Ghaffari       CeedScalar dEdx_j = 0;
32077841947SLeila Ghaffari       for (CeedInt k = 0; k < 3; k++) {
32177841947SLeila Ghaffari         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
32277841947SLeila Ghaffari         dEdx_j += dE[k] * dXdx[k][j];
32377841947SLeila Ghaffari       }
32477841947SLeila Ghaffari       u_dot_grad_E += u[j] * dEdx_j;
32577841947SLeila Ghaffari     }
32677841947SLeila Ghaffari     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
32777841947SLeila Ghaffari     CeedScalar strong_res  = q_dot[4][i] + strong_conv;
32877841947SLeila Ghaffari 
32977841947SLeila Ghaffari     v[4][i] = wdetJ * q_dot[4][i];  // transient part (ALWAYS)
33077841947SLeila Ghaffari 
33177841947SLeila Ghaffari     // Weak Galerkin convection term: -dv \cdot (E u)
3322b730f8bSJeremy 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]);
33377841947SLeila Ghaffari 
33477841947SLeila Ghaffari     // Strong Galerkin convection term: v div(E u)
33577841947SLeila Ghaffari     v[4][i] += wdetJ * strong_form * strong_conv;
33677841947SLeila Ghaffari 
33777841947SLeila Ghaffari     // Stabilization requires a measure of element transit time in the velocity
33877841947SLeila Ghaffari     //   field u.
33977841947SLeila Ghaffari     CeedScalar uX[3];
3402b730f8bSJeremy 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];
34177841947SLeila Ghaffari     const CeedScalar TauS = CtauS / sqrt(uX[0] * uX[0] + uX[1] * uX[1] + uX[2] * uX[2]);
34277841947SLeila Ghaffari 
3432b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) switch (context->stabilization) {
34477841947SLeila Ghaffari         case 0:
34577841947SLeila Ghaffari           break;
3462b730f8bSJeremy L Thompson         case 1:
3472b730f8bSJeremy L Thompson           dv[j][4][i] += wdetJ * TauS * strong_conv * uX[j];  // SU
34877841947SLeila Ghaffari           break;
3492b730f8bSJeremy L Thompson         case 2:
3502b730f8bSJeremy L Thompson           dv[j][4][i] += wdetJ * TauS * strong_res * uX[j];  // SUPG
35177841947SLeila Ghaffari           break;
35277841947SLeila Ghaffari       }
35377841947SLeila Ghaffari   }  // End Quadrature Point Loop
35477841947SLeila Ghaffari 
35577841947SLeila Ghaffari   return 0;
35677841947SLeila Ghaffari }
35777841947SLeila Ghaffari 
35877841947SLeila Ghaffari // *****************************************************************************
35977841947SLeila Ghaffari // This QFunction implements consistent outflow and inflow BCs
36077841947SLeila Ghaffari //      for 3D advection
36177841947SLeila Ghaffari //
36277841947SLeila Ghaffari //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
36377841947SLeila Ghaffari //    sign(dot(wind, normal)) > 0 : outflow BCs
36477841947SLeila Ghaffari //    sign(dot(wind, normal)) < 0 : inflow BCs
36577841947SLeila Ghaffari //
36677841947SLeila Ghaffari //  Outflow BCs:
367ea61e9acSJeremy 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.
36877841947SLeila Ghaffari //
36977841947SLeila Ghaffari //  Inflow BCs:
37077841947SLeila Ghaffari //    A prescribed Total Energy (E_wind) is applied weakly.
37177841947SLeila Ghaffari // *****************************************************************************
3722b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
37377841947SLeila Ghaffari   // Inputs
37446603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
375*f3e15844SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
37646603fc5SJames Wright 
37777841947SLeila Ghaffari   // Outputs
37877841947SLeila Ghaffari   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
37977841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
38077841947SLeila Ghaffari   const CeedScalar E_wind      = context->E_wind;
38177841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
382*f3e15844SJames Wright   const bool       is_implicit = context->implicit;
38377841947SLeila Ghaffari 
38477841947SLeila Ghaffari   // Quadrature Point Loop
38546603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
38677841947SLeila Ghaffari     // Setup
38777841947SLeila Ghaffari     // -- Interp in
38877841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
3892b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
39077841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
39177841947SLeila Ghaffari 
392*f3e15844SJames Wright     CeedScalar wdetJb, norm[3];
393*f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
394*f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
39577841947SLeila Ghaffari 
39677841947SLeila Ghaffari     // Normal velocity
39777841947SLeila Ghaffari     const CeedScalar u_normal = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2];
39877841947SLeila Ghaffari 
39977841947SLeila Ghaffari     // No Change in density or momentum
40077841947SLeila Ghaffari     for (CeedInt j = 0; j < 4; j++) {
40177841947SLeila Ghaffari       v[j][i] = 0;
40277841947SLeila Ghaffari     }
40377841947SLeila Ghaffari     // Implementing in/outflow BCs
40477841947SLeila Ghaffari     if (u_normal > 0) {  // outflow
40577841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
40677841947SLeila Ghaffari     } else {  // inflow
40777841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
40877841947SLeila Ghaffari     }
40977841947SLeila Ghaffari   }  // End Quadrature Point Loop
41077841947SLeila Ghaffari   return 0;
41177841947SLeila Ghaffari }
41277841947SLeila Ghaffari // *****************************************************************************
41377841947SLeila Ghaffari 
41477841947SLeila Ghaffari #endif  // advection_h
415