xref: /libCEED/examples/fluids/qfunctions/channel.h (revision f21e6b1c01e749a944b78ce17661976d3d016874)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
288626eedSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
388626eedSJames Wright //
488626eedSJames Wright // SPDX-License-Identifier: BSD-2-Clause
588626eedSJames Wright //
688626eedSJames Wright // This file is part of CEED:  http://github.com/ceed
788626eedSJames Wright 
888626eedSJames Wright /// @file
988626eedSJames Wright /// Operator for Navier-Stokes example using PETSc
10c9c2c079SJeremy L Thompson #include <ceed.h>
1188626eedSJames Wright #include <math.h>
122b730f8bSJeremy L Thompson 
13dc805cc4SLeila Ghaffari #include "newtonian_state.h"
14c9c2c079SJeremy L Thompson #include "newtonian_types.h"
1513fa47b2SJames Wright #include "utils.h"
1688626eedSJames Wright 
1788626eedSJames Wright typedef struct ChannelContext_ *ChannelContext;
1888626eedSJames Wright struct ChannelContext_ {
1988626eedSJames Wright   bool                             implicit;  // !< Using implicit timesteping or not
2088626eedSJames Wright   CeedScalar                       theta0;    // !< Reference temperature
2188626eedSJames Wright   CeedScalar                       P0;        // !< Reference Pressure
2288626eedSJames Wright   CeedScalar                       umax;      // !< Centerline velocity
2388626eedSJames Wright   CeedScalar                       center;    // !< Y Coordinate for center of channel
2488626eedSJames Wright   CeedScalar                       H;         // !< Channel half-height
2588626eedSJames Wright   CeedScalar                       B;         // !< Body-force driving the flow
2688626eedSJames Wright   struct NewtonianIdealGasContext_ newtonian_ctx;
2788626eedSJames Wright };
2888626eedSJames Wright 
292b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER State Exact_Channel(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, void *ctx) {
3088626eedSJames Wright   const ChannelContext     context = (ChannelContext)ctx;
3188626eedSJames Wright   const CeedScalar         theta0  = context->theta0;
3288626eedSJames Wright   const CeedScalar         P0      = context->P0;
3388626eedSJames Wright   const CeedScalar         umax    = context->umax;
3488626eedSJames Wright   const CeedScalar         center  = context->center;
3588626eedSJames Wright   const CeedScalar         H       = context->H;
36dc805cc4SLeila Ghaffari   NewtonianIdealGasContext gas     = &context->newtonian_ctx;
37dc805cc4SLeila Ghaffari   const CeedScalar         cp      = gas->cp;
38dc805cc4SLeila Ghaffari   const CeedScalar         mu      = gas->mu;
39dc805cc4SLeila Ghaffari   const CeedScalar         k       = gas->k;
40dc805cc4SLeila Ghaffari   // There is a gravity body force but it is excluded from
41dc805cc4SLeila Ghaffari   //   the potential energy due to periodicity.
422b89d87eSLeila Ghaffari   //     g = (g, 0, 0)
432b89d87eSLeila Ghaffari   //     x = (0, x_2, x_3)
442b89d87eSLeila Ghaffari   //     e_potential = dot(g, x) = 0
452b89d87eSLeila Ghaffari   const CeedScalar x[3] = {0, X[1], X[2]};
4688626eedSJames Wright 
4788626eedSJames Wright   const CeedScalar Pr    = mu / (cp * k);
4888626eedSJames Wright   const CeedScalar Ec    = (umax * umax) / (cp * theta0);
492b730f8bSJeremy L Thompson   const CeedScalar theta = theta0 * (1 + (Pr * Ec / 3) * (1 - Square(Square((x[1] - center) / H))));
50dc805cc4SLeila Ghaffari   CeedScalar       Y[5]  = {0.};
51dc805cc4SLeila Ghaffari   Y[0]                   = P0;
522b89d87eSLeila Ghaffari   Y[1]                   = umax * (1 - Square((x[1] - center) / H));
53dc805cc4SLeila Ghaffari   Y[2]                   = 0.;
54dc805cc4SLeila Ghaffari   Y[3]                   = 0.;
55dc805cc4SLeila Ghaffari   Y[4]                   = theta;
5688626eedSJames Wright 
573bd61617SKenneth E. Jansen   return StateFromY(gas, Y);
5888626eedSJames Wright }
5988626eedSJames Wright 
6088626eedSJames Wright // *****************************************************************************
61dc805cc4SLeila Ghaffari // This QFunction set the initial condition
6288626eedSJames Wright // *****************************************************************************
632b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsChannel)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
6488626eedSJames Wright   // Inputs
6588626eedSJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
6688626eedSJames Wright 
6788626eedSJames Wright   // Outputs
6888626eedSJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
6988626eedSJames Wright 
70dc805cc4SLeila Ghaffari   // Context
71dc805cc4SLeila Ghaffari   const ChannelContext context = (ChannelContext)ctx;
72dc805cc4SLeila Ghaffari 
7388626eedSJames Wright   // Quadrature Point Loop
742b730f8bSJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
7588626eedSJames Wright     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
76dc805cc4SLeila Ghaffari     State            s    = Exact_Channel(3, 0., x, 5, ctx);
772b89d87eSLeila Ghaffari     CeedScalar       q[5] = {0};
7897baf651SJames Wright     switch (context->newtonian_ctx.state_var) {
7997baf651SJames Wright       case STATEVAR_CONSERVATIVE:
802b89d87eSLeila Ghaffari         UnpackState_U(s.U, q);
8197baf651SJames Wright         break;
8297baf651SJames Wright       case STATEVAR_PRIMITIVE:
8397baf651SJames Wright         UnpackState_Y(s.Y, q);
8497baf651SJames Wright         break;
8597baf651SJames Wright     }
862b89d87eSLeila Ghaffari 
872b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
8888626eedSJames Wright 
8988626eedSJames Wright   }  // End of Quadrature Point Loop
9088626eedSJames Wright   return 0;
9188626eedSJames Wright }
9288626eedSJames Wright 
9388626eedSJames Wright // *****************************************************************************
942b89d87eSLeila Ghaffari // This QFunction set the inflow boundary condition for conservative variables
952b89d87eSLeila Ghaffari // *****************************************************************************
962b730f8bSJeremy L Thompson CEED_QFUNCTION(Channel_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
9788626eedSJames Wright   // Inputs
9846603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
99f3e15844SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
10046603fc5SJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
10188626eedSJames Wright 
10288626eedSJames Wright   // Outputs
10388626eedSJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
10446603fc5SJames Wright 
10588626eedSJames Wright   const ChannelContext     context     = (ChannelContext)ctx;
106f3e15844SJames Wright   const bool               is_implicit = context->implicit;
1072b89d87eSLeila Ghaffari   NewtonianIdealGasContext gas         = &context->newtonian_ctx;
1082b89d87eSLeila Ghaffari   const CeedScalar         cv          = gas->cv;
10946603fc5SJames Wright   const CeedScalar         gamma       = HeatCapacityRatio(&context->newtonian_ctx);
11088626eedSJames Wright 
11188626eedSJames Wright   // Quadrature Point Loop
11246603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
113f3e15844SJames Wright     CeedScalar wdetJb, norm[3];
114f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
115f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
11688626eedSJames Wright 
1172b89d87eSLeila Ghaffari     // There is a gravity body force but it is excluded from
1182b89d87eSLeila Ghaffari     //   the potential energy due to periodicity.
1192b89d87eSLeila Ghaffari     //     g = (g, 0, 0)
1202b89d87eSLeila Ghaffari     //     x = (0, x_2, x_3)
1212b89d87eSLeila Ghaffari     //     e_potential = dot(g, x) = 0
1222b89d87eSLeila Ghaffari     const CeedScalar x[3] = {0, X[1][i], X[2][i]};
1232b89d87eSLeila Ghaffari 
124*f21e6b1cSJames Wright     // Calculate prescribed inflow values
1252b89d87eSLeila Ghaffari     State      s_exact    = Exact_Channel(3, 0., x, 5, ctx);
12688626eedSJames Wright     CeedScalar q_exact[5] = {0.};
1272b89d87eSLeila Ghaffari     UnpackState_U(s_exact.U, q_exact);
12888626eedSJames Wright 
12988626eedSJames Wright     // Find pressure using state inside the domain
1302b89d87eSLeila Ghaffari     CeedScalar q_inside[5] = {0};
1312b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q_inside[j] = q[j][i];
1323bd61617SKenneth E. Jansen     State            s_inside = StateFromU(gas, q_inside);
1332b89d87eSLeila Ghaffari     const CeedScalar P        = s_inside.Y.pressure;
13488626eedSJames Wright 
13588626eedSJames Wright     // Find inflow state using calculated P and prescribed velocity, theta0
1362b89d87eSLeila Ghaffari     const CeedScalar e_internal = cv * s_exact.Y.temperature;
13788626eedSJames Wright     const CeedScalar rho_in     = P / ((gamma - 1) * e_internal);
1382b730f8bSJeremy L Thompson     const CeedScalar E_kinetic  = .5 * rho_in * Dot3(s_exact.Y.velocity, s_exact.Y.velocity);
13988626eedSJames Wright     const CeedScalar E          = rho_in * e_internal + E_kinetic;
1402b89d87eSLeila Ghaffari 
14188626eedSJames Wright     // The Physics
14288626eedSJames Wright     // Zero v so all future terms can safely sum into it
143ba6664aeSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.;
14488626eedSJames Wright 
1452b89d87eSLeila Ghaffari     const CeedScalar u_normal = Dot3(norm, s_exact.Y.velocity);
14688626eedSJames Wright 
14788626eedSJames Wright     // The Physics
14888626eedSJames Wright     // -- Density
14988626eedSJames Wright     v[0][i] -= wdetJb * rho_in * u_normal;
15088626eedSJames Wright 
15188626eedSJames Wright     // -- Momentum
1522b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho_in * u_normal * s_exact.Y.velocity[j] + norm[j] * P);
15388626eedSJames Wright 
15488626eedSJames Wright     // -- Total Energy Density
15588626eedSJames Wright     v[4][i] -= wdetJb * u_normal * (E + P);
15688626eedSJames Wright 
15788626eedSJames Wright   }  // End Quadrature Point Loop
15888626eedSJames Wright   return 0;
15988626eedSJames Wright }
16088626eedSJames Wright 
16188626eedSJames Wright // *****************************************************************************
1622b89d87eSLeila Ghaffari // This QFunction set the outflow boundary condition for conservative variables
1632b89d87eSLeila Ghaffari // *****************************************************************************
1642b730f8bSJeremy L Thompson CEED_QFUNCTION(Channel_Outflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
16588626eedSJames Wright   // Inputs
16646603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
167f3e15844SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
168e8b03feeSJames Wright 
16988626eedSJames Wright   // Outputs
17088626eedSJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
17188626eedSJames Wright 
17288626eedSJames Wright   const ChannelContext context     = (ChannelContext)ctx;
173f3e15844SJames Wright   const bool           is_implicit = context->implicit;
17488626eedSJames Wright   const CeedScalar     P0          = context->P0;
17588626eedSJames Wright 
17688626eedSJames Wright   // Quadrature Point Loop
17746603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
178f3e15844SJames Wright     CeedScalar wdetJb, norm[3];
179f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
180f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
181f3e15844SJames Wright 
18288626eedSJames Wright     const CeedScalar rho  = q[0][i];
1832b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
18488626eedSJames Wright     const CeedScalar E    = q[4][i];
18588626eedSJames Wright 
18688626eedSJames Wright     // The Physics
18788626eedSJames Wright     // Zero v so all future terms can safely sum into it
188ba6664aeSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.;
18988626eedSJames Wright 
19088626eedSJames Wright     // Implementing outflow condition
19188626eedSJames Wright     const CeedScalar P        = P0;             // pressure
19213fa47b2SJames Wright     const CeedScalar u_normal = Dot3(norm, u);  // Normal velocity
19388626eedSJames Wright     // The Physics
19488626eedSJames Wright     // -- Density
19588626eedSJames Wright     v[0][i] -= wdetJb * rho * u_normal;
19688626eedSJames Wright 
19788626eedSJames Wright     // -- Momentum
1982b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho * u_normal * u[j] + norm[j] * P);
19988626eedSJames Wright 
20088626eedSJames Wright     // -- Total Energy Density
20188626eedSJames Wright     v[4][i] -= wdetJb * u_normal * (E + P);
20288626eedSJames Wright 
20388626eedSJames Wright   }  // End Quadrature Point Loop
20488626eedSJames Wright   return 0;
20588626eedSJames Wright }
206