xref: /libCEED/examples/fluids/qfunctions/channel.h (revision 46603fc57e28d79cde01b07e9ca450b5fd78aed4)
188626eedSJames Wright // Copyright (c) 2017-2022, 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
1088626eedSJames Wright 
1188626eedSJames Wright #ifndef channel_h
1288626eedSJames Wright #define channel_h
1388626eedSJames Wright 
14c9c2c079SJeremy L Thompson #include <ceed.h>
1588626eedSJames Wright #include <math.h>
162b730f8bSJeremy L Thompson 
17dc805cc4SLeila Ghaffari #include "newtonian_state.h"
18c9c2c079SJeremy L Thompson #include "newtonian_types.h"
1913fa47b2SJames Wright #include "utils.h"
2088626eedSJames Wright 
2188626eedSJames Wright typedef struct ChannelContext_ *ChannelContext;
2288626eedSJames Wright struct ChannelContext_ {
2388626eedSJames Wright   bool                             implicit;  // !< Using implicit timesteping or not
2488626eedSJames Wright   CeedScalar                       theta0;    // !< Reference temperature
2588626eedSJames Wright   CeedScalar                       P0;        // !< Reference Pressure
2688626eedSJames Wright   CeedScalar                       umax;      // !< Centerline velocity
2788626eedSJames Wright   CeedScalar                       center;    // !< Y Coordinate for center of channel
2888626eedSJames Wright   CeedScalar                       H;         // !< Channel half-height
2988626eedSJames Wright   CeedScalar                       B;         // !< Body-force driving the flow
3088626eedSJames Wright   struct NewtonianIdealGasContext_ newtonian_ctx;
3188626eedSJames Wright };
3288626eedSJames Wright 
332b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER State Exact_Channel(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, void *ctx) {
3488626eedSJames Wright   const ChannelContext     context = (ChannelContext)ctx;
3588626eedSJames Wright   const CeedScalar         theta0  = context->theta0;
3688626eedSJames Wright   const CeedScalar         P0      = context->P0;
3788626eedSJames Wright   const CeedScalar         umax    = context->umax;
3888626eedSJames Wright   const CeedScalar         center  = context->center;
3988626eedSJames Wright   const CeedScalar         H       = context->H;
40dc805cc4SLeila Ghaffari   NewtonianIdealGasContext gas     = &context->newtonian_ctx;
41dc805cc4SLeila Ghaffari   const CeedScalar         cp      = gas->cp;
42dc805cc4SLeila Ghaffari   const CeedScalar         mu      = gas->mu;
43dc805cc4SLeila Ghaffari   const CeedScalar         k       = gas->k;
44dc805cc4SLeila Ghaffari   // There is a gravity body force but it is excluded from
45dc805cc4SLeila Ghaffari   //   the potential energy due to periodicity.
462b89d87eSLeila Ghaffari   //     g = (g, 0, 0)
472b89d87eSLeila Ghaffari   //     x = (0, x_2, x_3)
482b89d87eSLeila Ghaffari   //     e_potential = dot(g, x) = 0
492b89d87eSLeila Ghaffari   const CeedScalar x[3] = {0, X[1], X[2]};
5088626eedSJames Wright 
5188626eedSJames Wright   const CeedScalar Pr    = mu / (cp * k);
5288626eedSJames Wright   const CeedScalar Ec    = (umax * umax) / (cp * theta0);
532b730f8bSJeremy L Thompson   const CeedScalar theta = theta0 * (1 + (Pr * Ec / 3) * (1 - Square(Square((x[1] - center) / H))));
54dc805cc4SLeila Ghaffari   CeedScalar       Y[5]  = {0.};
55dc805cc4SLeila Ghaffari   Y[0]                   = P0;
562b89d87eSLeila Ghaffari   Y[1]                   = umax * (1 - Square((x[1] - center) / H));
57dc805cc4SLeila Ghaffari   Y[2]                   = 0.;
58dc805cc4SLeila Ghaffari   Y[3]                   = 0.;
59dc805cc4SLeila Ghaffari   Y[4]                   = theta;
6088626eedSJames Wright 
612b89d87eSLeila Ghaffari   return StateFromY(gas, Y, x);
6288626eedSJames Wright }
6388626eedSJames Wright 
6488626eedSJames Wright // *****************************************************************************
65dc805cc4SLeila Ghaffari // This QFunction set the initial condition
6688626eedSJames Wright // *****************************************************************************
672b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsChannel)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
6888626eedSJames Wright   // Inputs
6988626eedSJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
7088626eedSJames Wright 
7188626eedSJames Wright   // Outputs
7288626eedSJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
7388626eedSJames Wright 
74dc805cc4SLeila Ghaffari   // Context
75dc805cc4SLeila Ghaffari   const ChannelContext context = (ChannelContext)ctx;
76dc805cc4SLeila Ghaffari 
7788626eedSJames Wright   // Quadrature Point Loop
782b730f8bSJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
7988626eedSJames Wright     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
80dc805cc4SLeila Ghaffari     State            s    = Exact_Channel(3, 0., x, 5, ctx);
812b89d87eSLeila Ghaffari     CeedScalar       q[5] = {0};
8297baf651SJames Wright     switch (context->newtonian_ctx.state_var) {
8397baf651SJames Wright       case STATEVAR_CONSERVATIVE:
842b89d87eSLeila Ghaffari         UnpackState_U(s.U, q);
8597baf651SJames Wright         break;
8697baf651SJames Wright       case STATEVAR_PRIMITIVE:
8797baf651SJames Wright         UnpackState_Y(s.Y, q);
8897baf651SJames Wright         break;
8997baf651SJames Wright     }
902b89d87eSLeila Ghaffari 
912b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
9288626eedSJames Wright 
9388626eedSJames Wright   }  // End of Quadrature Point Loop
9488626eedSJames Wright   return 0;
9588626eedSJames Wright }
9688626eedSJames Wright 
9788626eedSJames Wright // *****************************************************************************
982b89d87eSLeila Ghaffari // This QFunction set the inflow boundary condition for conservative variables
992b89d87eSLeila Ghaffari // *****************************************************************************
1002b730f8bSJeremy L Thompson CEED_QFUNCTION(Channel_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
10188626eedSJames Wright   // Inputs
102*46603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0];
103*46603fc5SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
104*46603fc5SJames Wright   const CeedScalar(*X)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
10588626eedSJames Wright 
10688626eedSJames Wright   // Outputs
10788626eedSJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
108*46603fc5SJames Wright 
10988626eedSJames Wright   const ChannelContext     context  = (ChannelContext)ctx;
11088626eedSJames Wright   const bool               implicit = context->implicit;
1112b89d87eSLeila Ghaffari   NewtonianIdealGasContext gas      = &context->newtonian_ctx;
1122b89d87eSLeila Ghaffari   const CeedScalar         cv       = gas->cv;
113*46603fc5SJames Wright   const CeedScalar         gamma    = HeatCapacityRatio(&context->newtonian_ctx);
11488626eedSJames Wright 
11588626eedSJames Wright   // Quadrature Point Loop
116*46603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
11788626eedSJames Wright     // Setup
11888626eedSJames Wright     // -- Interp-to-Interp q_data
11988626eedSJames Wright     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
12088626eedSJames Wright     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
12188626eedSJames Wright     // We can effect this by swapping the sign on this weight
12288626eedSJames Wright     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
12388626eedSJames Wright 
1242b89d87eSLeila Ghaffari     // There is a gravity body force but it is excluded from
1252b89d87eSLeila Ghaffari     //   the potential energy due to periodicity.
1262b89d87eSLeila Ghaffari     //     g = (g, 0, 0)
1272b89d87eSLeila Ghaffari     //     x = (0, x_2, x_3)
1282b89d87eSLeila Ghaffari     //     e_potential = dot(g, x) = 0
1292b89d87eSLeila Ghaffari     const CeedScalar x[3] = {0, X[1][i], X[2][i]};
1302b89d87eSLeila Ghaffari 
13188626eedSJames Wright     // Calcualte prescribed inflow values
1322b89d87eSLeila Ghaffari     State      s_exact    = Exact_Channel(3, 0., x, 5, ctx);
13388626eedSJames Wright     CeedScalar q_exact[5] = {0.};
1342b89d87eSLeila Ghaffari     UnpackState_U(s_exact.U, q_exact);
13588626eedSJames Wright 
13688626eedSJames Wright     // Find pressure using state inside the domain
1372b89d87eSLeila Ghaffari     CeedScalar q_inside[5] = {0};
1382b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q_inside[j] = q[j][i];
1392b89d87eSLeila Ghaffari     State            s_inside = StateFromU(gas, q_inside, x);
1402b89d87eSLeila Ghaffari     const CeedScalar P        = s_inside.Y.pressure;
14188626eedSJames Wright 
14288626eedSJames Wright     // Find inflow state using calculated P and prescribed velocity, theta0
1432b89d87eSLeila Ghaffari     const CeedScalar e_internal = cv * s_exact.Y.temperature;
14488626eedSJames Wright     const CeedScalar rho_in     = P / ((gamma - 1) * e_internal);
1452b730f8bSJeremy L Thompson     const CeedScalar E_kinetic  = .5 * rho_in * Dot3(s_exact.Y.velocity, s_exact.Y.velocity);
14688626eedSJames Wright     const CeedScalar E          = rho_in * e_internal + E_kinetic;
1472b89d87eSLeila Ghaffari 
14888626eedSJames Wright     // ---- Normal vect
1492b730f8bSJeremy L Thompson     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
15088626eedSJames Wright     // The Physics
15188626eedSJames Wright     // Zero v so all future terms can safely sum into it
152ba6664aeSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.;
15388626eedSJames Wright 
1542b89d87eSLeila Ghaffari     const CeedScalar u_normal = Dot3(norm, s_exact.Y.velocity);
15588626eedSJames Wright 
15688626eedSJames Wright     // The Physics
15788626eedSJames Wright     // -- Density
15888626eedSJames Wright     v[0][i] -= wdetJb * rho_in * u_normal;
15988626eedSJames Wright 
16088626eedSJames Wright     // -- Momentum
1612b730f8bSJeremy 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);
16288626eedSJames Wright 
16388626eedSJames Wright     // -- Total Energy Density
16488626eedSJames Wright     v[4][i] -= wdetJb * u_normal * (E + P);
16588626eedSJames Wright 
16688626eedSJames Wright   }  // End Quadrature Point Loop
16788626eedSJames Wright   return 0;
16888626eedSJames Wright }
16988626eedSJames Wright 
17088626eedSJames Wright // *****************************************************************************
1712b89d87eSLeila Ghaffari // This QFunction set the outflow boundary condition for conservative variables
1722b89d87eSLeila Ghaffari // *****************************************************************************
1732b730f8bSJeremy L Thompson CEED_QFUNCTION(Channel_Outflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
17488626eedSJames Wright   // Inputs
175*46603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0];
176*46603fc5SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
177e8b03feeSJames Wright 
17888626eedSJames Wright   // Outputs
17988626eedSJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
18088626eedSJames Wright 
18188626eedSJames Wright   const ChannelContext context  = (ChannelContext)ctx;
18288626eedSJames Wright   const bool           implicit = context->implicit;
18388626eedSJames Wright   const CeedScalar     P0       = context->P0;
18488626eedSJames Wright 
18588626eedSJames Wright   // Quadrature Point Loop
186*46603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
18788626eedSJames Wright     // Setup
18888626eedSJames Wright     // -- Interp in
18988626eedSJames Wright     const CeedScalar rho  = q[0][i];
1902b730f8bSJeremy L Thompson     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
19188626eedSJames Wright     const CeedScalar E    = q[4][i];
19288626eedSJames Wright 
19388626eedSJames Wright     // -- Interp-to-Interp q_data
19488626eedSJames Wright     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
19588626eedSJames Wright     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
19688626eedSJames Wright     // We can effect this by swapping the sign on this weight
19788626eedSJames Wright     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
19888626eedSJames Wright 
19988626eedSJames Wright     // ---- Normal vect
2002b730f8bSJeremy L Thompson     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
20188626eedSJames Wright     // The Physics
20288626eedSJames Wright     // Zero v so all future terms can safely sum into it
203ba6664aeSJames Wright     for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.;
20488626eedSJames Wright 
20588626eedSJames Wright     // Implementing outflow condition
20688626eedSJames Wright     const CeedScalar P        = P0;             // pressure
20713fa47b2SJames Wright     const CeedScalar u_normal = Dot3(norm, u);  // Normal velocity
20888626eedSJames Wright     // The Physics
20988626eedSJames Wright     // -- Density
21088626eedSJames Wright     v[0][i] -= wdetJb * rho * u_normal;
21188626eedSJames Wright 
21288626eedSJames Wright     // -- Momentum
2132b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho * u_normal * u[j] + norm[j] * P);
21488626eedSJames Wright 
21588626eedSJames Wright     // -- Total Energy Density
21688626eedSJames Wright     v[4][i] -= wdetJb * u_normal * (E + P);
21788626eedSJames Wright 
21888626eedSJames Wright   }  // End Quadrature Point Loop
21988626eedSJames Wright   return 0;
22088626eedSJames Wright }
221dc805cc4SLeila Ghaffari 
222dc805cc4SLeila Ghaffari // *****************************************************************************
22388626eedSJames Wright #endif  // channel_h
224