xref: /libCEED/examples/fluids/qfunctions/advection.h (revision 372d1924747546c3b4612fc695542b8d0e960f39)
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 
1730e1b2c7SJames Wright #include "advection_generic.h"
18c44b1c7dSJames Wright #include "advection_types.h"
198f4d89c8SJames Wright #include "newtonian_state.h"
208f4d89c8SJames Wright #include "newtonian_types.h"
21c44b1c7dSJames Wright #include "stabilization_types.h"
228756a6ccSJames Wright #include "utils.h"
238756a6ccSJames Wright 
2477841947SLeila Ghaffari // *****************************************************************************
2577841947SLeila Ghaffari // This QFunction sets the initial conditions for 3D advection
2677841947SLeila Ghaffari // *****************************************************************************
272b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsAdvection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2877841947SLeila Ghaffari   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2977841947SLeila Ghaffari   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
3077841947SLeila Ghaffari 
3146603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
3277841947SLeila Ghaffari     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
33e6225c47SLeila Ghaffari     CeedScalar       q[5] = {0.};
3477841947SLeila Ghaffari 
3530e1b2c7SJames Wright     Exact_AdvectionGeneric(3, 0., x, 5, q, ctx);
3677841947SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
3730e1b2c7SJames Wright   }
3877841947SLeila Ghaffari   return 0;
3977841947SLeila Ghaffari }
4077841947SLeila Ghaffari 
4177841947SLeila Ghaffari // *****************************************************************************
4277841947SLeila Ghaffari // This QFunction implements the following formulation of the advection equation
4377841947SLeila Ghaffari //
4477841947SLeila Ghaffari // This is 3D advection given in two formulations based upon the weak form.
4577841947SLeila Ghaffari //
4677841947SLeila Ghaffari // State Variables: q = ( rho, U1, U2, U3, E )
4777841947SLeila Ghaffari //   rho - Mass Density
4877841947SLeila Ghaffari //   Ui  - Momentum Density    ,  Ui = rho ui
4977841947SLeila Ghaffari //   E   - Total Energy Density
5077841947SLeila Ghaffari //
5177841947SLeila Ghaffari // Advection Equation:
5277841947SLeila Ghaffari //   dE/dt + div( E u ) = 0
5377841947SLeila Ghaffari // *****************************************************************************
542b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
5577841947SLeila Ghaffari   // Inputs
5646603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
5746603fc5SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
58f3e15844SJames Wright   const CeedScalar(*q_data)            = in[2];
5977841947SLeila Ghaffari 
6077841947SLeila Ghaffari   // Outputs
6146603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
6246603fc5SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
6377841947SLeila Ghaffari 
6477841947SLeila Ghaffari   // Context
6577841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
6677841947SLeila Ghaffari   const CeedScalar CtauS       = context->CtauS;
6777841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
6877841947SLeila Ghaffari 
6977841947SLeila Ghaffari   // Quadrature Point Loop
7046603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
7177841947SLeila Ghaffari     // Setup
7277841947SLeila Ghaffari     // -- Interp in
7377841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
742b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
7577841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
7677841947SLeila Ghaffari     // -- Grad in
772b730f8bSJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
782b730f8bSJeremy L Thompson     const CeedScalar du[3][3] = {
792b730f8bSJeremy 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},
802b730f8bSJeremy 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},
812b730f8bSJeremy 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}
8277841947SLeila Ghaffari     };
832b730f8bSJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
84f3e15844SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
85f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
8677841947SLeila Ghaffari     // The Physics
8777841947SLeila Ghaffari     // Note with the order that du was filled and the order that dXdx was filled
8877841947SLeila Ghaffari     //   du[j][k]= du_j / dX_K    (note cap K to be clear this is u_{j,xi_k})
8977841947SLeila Ghaffari     //   dXdx[k][j] = dX_K / dx_j
9077841947SLeila Ghaffari     //   X_K=Kth reference element coordinate (note cap X and K instead of xi_k}
9177841947SLeila Ghaffari     //   x_j and u_j are jth  physical position and velocity components
9277841947SLeila Ghaffari 
9377841947SLeila Ghaffari     // No Change in density or momentum
9477841947SLeila Ghaffari     for (CeedInt f = 0; f < 4; f++) {
952b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][f][i] = 0;
9677841947SLeila Ghaffari       v[f][i] = 0;
9777841947SLeila Ghaffari     }
9877841947SLeila Ghaffari 
9977841947SLeila Ghaffari     // -- Total Energy
10077841947SLeila Ghaffari     // Evaluate the strong form using div(E u) = u . grad(E) + E div(u)
10177841947SLeila Ghaffari     // or in index notation: (u_j E)_{,j} = u_j E_j + E u_{j,j}
10277841947SLeila Ghaffari     CeedScalar div_u = 0, u_dot_grad_E = 0;
10377841947SLeila Ghaffari     for (CeedInt j = 0; j < 3; j++) {
10477841947SLeila Ghaffari       CeedScalar dEdx_j = 0;
10577841947SLeila Ghaffari       for (CeedInt k = 0; k < 3; k++) {
10677841947SLeila Ghaffari         div_u += du[j][k] * dXdx[k][j];  // u_{j,j} = u_{j,K} X_{K,j}
10777841947SLeila Ghaffari         dEdx_j += dE[k] * dXdx[k][j];
10877841947SLeila Ghaffari       }
10977841947SLeila Ghaffari       u_dot_grad_E += u[j] * dEdx_j;
11077841947SLeila Ghaffari     }
11177841947SLeila Ghaffari     CeedScalar strong_conv = E * div_u + u_dot_grad_E;
11277841947SLeila Ghaffari 
11377841947SLeila Ghaffari     // Weak Galerkin convection term: dv \cdot (E u)
1142b730f8bSJeremy 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]);
11577841947SLeila Ghaffari     v[4][i] = 0;
11677841947SLeila Ghaffari 
11777841947SLeila Ghaffari     // Strong Galerkin convection term: - v div(E u)
11877841947SLeila Ghaffari     v[4][i] = -strong_form * wdetJ * strong_conv;
11977841947SLeila Ghaffari 
12077841947SLeila Ghaffari     // Stabilization requires a measure of element transit time in the velocity
12177841947SLeila Ghaffari     //   field u.
12277841947SLeila Ghaffari     CeedScalar uX[3];
1232b730f8bSJeremy 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];
1244bd6ffc9SJames Wright     const CeedScalar TauS = CtauS / sqrt(Dot3(uX, uX));
1252b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * TauS * strong_conv * uX[j];
12677841947SLeila Ghaffari   }  // End Quadrature Point Loop
12777841947SLeila Ghaffari 
12877841947SLeila Ghaffari   return 0;
12977841947SLeila Ghaffari }
13077841947SLeila Ghaffari 
1312b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
132*372d1924SJames Wright   IFunction_AdvectionGeneric(ctx, Q, in, out, 3);
13377841947SLeila Ghaffari   return 0;
13477841947SLeila Ghaffari }
13577841947SLeila Ghaffari 
13677841947SLeila Ghaffari // *****************************************************************************
13777841947SLeila Ghaffari // This QFunction implements consistent outflow and inflow BCs
13877841947SLeila Ghaffari //      for 3D advection
13977841947SLeila Ghaffari //
14077841947SLeila Ghaffari //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
14177841947SLeila Ghaffari //    sign(dot(wind, normal)) > 0 : outflow BCs
14277841947SLeila Ghaffari //    sign(dot(wind, normal)) < 0 : inflow BCs
14377841947SLeila Ghaffari //
14477841947SLeila Ghaffari //  Outflow BCs:
145ea61e9acSJeremy 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.
14677841947SLeila Ghaffari //
14777841947SLeila Ghaffari //  Inflow BCs:
14877841947SLeila Ghaffari //    A prescribed Total Energy (E_wind) is applied weakly.
14977841947SLeila Ghaffari // *****************************************************************************
1502b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
15177841947SLeila Ghaffari   // Inputs
15246603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
153f3e15844SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
15446603fc5SJames Wright 
15577841947SLeila Ghaffari   // Outputs
15677841947SLeila Ghaffari   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
15777841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
15877841947SLeila Ghaffari   const CeedScalar E_wind      = context->E_wind;
15977841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
160f3e15844SJames Wright   const bool       is_implicit = context->implicit;
16177841947SLeila Ghaffari 
16277841947SLeila Ghaffari   // Quadrature Point Loop
16346603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
16477841947SLeila Ghaffari     // Setup
16577841947SLeila Ghaffari     // -- Interp in
16677841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
1672b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
16877841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
16977841947SLeila Ghaffari 
170f3e15844SJames Wright     CeedScalar wdetJb, norm[3];
171f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
172f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
17377841947SLeila Ghaffari 
17477841947SLeila Ghaffari     // Normal velocity
17577841947SLeila Ghaffari     const CeedScalar u_normal = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2];
17677841947SLeila Ghaffari 
17777841947SLeila Ghaffari     // No Change in density or momentum
17877841947SLeila Ghaffari     for (CeedInt j = 0; j < 4; j++) {
17977841947SLeila Ghaffari       v[j][i] = 0;
18077841947SLeila Ghaffari     }
18177841947SLeila Ghaffari     // Implementing in/outflow BCs
18277841947SLeila Ghaffari     if (u_normal > 0) {  // outflow
18377841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
18477841947SLeila Ghaffari     } else {  // inflow
18577841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
18677841947SLeila Ghaffari     }
18777841947SLeila Ghaffari   }  // End Quadrature Point Loop
18877841947SLeila Ghaffari   return 0;
18977841947SLeila Ghaffari }
19077841947SLeila Ghaffari // *****************************************************************************
19177841947SLeila Ghaffari 
19277841947SLeila Ghaffari #endif  // advection_h
193