xref: /libCEED/examples/fluids/qfunctions/advection.h (revision 30e1b2c7fb15f28dd172201f6d9e657bf5f698ea)
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 
17*30e1b2c7SJames 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 
35*30e1b2c7SJames Wright     Exact_AdvectionGeneric(3, 0., x, 5, q, ctx);
3677841947SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
37*30e1b2c7SJames 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 
13177841947SLeila Ghaffari // *****************************************************************************
132ea61e9acSJeremy L Thompson // This QFunction implements 3D (mentioned above) with implicit time stepping method
13377841947SLeila Ghaffari // *****************************************************************************
1342b730f8bSJeremy L Thompson CEED_QFUNCTION(IFunction_Advection)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
13546603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1368f4d89c8SJames Wright   const CeedScalar(*Grad_q)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
13746603fc5SJames Wright   const CeedScalar(*q_dot)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[2];
138f3e15844SJames Wright   const CeedScalar(*q_data)                = in[3];
13946603fc5SJames Wright 
14046603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]         = (CeedScalar(*)[CEED_Q_VLA])out[0];
1418f4d89c8SJames Wright   CeedScalar(*Grad_v)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
14229ea4e10SJames Wright   CeedScalar *jac_data               = out[2];
14346603fc5SJames Wright 
14477841947SLeila Ghaffari   AdvectionContext                 context     = (AdvectionContext)ctx;
14577841947SLeila Ghaffari   const CeedScalar                 CtauS       = context->CtauS;
14677841947SLeila Ghaffari   const CeedScalar                 strong_form = context->strong_form;
14729ea4e10SJames Wright   const CeedScalar                 zeros[14]   = {0.};
1488f4d89c8SJames Wright   NewtonianIdealGasContext         gas;
1498f4d89c8SJames Wright   struct NewtonianIdealGasContext_ gas_struct = {0};
1508f4d89c8SJames Wright   gas                                         = &gas_struct;
15177841947SLeila Ghaffari 
15246603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1538f4d89c8SJames Wright     const CeedScalar qi[5] = {q[0][i], q[1][i], q[2][i], q[3][i], q[4][i]};
1548f4d89c8SJames Wright     const State      s     = StateFromU(gas, qi);
1558f4d89c8SJames Wright 
156f3e15844SJames Wright     CeedScalar wdetJ, dXdx[3][3];
157f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
1588f4d89c8SJames Wright     State grad_s[3];
1598f4d89c8SJames Wright     StatePhysicalGradientFromReference(Q, i, gas, s, STATEVAR_CONSERVATIVE, (CeedScalar *)Grad_q, dXdx, grad_s);
16077841947SLeila Ghaffari 
1618f4d89c8SJames Wright     const CeedScalar Grad_E[3] = {grad_s[0].U.E_total, grad_s[1].U.E_total, grad_s[2].U.E_total};
1628f4d89c8SJames Wright 
16377841947SLeila Ghaffari     for (CeedInt f = 0; f < 4; f++) {
1648f4d89c8SJames Wright       for (CeedInt j = 0; j < 3; j++) Grad_v[j][f][i] = 0;  // No Change in density or momentum
16577841947SLeila Ghaffari       v[f][i] = wdetJ * q_dot[f][i];                        // K Mass/transient term
16677841947SLeila Ghaffari     }
16777841947SLeila Ghaffari 
1688f4d89c8SJames Wright     CeedScalar div_u = 0;
16977841947SLeila Ghaffari     for (CeedInt j = 0; j < 3; j++) {
17077841947SLeila Ghaffari       for (CeedInt k = 0; k < 3; k++) {
1718f4d89c8SJames Wright         div_u += grad_s[k].Y.velocity[j];
17277841947SLeila Ghaffari       }
17377841947SLeila Ghaffari     }
1748f4d89c8SJames Wright     CeedScalar strong_conv = s.U.E_total * div_u + Dot3(s.Y.velocity, Grad_E);
17577841947SLeila Ghaffari     CeedScalar strong_res  = q_dot[4][i] + strong_conv;
17677841947SLeila Ghaffari 
17777841947SLeila Ghaffari     v[4][i] = wdetJ * q_dot[4][i];  // transient part (ALWAYS)
17877841947SLeila Ghaffari 
1798f4d89c8SJames Wright     if (strong_form) {  // Strong Galerkin convection term: v div(E u)
1808f4d89c8SJames Wright       v[4][i] += wdetJ * strong_conv;
1818f4d89c8SJames Wright     } else {  // Weak Galerkin convection term: -dv \cdot (E u)
1828f4d89c8SJames Wright       for (CeedInt j = 0; j < 3; j++)
1838f4d89c8SJames Wright         Grad_v[j][4][i] = -wdetJ * s.U.E_total * (s.Y.velocity[0] * dXdx[j][0] + s.Y.velocity[1] * dXdx[j][1] + s.Y.velocity[2] * dXdx[j][2]);
1848f4d89c8SJames Wright     }
18577841947SLeila Ghaffari 
1868f4d89c8SJames Wright     // Stabilization requires a measure of element transit time in the velocity field u.
1878f4d89c8SJames Wright     CeedScalar uX[3] = {0.};
1888f4d89c8SJames Wright     MatVec3(dXdx, s.Y.velocity, CEED_NOTRANSPOSE, uX);
1898f4d89c8SJames Wright     const CeedScalar TauS = CtauS / sqrt(Dot3(uX, uX));
19077841947SLeila Ghaffari 
1912b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) switch (context->stabilization) {
192700ae941SJames Wright         case STAB_NONE:
19377841947SLeila Ghaffari           break;
194700ae941SJames Wright         case STAB_SU:
1958f4d89c8SJames Wright           Grad_v[j][4][i] += wdetJ * TauS * strong_conv * uX[j];
19677841947SLeila Ghaffari           break;
197700ae941SJames Wright         case STAB_SUPG:
1988f4d89c8SJames Wright           Grad_v[j][4][i] += wdetJ * TauS * strong_res * uX[j];
19977841947SLeila Ghaffari           break;
20077841947SLeila Ghaffari       }
20129ea4e10SJames Wright     StoredValuesPack(Q, i, 0, 14, zeros, jac_data);
2028f4d89c8SJames Wright   }
20377841947SLeila Ghaffari   return 0;
20477841947SLeila Ghaffari }
20577841947SLeila Ghaffari 
20677841947SLeila Ghaffari // *****************************************************************************
20777841947SLeila Ghaffari // This QFunction implements consistent outflow and inflow BCs
20877841947SLeila Ghaffari //      for 3D advection
20977841947SLeila Ghaffari //
21077841947SLeila Ghaffari //  Inflow and outflow faces are determined based on sign(dot(wind, normal)):
21177841947SLeila Ghaffari //    sign(dot(wind, normal)) > 0 : outflow BCs
21277841947SLeila Ghaffari //    sign(dot(wind, normal)) < 0 : inflow BCs
21377841947SLeila Ghaffari //
21477841947SLeila Ghaffari //  Outflow BCs:
215ea61e9acSJeremy 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.
21677841947SLeila Ghaffari //
21777841947SLeila Ghaffari //  Inflow BCs:
21877841947SLeila Ghaffari //    A prescribed Total Energy (E_wind) is applied weakly.
21977841947SLeila Ghaffari // *****************************************************************************
2202b730f8bSJeremy L Thompson CEED_QFUNCTION(Advection_InOutFlow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
22177841947SLeila Ghaffari   // Inputs
22246603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
223f3e15844SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
22446603fc5SJames Wright 
22577841947SLeila Ghaffari   // Outputs
22677841947SLeila Ghaffari   CeedScalar(*v)[CEED_Q_VLA]   = (CeedScalar(*)[CEED_Q_VLA])out[0];
22777841947SLeila Ghaffari   AdvectionContext context     = (AdvectionContext)ctx;
22877841947SLeila Ghaffari   const CeedScalar E_wind      = context->E_wind;
22977841947SLeila Ghaffari   const CeedScalar strong_form = context->strong_form;
230f3e15844SJames Wright   const bool       is_implicit = context->implicit;
23177841947SLeila Ghaffari 
23277841947SLeila Ghaffari   // Quadrature Point Loop
23346603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
23477841947SLeila Ghaffari     // Setup
23577841947SLeila Ghaffari     // -- Interp in
23677841947SLeila Ghaffari     const CeedScalar rho  = q[0][i];
2372b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
23877841947SLeila Ghaffari     const CeedScalar E    = q[4][i];
23977841947SLeila Ghaffari 
240f3e15844SJames Wright     CeedScalar wdetJb, norm[3];
241f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
242f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
24377841947SLeila Ghaffari 
24477841947SLeila Ghaffari     // Normal velocity
24577841947SLeila Ghaffari     const CeedScalar u_normal = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2];
24677841947SLeila Ghaffari 
24777841947SLeila Ghaffari     // No Change in density or momentum
24877841947SLeila Ghaffari     for (CeedInt j = 0; j < 4; j++) {
24977841947SLeila Ghaffari       v[j][i] = 0;
25077841947SLeila Ghaffari     }
25177841947SLeila Ghaffari     // Implementing in/outflow BCs
25277841947SLeila Ghaffari     if (u_normal > 0) {  // outflow
25377841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E * u_normal;
25477841947SLeila Ghaffari     } else {  // inflow
25577841947SLeila Ghaffari       v[4][i] = -(1 - strong_form) * wdetJb * E_wind * u_normal;
25677841947SLeila Ghaffari     }
25777841947SLeila Ghaffari   }  // End Quadrature Point Loop
25877841947SLeila Ghaffari   return 0;
25977841947SLeila Ghaffari }
26077841947SLeila Ghaffari // *****************************************************************************
26177841947SLeila Ghaffari 
26277841947SLeila Ghaffari #endif  // advection_h
263