xref: /libCEED/examples/fluids/qfunctions/channel.h (revision dc805cc4a09d29f27b3febd084feb659e74b9d08)
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 
1288626eedSJames Wright #ifndef channel_h
1388626eedSJames Wright #define channel_h
1488626eedSJames Wright 
1588626eedSJames Wright #include <math.h>
16c32eb7cbSJed Brown #include <ceed/ceed.h>
17841e4c73SJed Brown #include "newtonian_types.h"
18*dc805cc4SLeila Ghaffari #include "newtonian_state.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 
33*dc805cc4SLeila Ghaffari CEED_QFUNCTION_HELPER State Exact_Channel(CeedInt dim, CeedScalar time,
34*dc805cc4SLeila Ghaffari     const CeedScalar X[], CeedInt Nf, void *ctx) {
3588626eedSJames Wright 
3688626eedSJames Wright   const ChannelContext context = (ChannelContext)ctx;
3788626eedSJames Wright   const CeedScalar theta0      = context->theta0;
3888626eedSJames Wright   const CeedScalar P0          = context->P0;
3988626eedSJames Wright   const CeedScalar umax        = context->umax;
4088626eedSJames Wright   const CeedScalar center      = context->center;
4188626eedSJames Wright   const CeedScalar H           = context->H;
42*dc805cc4SLeila Ghaffari   NewtonianIdealGasContext gas = &context->newtonian_ctx;
43*dc805cc4SLeila Ghaffari   const CeedScalar cp          = gas->cp;
44*dc805cc4SLeila Ghaffari   const CeedScalar mu          = gas->mu;
45*dc805cc4SLeila Ghaffari   const CeedScalar k           = gas->k;
46*dc805cc4SLeila Ghaffari   // There is a gravity body force but it is excluded from
47*dc805cc4SLeila Ghaffari   //   the potential energy due to periodicity.
48*dc805cc4SLeila Ghaffari   gas->g[0] = 0.;
49*dc805cc4SLeila Ghaffari   gas->g[1] = 0.;
50*dc805cc4SLeila Ghaffari   gas->g[2] = 0.;
5188626eedSJames Wright 
5288626eedSJames Wright   const CeedScalar y     = X[1];
5388626eedSJames Wright   const CeedScalar Pr    = mu / (cp*k);
5488626eedSJames Wright   const CeedScalar Ec    = (umax*umax) / (cp*theta0);
55c32eb7cbSJed Brown   const CeedScalar theta = theta0*(1 + (Pr*Ec/3)
56c32eb7cbSJed Brown                                    * (1 - Square(Square((y-center)/H))));
57*dc805cc4SLeila Ghaffari   CeedScalar Y[5] = {0.};
58*dc805cc4SLeila Ghaffari   Y[0] = P0;
59*dc805cc4SLeila Ghaffari   Y[1] = umax*(1 - Square((y-center)/H));
60*dc805cc4SLeila Ghaffari   Y[2] = 0.;
61*dc805cc4SLeila Ghaffari   Y[3] = 0.;
62*dc805cc4SLeila Ghaffari   Y[4] = theta;
6388626eedSJames Wright 
64*dc805cc4SLeila Ghaffari   return StateFromY(gas, Y, X);
6588626eedSJames Wright }
6688626eedSJames Wright 
6788626eedSJames Wright // *****************************************************************************
68*dc805cc4SLeila Ghaffari // This QFunction set the initial condition
6988626eedSJames Wright // *****************************************************************************
7088626eedSJames Wright CEED_QFUNCTION(ICsChannel)(void *ctx, CeedInt Q,
7188626eedSJames Wright                            const CeedScalar *const *in, CeedScalar *const *out) {
7288626eedSJames Wright   // Inputs
7388626eedSJames Wright   const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
7488626eedSJames Wright 
7588626eedSJames Wright   // Outputs
7688626eedSJames Wright   CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
7788626eedSJames Wright 
78*dc805cc4SLeila Ghaffari   // Context
79*dc805cc4SLeila Ghaffari   const ChannelContext context = (ChannelContext)ctx;
80*dc805cc4SLeila Ghaffari 
8188626eedSJames Wright   // Quadrature Point Loop
8288626eedSJames Wright   CeedPragmaSIMD
8388626eedSJames Wright   for (CeedInt i=0; i<Q; i++) {
8488626eedSJames Wright     const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]};
85*dc805cc4SLeila Ghaffari     State s = Exact_Channel(3, 0., x, 5, ctx);
86*dc805cc4SLeila Ghaffari     if (context->newtonian_ctx.primitive) {
87*dc805cc4SLeila Ghaffari       q0[0][i] = s.Y.pressure;
88*dc805cc4SLeila Ghaffari       for (CeedInt j=0; j<3; j++)
89*dc805cc4SLeila Ghaffari         q0[j+1][i] = s.Y.velocity[j];
90*dc805cc4SLeila Ghaffari       q0[4][i] = s.Y.temperature;
91*dc805cc4SLeila Ghaffari     } else {
92*dc805cc4SLeila Ghaffari       q0[0][i] = s.U.density;
93*dc805cc4SLeila Ghaffari       for (CeedInt j=0; j<3; j++)
94*dc805cc4SLeila Ghaffari         q0[j+1][i] = s.U.momentum[j];
95*dc805cc4SLeila Ghaffari       q0[4][i] = s.U.E_total;
96*dc805cc4SLeila Ghaffari     }
9788626eedSJames Wright 
9888626eedSJames Wright   } // End of Quadrature Point Loop
9988626eedSJames Wright   return 0;
10088626eedSJames Wright }
10188626eedSJames Wright 
10288626eedSJames Wright // *****************************************************************************
10388626eedSJames Wright CEED_QFUNCTION(Channel_Inflow)(void *ctx, CeedInt Q,
10488626eedSJames Wright                                const CeedScalar *const *in,
10588626eedSJames Wright                                CeedScalar *const *out) {
10688626eedSJames Wright   // *INDENT-OFF*
10788626eedSJames Wright   // Inputs
10888626eedSJames Wright   const CeedScalar (*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0],
109e8b03feeSJames Wright                    (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
110e8b03feeSJames Wright                    (*X)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
11188626eedSJames Wright 
11288626eedSJames Wright   // Outputs
11388626eedSJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
11488626eedSJames Wright   // *INDENT-ON*
11588626eedSJames Wright   const ChannelContext context = (ChannelContext)ctx;
11688626eedSJames Wright   const bool implicit     = context->implicit;
11788626eedSJames Wright   const CeedScalar cv     = context->newtonian_ctx.cv;
11888626eedSJames Wright   const CeedScalar cp     = context->newtonian_ctx.cp;
11988626eedSJames Wright   const CeedScalar gamma  = cp/cv;
12088626eedSJames Wright 
12188626eedSJames Wright   CeedPragmaSIMD
12288626eedSJames Wright   // Quadrature Point Loop
12388626eedSJames Wright   for (CeedInt i=0; i<Q; i++) {
12488626eedSJames Wright     // Setup
12588626eedSJames Wright     // -- Interp-to-Interp q_data
12688626eedSJames Wright     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
12788626eedSJames Wright     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
12888626eedSJames Wright     // We can effect this by swapping the sign on this weight
12988626eedSJames Wright     const CeedScalar wdetJb  = (implicit ? -1. : 1.) * q_data_sur[0][i];
13088626eedSJames Wright 
13188626eedSJames Wright     // Calcualte prescribed inflow values
13288626eedSJames Wright     const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
133*dc805cc4SLeila Ghaffari     State s = Exact_Channel(3, 0., x, 5, ctx);
13488626eedSJames Wright     CeedScalar q_exact[5] = {0.};
135*dc805cc4SLeila Ghaffari     q_exact[0] = s.U.density;
136*dc805cc4SLeila Ghaffari     for (CeedInt j=0; j<3; j++)
137*dc805cc4SLeila Ghaffari       q_exact[j+1] = s.U.momentum[j];
138*dc805cc4SLeila Ghaffari     q_exact[4] = s.U.E_total;
13913fa47b2SJames Wright     const CeedScalar E_kinetic_exact = 0.5*Dot3(&q_exact[1], &q_exact[1])
14013fa47b2SJames Wright                                        / q_exact[0];
14188626eedSJames Wright     const CeedScalar velocity[3] = {q_exact[1]/q_exact[0],
14288626eedSJames Wright                                     q_exact[2]/q_exact[0],
14388626eedSJames Wright                                     q_exact[3]/q_exact[0]
14488626eedSJames Wright                                    };
14588626eedSJames Wright     const CeedScalar theta = (q_exact[4] - E_kinetic_exact) / (q_exact[0]*cv);
14688626eedSJames Wright 
14788626eedSJames Wright     // Find pressure using state inside the domain
14888626eedSJames Wright     const CeedScalar rho = q[0][i];
14988626eedSJames Wright     const CeedScalar u[3] = {q[1][i]/rho, q[2][i]/rho, q[3][i]/rho};
15013fa47b2SJames Wright     const CeedScalar E_internal = q[4][i] - .5 * rho * Dot3(u,u);
15188626eedSJames Wright     const CeedScalar P = E_internal * (gamma - 1.);
15288626eedSJames Wright 
15388626eedSJames Wright     // Find inflow state using calculated P and prescribed velocity, theta0
15488626eedSJames Wright     const CeedScalar e_internal = cv * theta;
15588626eedSJames Wright     const CeedScalar rho_in = P / ((gamma - 1) * e_internal);
15613fa47b2SJames Wright     const CeedScalar E_kinetic = .5 * rho_in * Dot3(velocity, velocity);
15788626eedSJames Wright     const CeedScalar E = rho_in * e_internal + E_kinetic;
15888626eedSJames Wright     // ---- Normal vect
15988626eedSJames Wright     const CeedScalar norm[3] = {q_data_sur[1][i],
16088626eedSJames Wright                                 q_data_sur[2][i],
16188626eedSJames Wright                                 q_data_sur[3][i]
16288626eedSJames Wright                                };
16388626eedSJames Wright 
16488626eedSJames Wright     // The Physics
16588626eedSJames Wright     // Zero v so all future terms can safely sum into it
166ba6664aeSJames Wright     for (CeedInt j=0; j<5; j++) v[j][i] = 0.;
16788626eedSJames Wright 
16813fa47b2SJames Wright     const CeedScalar u_normal = Dot3(norm, velocity);
16988626eedSJames Wright 
17088626eedSJames Wright     // The Physics
17188626eedSJames Wright     // -- Density
17288626eedSJames Wright     v[0][i] -= wdetJb * rho_in * u_normal;
17388626eedSJames Wright 
17488626eedSJames Wright     // -- Momentum
175ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++)
17688626eedSJames Wright       v[j+1][i] -= wdetJb * (rho_in * u_normal * velocity[j] +
17788626eedSJames Wright                              norm[j] * P);
17888626eedSJames Wright 
17988626eedSJames Wright     // -- Total Energy Density
18088626eedSJames Wright     v[4][i] -= wdetJb * u_normal * (E + P);
18188626eedSJames Wright 
18288626eedSJames Wright   } // End Quadrature Point Loop
18388626eedSJames Wright   return 0;
18488626eedSJames Wright }
18588626eedSJames Wright 
18688626eedSJames Wright // *****************************************************************************
18788626eedSJames Wright CEED_QFUNCTION(Channel_Outflow)(void *ctx, CeedInt Q,
18888626eedSJames Wright                                 const CeedScalar *const *in,
18988626eedSJames Wright                                 CeedScalar *const *out) {
19088626eedSJames Wright   // *INDENT-OFF*
19188626eedSJames Wright   // Inputs
19288626eedSJames Wright   const CeedScalar (*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0],
193e8b03feeSJames Wright                    (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
194e8b03feeSJames Wright 
19588626eedSJames Wright   // Outputs
19688626eedSJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
19788626eedSJames Wright   // *INDENT-ON*
19888626eedSJames Wright 
19988626eedSJames Wright   const ChannelContext context = (ChannelContext)ctx;
20088626eedSJames Wright   const bool implicit     = context->implicit;
20188626eedSJames Wright   const CeedScalar P0     = context->P0;
20288626eedSJames Wright 
20388626eedSJames Wright   CeedPragmaSIMD
20488626eedSJames Wright   // Quadrature Point Loop
20588626eedSJames Wright   for (CeedInt i=0; i<Q; i++) {
20688626eedSJames Wright     // Setup
20788626eedSJames Wright     // -- Interp in
20888626eedSJames Wright     const CeedScalar rho      =  q[0][i];
20988626eedSJames Wright     const CeedScalar u[3]     = {q[1][i] / rho,
21088626eedSJames Wright                                  q[2][i] / rho,
21188626eedSJames Wright                                  q[3][i] / rho
21288626eedSJames Wright                                 };
21388626eedSJames Wright     const CeedScalar E        =  q[4][i];
21488626eedSJames Wright 
21588626eedSJames Wright     // -- Interp-to-Interp q_data
21688626eedSJames Wright     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
21788626eedSJames Wright     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
21888626eedSJames Wright     // We can effect this by swapping the sign on this weight
21988626eedSJames Wright     const CeedScalar wdetJb  = (implicit ? -1. : 1.) * q_data_sur[0][i];
22088626eedSJames Wright 
22188626eedSJames Wright     // ---- Normal vect
22288626eedSJames Wright     const CeedScalar norm[3] = {q_data_sur[1][i],
22388626eedSJames Wright                                 q_data_sur[2][i],
22488626eedSJames Wright                                 q_data_sur[3][i]
22588626eedSJames Wright                                };
22688626eedSJames Wright 
22788626eedSJames Wright     // The Physics
22888626eedSJames Wright     // Zero v so all future terms can safely sum into it
229ba6664aeSJames Wright     for (CeedInt j=0; j<5; j++) v[j][i] = 0.;
23088626eedSJames Wright 
23188626eedSJames Wright     // Implementing outflow condition
23288626eedSJames Wright     const CeedScalar P         = P0; // pressure
23313fa47b2SJames Wright     const CeedScalar u_normal  = Dot3(norm, u); // Normal velocity
23488626eedSJames Wright     // The Physics
23588626eedSJames Wright     // -- Density
23688626eedSJames Wright     v[0][i] -= wdetJb * rho * u_normal;
23788626eedSJames Wright 
23888626eedSJames Wright     // -- Momentum
239ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++)
24088626eedSJames Wright       v[j+1][i] -= wdetJb *(rho * u_normal * u[j] + norm[j] * P);
24188626eedSJames Wright 
24288626eedSJames Wright     // -- Total Energy Density
24388626eedSJames Wright     v[4][i] -= wdetJb * u_normal * (E + P);
24488626eedSJames Wright 
24588626eedSJames Wright   } // End Quadrature Point Loop
24688626eedSJames Wright   return 0;
24788626eedSJames Wright }
248*dc805cc4SLeila Ghaffari 
249*dc805cc4SLeila Ghaffari // *****************************************************************************
25088626eedSJames Wright #endif // channel_h
251