xref: /libCEED/examples/fluids/qfunctions/blasius.h (revision ff9b3c0e2ebd8a09dedd1c00be3d2c5b29de65cc)
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
1088626eedSJames Wright #include <ceed.h>
112b730f8bSJeremy L Thompson 
122518f336SLeila Ghaffari #include "newtonian_state.h"
13841e4c73SJed Brown #include "newtonian_types.h"
1413fa47b2SJames Wright #include "utils.h"
1588626eedSJames Wright 
1607d14e58SLeila Ghaffari #define BLASIUS_MAX_N_CHEBYSHEV 50
1707d14e58SLeila Ghaffari 
1888626eedSJames Wright typedef struct BlasiusContext_ *BlasiusContext;
1988626eedSJames Wright struct BlasiusContext_ {
2088626eedSJames Wright   bool                             implicit;  // !< Using implicit timesteping or not
21871db79fSKenneth E. Jansen   bool                             weakT;     // !< flag to set Temperature weakly at inflow
2288626eedSJames Wright   CeedScalar                       delta0;    // !< Boundary layer height at inflow
23*ff9b3c0eSJames Wright   State                            S_infty;
242518f336SLeila Ghaffari   CeedScalar                       T_wall;                                // !< Temperature at the wall
25f1122ed0SJames Wright   CeedScalar                       x_inflow;                              // !< Location of inflow in x
262518f336SLeila Ghaffari   CeedScalar                       n_cheb;                                // !< Number of Chebyshev terms
2707d14e58SLeila Ghaffari   CeedScalar                      *X;                                     // !< Chebyshev polynomial coordinate vector (CPU only)
282518f336SLeila Ghaffari   CeedScalar                       eta_max;                               // !< Maximum eta in the domain
2907d14e58SLeila Ghaffari   CeedScalar                       Tf_cheb[BLASIUS_MAX_N_CHEBYSHEV];      // !< Chebyshev coefficient for f
3007d14e58SLeila Ghaffari   CeedScalar                       Th_cheb[BLASIUS_MAX_N_CHEBYSHEV - 1];  // !< Chebyshev coefficient for h
3188626eedSJames Wright   struct NewtonianIdealGasContext_ newtonian_ctx;
3288626eedSJames Wright };
3388626eedSJames Wright 
342518f336SLeila Ghaffari // *****************************************************************************
35ea61e9acSJeremy L Thompson // This helper function evaluates Chebyshev polynomials with a set of coefficients with all their derivatives represented as a recurrence table.
362518f336SLeila Ghaffari // *****************************************************************************
372b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ChebyshevEval(int N, const double *Tf, double x, double eta_max, double *f) {
382518f336SLeila Ghaffari   double dX_deta     = 2 / eta_max;
392518f336SLeila Ghaffari   double table[4][3] = {
402518f336SLeila Ghaffari   // Chebyshev polynomials T_0, T_1, T_2 of the first kind in (-1,1)
412b730f8bSJeremy L Thompson       {1, x, 2 * x * x - 1},
422b730f8bSJeremy L Thompson       {0, 1, 4 * x        },
432b730f8bSJeremy L Thompson       {0, 0, 4            },
442b730f8bSJeremy L Thompson       {0, 0, 0            }
452518f336SLeila Ghaffari   };
462518f336SLeila Ghaffari   for (int i = 0; i < 4; i++) {
472518f336SLeila Ghaffari     // i-th derivative of f
482518f336SLeila Ghaffari     f[i] = table[i][0] * Tf[0] + table[i][1] * Tf[1] + table[i][2] * Tf[2];
492518f336SLeila Ghaffari   }
502518f336SLeila Ghaffari   for (int i = 3; i < N; i++) {
512518f336SLeila Ghaffari     // T_n(x) = 2xT_{n-1}(x) - T_{n-2}(x)
522518f336SLeila Ghaffari     table[0][i % 3] = 2 * x * table[0][(i - 1) % 3] - table[0][(i - 2) % 3];
532518f336SLeila Ghaffari     // Differentiate Chebyshev polynomials with the recurrence relation
542518f336SLeila Ghaffari     for (int j = 1; j < 4; j++) {
552518f336SLeila Ghaffari       // T'_{n}(x)/n = 2T_{n-1}(x) + T'_{n-2}(x)/n-2
562518f336SLeila Ghaffari       table[j][i % 3] = i * (2 * table[j - 1][(i - 1) % 3] + table[j][(i - 2) % 3] / (i - 2));
572518f336SLeila Ghaffari     }
582518f336SLeila Ghaffari     for (int j = 0; j < 4; j++) {
592518f336SLeila Ghaffari       f[j] += table[j][i % 3] * Tf[i];
6088626eedSJames Wright     }
6188626eedSJames Wright   }
622518f336SLeila Ghaffari   for (int i = 1; i < 4; i++) {
632518f336SLeila Ghaffari     // Transform derivatives from Chebyshev [-1, 1] to [0, eta_max].
642518f336SLeila Ghaffari     for (int j = 0; j < i; j++) f[i] *= dX_deta;
652518f336SLeila Ghaffari   }
6688626eedSJames Wright }
6788626eedSJames Wright 
682518f336SLeila Ghaffari // *****************************************************************************
692518f336SLeila Ghaffari // This helper function computes the Blasius boundary layer solution.
702518f336SLeila Ghaffari // *****************************************************************************
712b730f8bSJeremy L Thompson State CEED_QFUNCTION_HELPER(BlasiusSolution)(const BlasiusContext blasius, const CeedScalar x[3], const CeedScalar x0, const CeedScalar x_inflow,
7207d14e58SLeila Ghaffari                                              const CeedScalar rho_infty, CeedScalar *t12) {
732518f336SLeila Ghaffari   CeedInt    N       = blasius->n_cheb;
7407d14e58SLeila Ghaffari   CeedScalar mu      = blasius->newtonian_ctx.mu;
75*ff9b3c0eSJames Wright   State      S_infty = blasius->S_infty;
7607d14e58SLeila Ghaffari   CeedScalar nu      = mu / rho_infty;
77*ff9b3c0eSJames Wright   CeedScalar U_infty = sqrt(Dot3(S_infty.Y.velocity, S_infty.Y.velocity));
78*ff9b3c0eSJames Wright   CeedScalar eta     = x[1] * sqrt(U_infty / (nu * (x0 + x[0] - x_inflow)));
792518f336SLeila Ghaffari   CeedScalar X       = 2 * (eta / blasius->eta_max) - 1.;
802518f336SLeila Ghaffari   CeedScalar Rd      = GasConstant(&blasius->newtonian_ctx);
812518f336SLeila Ghaffari 
822518f336SLeila Ghaffari   CeedScalar f[4], h[4];
832518f336SLeila Ghaffari   ChebyshevEval(N, blasius->Tf_cheb, X, blasius->eta_max, f);
842518f336SLeila Ghaffari   ChebyshevEval(N - 1, blasius->Th_cheb, X, blasius->eta_max, h);
852518f336SLeila Ghaffari 
86*ff9b3c0eSJames Wright   *t12 = mu * U_infty * f[2] * sqrt(U_infty / (nu * (x0 + x[0] - x_inflow)));
872518f336SLeila Ghaffari 
882518f336SLeila Ghaffari   CeedScalar Y[5];
89*ff9b3c0eSJames Wright   Y[1] = U_infty * f[1];
90*ff9b3c0eSJames Wright   Y[2] = 0.5 * sqrt(nu * U_infty / (x0 + x[0] - x_inflow)) * (eta * f[1] - f[0]);
912518f336SLeila Ghaffari   Y[3] = 0.;
92*ff9b3c0eSJames Wright   Y[4] = S_infty.Y.temperature * h[0];
9307d14e58SLeila Ghaffari   Y[0] = rho_infty / h[0] * Rd * Y[4];
943bd61617SKenneth E. Jansen   return StateFromY(&blasius->newtonian_ctx, Y);
9588626eedSJames Wright }
9688626eedSJames Wright 
9788626eedSJames Wright // *****************************************************************************
9888626eedSJames Wright // This QFunction sets a Blasius boundary layer for the initial condition
9988626eedSJames Wright // *****************************************************************************
1002b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsBlasius)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
10188626eedSJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
10288626eedSJames Wright   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
10388626eedSJames Wright 
10488626eedSJames Wright   const BlasiusContext           context  = (BlasiusContext)ctx;
105912a746fSJames Wright   const NewtonianIdealGasContext gas      = &context->newtonian_ctx;
10688626eedSJames Wright   const CeedScalar               mu       = context->newtonian_ctx.mu;
10788626eedSJames Wright   const CeedScalar               delta0   = context->delta0;
108f1122ed0SJames Wright   const CeedScalar               x_inflow = context->x_inflow;
1092518f336SLeila Ghaffari   CeedScalar                     t12;
11088626eedSJames Wright 
111*ff9b3c0eSJames Wright   const State      S_infty = context->S_infty;
112*ff9b3c0eSJames Wright   const CeedScalar U_infty = sqrt(Dot3(S_infty.Y.velocity, S_infty.Y.velocity));
11388626eedSJames Wright 
114*ff9b3c0eSJames Wright   const CeedScalar x0 = U_infty * S_infty.U.density / (mu * 25 / Square(delta0));
115912a746fSJames Wright 
116912a746fSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
117912a746fSJames Wright     const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
118*ff9b3c0eSJames Wright     State            s    = BlasiusSolution(context, x, x0, x_inflow, S_infty.U.density, &t12);
119912a746fSJames Wright     CeedScalar       q[5] = {0};
120912a746fSJames Wright 
121912a746fSJames Wright     switch (gas->state_var) {
122912a746fSJames Wright       case STATEVAR_CONSERVATIVE:
123912a746fSJames Wright         UnpackState_U(s.U, q);
124912a746fSJames Wright         break;
125912a746fSJames Wright       case STATEVAR_PRIMITIVE:
126912a746fSJames Wright         UnpackState_Y(s.Y, q);
127912a746fSJames Wright         break;
128912a746fSJames Wright     }
129912a746fSJames Wright     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
130912a746fSJames Wright   }
13188626eedSJames Wright   return 0;
13288626eedSJames Wright }
13388626eedSJames Wright 
13488626eedSJames Wright // *****************************************************************************
1352b730f8bSJeremy L Thompson CEED_QFUNCTION(Blasius_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
136f21e6b1cSJames Wright   const BlasiusContext context     = (BlasiusContext)ctx;
13746603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
138f3e15844SJames Wright   const CeedScalar(*q_data_sur)    = in[2];
13946603fc5SJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
14088626eedSJames Wright   CeedScalar(*v)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
141f21e6b1cSJames Wright   CeedScalar(*jac_data_sur)        = context->newtonian_ctx.is_implicit ? out[1] : NULL;
14246603fc5SJames Wright 
143f3e15844SJames Wright   const bool                     is_implicit = context->implicit;
1444c0e8230SJames Wright   const NewtonianIdealGasContext gas         = &context->newtonian_ctx;
145*ff9b3c0eSJames Wright   State                          S_infty     = context->S_infty;
146*ff9b3c0eSJames Wright   const CeedScalar               rho_0       = S_infty.U.density;
147*ff9b3c0eSJames Wright   const CeedScalar               U_infty     = sqrt(Dot3(S_infty.Y.velocity, S_infty.Y.velocity));
148*ff9b3c0eSJames Wright   const CeedScalar               x0          = U_infty * rho_0 / (gas->mu * 25 / Square(context->delta0));
14929ea4e10SJames Wright   const CeedScalar               zeros[11]   = {0.};
15088626eedSJames Wright 
15146603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
152f3e15844SJames Wright     CeedScalar wdetJb, norm[3];
153f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
154f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
15588626eedSJames Wright 
156871db79fSKenneth E. Jansen     // Calculate inflow values
1572518f336SLeila Ghaffari     const CeedScalar x[3] = {X[0][i], X[1][i], 0.};
15888626eedSJames Wright     CeedScalar       t12;
1594c0e8230SJames Wright     State            s = BlasiusSolution(context, x, x0, context->x_inflow, rho_0, &t12);
16007d14e58SLeila Ghaffari     CeedScalar       qi[5];
16107d14e58SLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) qi[j] = q[j][i];
1623bd61617SKenneth E. Jansen     State s_int = StateFromU(gas, qi);
16388626eedSJames Wright 
164871db79fSKenneth E. Jansen     // enabling user to choose between weak T and weak rho inflow
1654c0e8230SJames Wright     if (context->weakT) {  // density from the current solution
16607d14e58SLeila Ghaffari       s.U.density = s_int.U.density;
1673bd61617SKenneth E. Jansen       s.Y         = StatePrimitiveFromConservative(gas, s.U);
16807d14e58SLeila Ghaffari     } else {  // Total energy from current solution
16907d14e58SLeila Ghaffari       s.U.E_total = s_int.U.E_total;
1703bd61617SKenneth E. Jansen       s.Y         = StatePrimitiveFromConservative(gas, s.U);
171871db79fSKenneth E. Jansen     }
17207d14e58SLeila Ghaffari 
17307d14e58SLeila Ghaffari     StateConservative Flux_inviscid[3];
17407d14e58SLeila Ghaffari     FluxInviscid(&context->newtonian_ctx, s, Flux_inviscid);
17588626eedSJames Wright 
1762b730f8bSJeremy L Thompson     const CeedScalar stress[3][3] = {
1772b730f8bSJeremy L Thompson         {0,   t12, 0},
1782b730f8bSJeremy L Thompson         {t12, 0,   0},
1792b730f8bSJeremy L Thompson         {0,   0,   0}
1802b730f8bSJeremy L Thompson     };
18107d14e58SLeila Ghaffari     const CeedScalar Fe[3] = {0};  // TODO: viscous energy flux needs grad temperature
18207d14e58SLeila Ghaffari     CeedScalar       Flux[5];
18307d14e58SLeila Ghaffari     FluxTotal_Boundary(Flux_inviscid, stress, Fe, norm, Flux);
1842b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
185f21e6b1cSJames Wright     if (is_implicit) StoredValuesPack(Q, i, 0, 11, zeros, jac_data_sur);
186f3e15844SJames Wright   }
18788626eedSJames Wright   return 0;
18888626eedSJames Wright }
18988626eedSJames Wright 
1902518f336SLeila Ghaffari // *****************************************************************************
1912b730f8bSJeremy L Thompson CEED_QFUNCTION(Blasius_Inflow_Jacobian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
19246603fc5SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
193f3e15844SJames Wright   const CeedScalar(*q_data_sur)     = in[2];
19446603fc5SJames Wright   const CeedScalar(*X)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[3];
195e334ad8fSJed Brown   CeedScalar(*v)[CEED_Q_VLA]        = (CeedScalar(*)[CEED_Q_VLA])out[0];
19646603fc5SJames Wright 
197e334ad8fSJed Brown   const BlasiusContext           context     = (BlasiusContext)ctx;
1984c0e8230SJames Wright   const NewtonianIdealGasContext gas         = &context->newtonian_ctx;
199f3e15844SJames Wright   const bool                     is_implicit = context->implicit;
2004c0e8230SJames Wright   const CeedScalar               Rd          = GasConstant(gas);
2014c0e8230SJames Wright   const CeedScalar               gamma       = HeatCapacityRatio(gas);
202*ff9b3c0eSJames Wright   const State                    S_infty     = context->S_infty;
203*ff9b3c0eSJames Wright   const CeedScalar               rho_0       = S_infty.U.density;
204*ff9b3c0eSJames Wright   const CeedScalar               U_infty     = sqrt(Dot3(S_infty.Y.velocity, S_infty.Y.velocity));
205*ff9b3c0eSJames Wright   const CeedScalar               x0          = U_infty * rho_0 / (gas->mu * 25 / Square(context->delta0));
206e334ad8fSJed Brown 
20746603fc5SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
208f3e15844SJames Wright     CeedScalar wdetJb, norm[3];
209f3e15844SJames Wright     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
210f3e15844SJames Wright     wdetJb *= is_implicit ? -1. : 1.;
211e334ad8fSJed Brown 
212e334ad8fSJed Brown     // Calculate inflow values
21307d14e58SLeila Ghaffari     const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
214e334ad8fSJed Brown     CeedScalar       t12;
2152518f336SLeila Ghaffari     State            s = BlasiusSolution(context, x, x0, 0, rho_0, &t12);
216e334ad8fSJed Brown 
217e334ad8fSJed Brown     // enabling user to choose between weak T and weak rho inflow
218e334ad8fSJed Brown     CeedScalar drho, dE, dP;
2194c0e8230SJames Wright     if (context->weakT) {
220e334ad8fSJed Brown       // rho should be from the current solution
221e334ad8fSJed Brown       drho                   = dq[0][i];
222*ff9b3c0eSJames Wright       CeedScalar dE_internal = drho * gas->cv * S_infty.Y.temperature;
2232518f336SLeila Ghaffari       CeedScalar dE_kinetic  = .5 * drho * Dot3(s.Y.velocity, s.Y.velocity);
224e334ad8fSJed Brown       dE                     = dE_internal + dE_kinetic;
225*ff9b3c0eSJames Wright       dP                     = drho * Rd * S_infty.Y.temperature;  // interior rho with exterior T
226*ff9b3c0eSJames Wright     } else {
227*ff9b3c0eSJames Wright       // rho specified, E_internal from solution
228e334ad8fSJed Brown       drho = 0;
229e334ad8fSJed Brown       dE   = dq[4][i];
230e334ad8fSJed Brown       dP   = dE * (gamma - 1.);
231e334ad8fSJed Brown     }
232e334ad8fSJed Brown 
2332518f336SLeila Ghaffari     const CeedScalar u_normal = Dot3(norm, s.Y.velocity);
234e334ad8fSJed Brown 
235e334ad8fSJed Brown     v[0][i] = -wdetJb * drho * u_normal;
2362b730f8bSJeremy L Thompson     for (int j = 0; j < 3; j++) {
2372518f336SLeila Ghaffari       v[j + 1][i] = -wdetJb * (drho * u_normal * s.Y.velocity[j] + norm[j] * dP);
2382b730f8bSJeremy L Thompson     }
239e334ad8fSJed Brown     v[4][i] = -wdetJb * u_normal * (dE + dP);
240f3e15844SJames Wright   }
241e334ad8fSJed Brown   return 0;
242e334ad8fSJed Brown }
243