xref: /libCEED/examples/fluids/qfunctions/blasius.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 blasius_h
1288626eedSJames Wright #define blasius_h
1388626eedSJames Wright 
1488626eedSJames Wright #include <ceed.h>
152b730f8bSJeremy L Thompson 
162518f336SLeila Ghaffari #include "newtonian_state.h"
17841e4c73SJed Brown #include "newtonian_types.h"
1813fa47b2SJames Wright #include "utils.h"
1988626eedSJames Wright 
2007d14e58SLeila Ghaffari #define BLASIUS_MAX_N_CHEBYSHEV 50
2107d14e58SLeila Ghaffari 
2288626eedSJames Wright typedef struct BlasiusContext_ *BlasiusContext;
2388626eedSJames Wright struct BlasiusContext_ {
2488626eedSJames Wright   bool                             implicit;                              // !< Using implicit timesteping or not
25871db79fSKenneth E. Jansen   bool                             weakT;                                 // !< flag to set Temperature weakly at inflow
2688626eedSJames Wright   CeedScalar                       delta0;                                // !< Boundary layer height at inflow
27fb455ff0SLeila Ghaffari   CeedScalar                       U_inf;                                 // !< Velocity at boundary layer edge
28fb455ff0SLeila Ghaffari   CeedScalar                       T_inf;                                 // !< Temperature at boundary layer edge
292518f336SLeila Ghaffari   CeedScalar                       T_wall;                                // !< Temperature at the wall
3088626eedSJames Wright   CeedScalar                       P0;                                    // !< Pressure at outflow
31f1122ed0SJames Wright   CeedScalar                       x_inflow;                              // !< Location of inflow in x
322518f336SLeila Ghaffari   CeedScalar                       n_cheb;                                // !< Number of Chebyshev terms
3307d14e58SLeila Ghaffari   CeedScalar                      *X;                                     // !< Chebyshev polynomial coordinate vector (CPU only)
342518f336SLeila Ghaffari   CeedScalar                       eta_max;                               // !< Maximum eta in the domain
3507d14e58SLeila Ghaffari   CeedScalar                       Tf_cheb[BLASIUS_MAX_N_CHEBYSHEV];      // !< Chebyshev coefficient for f
3607d14e58SLeila Ghaffari   CeedScalar                       Th_cheb[BLASIUS_MAX_N_CHEBYSHEV - 1];  // !< Chebyshev coefficient for h
3788626eedSJames Wright   struct NewtonianIdealGasContext_ newtonian_ctx;
3888626eedSJames Wright };
3988626eedSJames Wright 
402518f336SLeila Ghaffari // *****************************************************************************
412518f336SLeila Ghaffari // This helper function evaluates Chebyshev polynomials with a set of
422518f336SLeila Ghaffari //  coefficients with all their derivatives represented as a recurrence table.
432518f336SLeila Ghaffari // *****************************************************************************
442b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ChebyshevEval(int N, const double *Tf, double x, double eta_max, double *f) {
452518f336SLeila Ghaffari   double dX_deta     = 2 / eta_max;
462518f336SLeila Ghaffari   double table[4][3] = {
472518f336SLeila Ghaffari   // Chebyshev polynomials T_0, T_1, T_2 of the first kind in (-1,1)
482b730f8bSJeremy L Thompson       {1, x, 2 * x * x - 1},
492b730f8bSJeremy L Thompson       {0, 1, 4 * x        },
502b730f8bSJeremy L Thompson       {0, 0, 4            },
512b730f8bSJeremy L Thompson       {0, 0, 0            }
522518f336SLeila Ghaffari   };
532518f336SLeila Ghaffari   for (int i = 0; i < 4; i++) {
542518f336SLeila Ghaffari     // i-th derivative of f
552518f336SLeila Ghaffari     f[i] = table[i][0] * Tf[0] + table[i][1] * Tf[1] + table[i][2] * Tf[2];
562518f336SLeila Ghaffari   }
572518f336SLeila Ghaffari   for (int i = 3; i < N; i++) {
582518f336SLeila Ghaffari     // T_n(x) = 2xT_{n-1}(x) - T_{n-2}(x)
592518f336SLeila Ghaffari     table[0][i % 3] = 2 * x * table[0][(i - 1) % 3] - table[0][(i - 2) % 3];
602518f336SLeila Ghaffari     // Differentiate Chebyshev polynomials with the recurrence relation
612518f336SLeila Ghaffari     for (int j = 1; j < 4; j++) {
622518f336SLeila Ghaffari       // T'_{n}(x)/n = 2T_{n-1}(x) + T'_{n-2}(x)/n-2
632518f336SLeila Ghaffari       table[j][i % 3] = i * (2 * table[j - 1][(i - 1) % 3] + table[j][(i - 2) % 3] / (i - 2));
642518f336SLeila Ghaffari     }
652518f336SLeila Ghaffari     for (int j = 0; j < 4; j++) {
662518f336SLeila Ghaffari       f[j] += table[j][i % 3] * Tf[i];
6788626eedSJames Wright     }
6888626eedSJames Wright   }
692518f336SLeila Ghaffari   for (int i = 1; i < 4; i++) {
702518f336SLeila Ghaffari     // Transform derivatives from Chebyshev [-1, 1] to [0, eta_max].
712518f336SLeila Ghaffari     for (int j = 0; j < i; j++) f[i] *= dX_deta;
722518f336SLeila Ghaffari   }
7388626eedSJames Wright }
7488626eedSJames Wright 
752518f336SLeila Ghaffari // *****************************************************************************
762518f336SLeila Ghaffari // This helper function computes the Blasius boundary layer solution.
772518f336SLeila Ghaffari // *****************************************************************************
782b730f8bSJeremy L Thompson State CEED_QFUNCTION_HELPER(BlasiusSolution)(const BlasiusContext blasius, const CeedScalar x[3], const CeedScalar x0, const CeedScalar x_inflow,
7907d14e58SLeila Ghaffari                                              const CeedScalar rho_infty, CeedScalar *t12) {
802518f336SLeila Ghaffari   CeedInt    N     = blasius->n_cheb;
8107d14e58SLeila Ghaffari   CeedScalar mu    = blasius->newtonian_ctx.mu;
8207d14e58SLeila Ghaffari   CeedScalar nu    = mu / rho_infty;
83fb455ff0SLeila Ghaffari   CeedScalar eta   = x[1] * sqrt(blasius->U_inf / (nu * (x0 + x[0] - x_inflow)));
842518f336SLeila Ghaffari   CeedScalar X     = 2 * (eta / blasius->eta_max) - 1.;
85fb455ff0SLeila Ghaffari   CeedScalar U_inf = blasius->U_inf;
862518f336SLeila Ghaffari   CeedScalar Rd    = GasConstant(&blasius->newtonian_ctx);
872518f336SLeila Ghaffari 
882518f336SLeila Ghaffari   CeedScalar f[4], h[4];
892518f336SLeila Ghaffari   ChebyshevEval(N, blasius->Tf_cheb, X, blasius->eta_max, f);
902518f336SLeila Ghaffari   ChebyshevEval(N - 1, blasius->Th_cheb, X, blasius->eta_max, h);
912518f336SLeila Ghaffari 
9207d14e58SLeila Ghaffari   *t12 = mu * U_inf * f[2] * sqrt(U_inf / (nu * (x0 + x[0] - x_inflow)));
932518f336SLeila Ghaffari 
942518f336SLeila Ghaffari   CeedScalar Y[5];
95fb455ff0SLeila Ghaffari   Y[1] = U_inf * f[1];
96fb455ff0SLeila Ghaffari   Y[2] = 0.5 * sqrt(nu * U_inf / (x0 + x[0] - x_inflow)) * (eta * f[1] - f[0]);
972518f336SLeila Ghaffari   Y[3] = 0.;
98fb455ff0SLeila Ghaffari   Y[4] = blasius->T_inf * h[0];
9907d14e58SLeila Ghaffari   Y[0] = rho_infty / h[0] * Rd * Y[4];
1002518f336SLeila Ghaffari   return StateFromY(&blasius->newtonian_ctx, Y, x);
10188626eedSJames Wright }
10288626eedSJames Wright 
10388626eedSJames Wright // *****************************************************************************
10488626eedSJames Wright // This QFunction sets a Blasius boundary layer for the initial condition
10588626eedSJames Wright // *****************************************************************************
1062b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsBlasius)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
10788626eedSJames Wright   // Inputs
10888626eedSJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
10988626eedSJames Wright 
11088626eedSJames Wright   // Outputs
11188626eedSJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
11288626eedSJames Wright 
11388626eedSJames Wright   const BlasiusContext context    = (BlasiusContext)ctx;
11488626eedSJames Wright   const CeedScalar     cv         = context->newtonian_ctx.cv;
11588626eedSJames Wright   const CeedScalar     mu         = context->newtonian_ctx.mu;
116fb455ff0SLeila Ghaffari   const CeedScalar     T_inf      = context->T_inf;
11788626eedSJames Wright   const CeedScalar     P0         = context->P0;
11888626eedSJames Wright   const CeedScalar     delta0     = context->delta0;
119fb455ff0SLeila Ghaffari   const CeedScalar     U_inf      = context->U_inf;
120f1122ed0SJames Wright   const CeedScalar     x_inflow   = context->x_inflow;
1212518f336SLeila Ghaffari   const CeedScalar     gamma      = HeatCapacityRatio(&context->newtonian_ctx);
122fb455ff0SLeila Ghaffari   const CeedScalar     e_internal = cv * T_inf;
12388626eedSJames Wright   const CeedScalar     rho        = P0 / ((gamma - 1) * e_internal);
124fb455ff0SLeila Ghaffari   const CeedScalar     x0         = U_inf * rho / (mu * 25 / (delta0 * delta0));
1252518f336SLeila Ghaffari   CeedScalar           t12;
12688626eedSJames Wright 
12788626eedSJames Wright   // Quadrature Point Loop
1282b730f8bSJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1292518f336SLeila Ghaffari     const CeedScalar x[3] = {X[0][i], X[1][i], 0.};
1302518f336SLeila Ghaffari     State            s    = BlasiusSolution(context, x, x0, x_inflow, rho, &t12);
1312518f336SLeila Ghaffari     CeedScalar       q[5] = {0};
1322518f336SLeila Ghaffari     UnpackState_U(s.U, q);
1332518f336SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
13488626eedSJames Wright 
13588626eedSJames Wright   }  // End of Quadrature Point Loop
13688626eedSJames Wright   return 0;
13788626eedSJames Wright }
13888626eedSJames Wright 
13988626eedSJames Wright // *****************************************************************************
1402b730f8bSJeremy L Thompson CEED_QFUNCTION(Blasius_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
14188626eedSJames Wright   // Inputs
142*46603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0];
143*46603fc5SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
144*46603fc5SJames Wright   const CeedScalar(*X)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
14588626eedSJames Wright 
14688626eedSJames Wright   // Outputs
14788626eedSJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
148*46603fc5SJames Wright 
14988626eedSJames Wright   const BlasiusContext     context  = (BlasiusContext)ctx;
15088626eedSJames Wright   const bool               implicit = context->implicit;
15107d14e58SLeila Ghaffari   NewtonianIdealGasContext gas      = &context->newtonian_ctx;
15288626eedSJames Wright   const CeedScalar         mu       = context->newtonian_ctx.mu;
1532518f336SLeila Ghaffari   const CeedScalar         Rd       = GasConstant(&context->newtonian_ctx);
154fb455ff0SLeila Ghaffari   const CeedScalar         T_inf    = context->T_inf;
15588626eedSJames Wright   const CeedScalar         P0       = context->P0;
15688626eedSJames Wright   const CeedScalar         delta0   = context->delta0;
157fb455ff0SLeila Ghaffari   const CeedScalar         U_inf    = context->U_inf;
158f1122ed0SJames Wright   const CeedScalar         x_inflow = context->x_inflow;
159871db79fSKenneth E. Jansen   const bool               weakT    = context->weakT;
160fb455ff0SLeila Ghaffari   const CeedScalar         rho_0    = P0 / (Rd * T_inf);
161fb455ff0SLeila Ghaffari   const CeedScalar         x0       = U_inf * rho_0 / (mu * 25 / Square(delta0));
16288626eedSJames Wright 
16388626eedSJames Wright   // Quadrature Point Loop
164*46603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
16588626eedSJames Wright     // Setup
16688626eedSJames Wright     // -- Interp-to-Interp q_data
16788626eedSJames Wright     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
16888626eedSJames Wright     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
16988626eedSJames Wright     // We can effect this by swapping the sign on this weight
17088626eedSJames Wright     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
17188626eedSJames Wright 
172871db79fSKenneth E. Jansen     // Calculate inflow values
1732518f336SLeila Ghaffari     const CeedScalar x[3] = {X[0][i], X[1][i], 0.};
17488626eedSJames Wright     CeedScalar       t12;
1752518f336SLeila Ghaffari     State            s = BlasiusSolution(context, x, x0, x_inflow, rho_0, &t12);
17607d14e58SLeila Ghaffari     CeedScalar       qi[5];
17707d14e58SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) qi[j] = q[j][i];
17807d14e58SLeila Ghaffari     State s_int = StateFromU(gas, qi, x);
17988626eedSJames Wright 
180871db79fSKenneth E. Jansen     // enabling user to choose between weak T and weak rho inflow
18107d14e58SLeila Ghaffari     if (weakT) {  // density from the current solution
18207d14e58SLeila Ghaffari       s.U.density = s_int.U.density;
18307d14e58SLeila Ghaffari       s.Y         = StatePrimitiveFromConservative(gas, s.U, x);
18407d14e58SLeila Ghaffari     } else {  // Total energy from current solution
18507d14e58SLeila Ghaffari       s.U.E_total = s_int.U.E_total;
18607d14e58SLeila Ghaffari       s.Y         = StatePrimitiveFromConservative(gas, s.U, x);
187871db79fSKenneth E. Jansen     }
18807d14e58SLeila Ghaffari 
18988626eedSJames Wright     // ---- Normal vect
1902b730f8bSJeremy L Thompson     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
19188626eedSJames Wright 
19207d14e58SLeila Ghaffari     StateConservative Flux_inviscid[3];
19307d14e58SLeila Ghaffari     FluxInviscid(&context->newtonian_ctx, s, Flux_inviscid);
19488626eedSJames Wright 
1952b730f8bSJeremy L Thompson     const CeedScalar stress[3][3] = {
1962b730f8bSJeremy L Thompson         {0,   t12, 0},
1972b730f8bSJeremy L Thompson         {t12, 0,   0},
1982b730f8bSJeremy L Thompson         {0,   0,   0}
1992b730f8bSJeremy L Thompson     };
20007d14e58SLeila Ghaffari     const CeedScalar Fe[3] = {0};  // TODO: viscous energy flux needs grad temperature
20107d14e58SLeila Ghaffari     CeedScalar       Flux[5];
20207d14e58SLeila Ghaffari     FluxTotal_Boundary(Flux_inviscid, stress, Fe, norm, Flux);
2032b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
20488626eedSJames Wright   }  // End Quadrature Point Loop
20588626eedSJames Wright   return 0;
20688626eedSJames Wright }
20788626eedSJames Wright 
2082518f336SLeila Ghaffari // *****************************************************************************
2092b730f8bSJeremy L Thompson CEED_QFUNCTION(Blasius_Inflow_Jacobian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
210e334ad8fSJed Brown   // Inputs
211*46603fc5SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
212*46603fc5SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
213*46603fc5SJames Wright   const CeedScalar(*X)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
214e334ad8fSJed Brown 
215e334ad8fSJed Brown   // Outputs
216e334ad8fSJed Brown   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
217*46603fc5SJames Wright 
218e334ad8fSJed Brown   const BlasiusContext context  = (BlasiusContext)ctx;
219e334ad8fSJed Brown   const bool           implicit = context->implicit;
220e334ad8fSJed Brown   const CeedScalar     mu       = context->newtonian_ctx.mu;
221e334ad8fSJed Brown   const CeedScalar     cv       = context->newtonian_ctx.cv;
2222518f336SLeila Ghaffari   const CeedScalar     Rd       = GasConstant(&context->newtonian_ctx);
2232518f336SLeila Ghaffari   const CeedScalar     gamma    = HeatCapacityRatio(&context->newtonian_ctx);
224fb455ff0SLeila Ghaffari   const CeedScalar     T_inf    = context->T_inf;
225e334ad8fSJed Brown   const CeedScalar     P0       = context->P0;
226e334ad8fSJed Brown   const CeedScalar     delta0   = context->delta0;
227fb455ff0SLeila Ghaffari   const CeedScalar     U_inf    = context->U_inf;
228e334ad8fSJed Brown   const bool           weakT    = context->weakT;
229fb455ff0SLeila Ghaffari   const CeedScalar     rho_0    = P0 / (Rd * T_inf);
230fb455ff0SLeila Ghaffari   const CeedScalar     x0       = U_inf * rho_0 / (mu * 25 / (delta0 * delta0));
231e334ad8fSJed Brown 
232e334ad8fSJed Brown   // Quadrature Point Loop
233*46603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
234e334ad8fSJed Brown     // Setup
235e334ad8fSJed Brown     // -- Interp-to-Interp q_data
236e334ad8fSJed Brown     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
237e334ad8fSJed Brown     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
238e334ad8fSJed Brown     // We can effect this by swapping the sign on this weight
239e334ad8fSJed Brown     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
240e334ad8fSJed Brown 
241e334ad8fSJed Brown     // Calculate inflow values
24207d14e58SLeila Ghaffari     const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
243e334ad8fSJed Brown     CeedScalar       t12;
2442518f336SLeila Ghaffari     State            s = BlasiusSolution(context, x, x0, 0, rho_0, &t12);
245e334ad8fSJed Brown 
246e334ad8fSJed Brown     // enabling user to choose between weak T and weak rho inflow
247e334ad8fSJed Brown     CeedScalar drho, dE, dP;
248e334ad8fSJed Brown     if (weakT) {
249e334ad8fSJed Brown       // rho should be from the current solution
250e334ad8fSJed Brown       drho                   = dq[0][i];
251fb455ff0SLeila Ghaffari       CeedScalar dE_internal = drho * cv * T_inf;
2522518f336SLeila Ghaffari       CeedScalar dE_kinetic  = .5 * drho * Dot3(s.Y.velocity, s.Y.velocity);
253e334ad8fSJed Brown       dE                     = dE_internal + dE_kinetic;
254fb455ff0SLeila Ghaffari       dP                     = drho * Rd * T_inf;  // interior rho with exterior T
255e334ad8fSJed Brown     } else {                                       // rho specified, E_internal from solution
256e334ad8fSJed Brown       drho = 0;
257e334ad8fSJed Brown       dE   = dq[4][i];
258e334ad8fSJed Brown       dP   = dE * (gamma - 1.);
259e334ad8fSJed Brown     }
2602b730f8bSJeremy L Thompson     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
261e334ad8fSJed Brown 
2622518f336SLeila Ghaffari     const CeedScalar u_normal = Dot3(norm, s.Y.velocity);
263e334ad8fSJed Brown 
264e334ad8fSJed Brown     v[0][i] = -wdetJb * drho * u_normal;
2652b730f8bSJeremy L Thompson     for (int j = 0; j < 3; j++) {
2662518f336SLeila Ghaffari       v[j + 1][i] = -wdetJb * (drho * u_normal * s.Y.velocity[j] + norm[j] * dP);
2672b730f8bSJeremy L Thompson     }
268e334ad8fSJed Brown     v[4][i] = -wdetJb * u_normal * (dE + dP);
269e334ad8fSJed Brown   }  // End Quadrature Point Loop
270e334ad8fSJed Brown   return 0;
271e334ad8fSJed Brown }
272e334ad8fSJed Brown 
27388626eedSJames Wright #endif  // blasius_h
274