xref: /honee/qfunctions/blasius.h (revision 04e40bb60650195adcc92556a3eb81ec7887ccc8)
1bb8a0c61SJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2bb8a0c61SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3bb8a0c61SJames Wright //
4bb8a0c61SJames Wright // SPDX-License-Identifier: BSD-2-Clause
5bb8a0c61SJames Wright //
6bb8a0c61SJames Wright // This file is part of CEED:  http://github.com/ceed
7bb8a0c61SJames Wright 
8bb8a0c61SJames Wright /// @file
9bb8a0c61SJames Wright /// Operator for Navier-Stokes example using PETSc
10bb8a0c61SJames Wright 
11bb8a0c61SJames Wright #ifndef blasius_h
12bb8a0c61SJames Wright #define blasius_h
13bb8a0c61SJames Wright 
14bb8a0c61SJames Wright #include <ceed.h>
152b916ea7SJeremy L Thompson 
16e0d1a4dfSLeila Ghaffari #include "newtonian_state.h"
1715a3537eSJed Brown #include "newtonian_types.h"
18704b8bbeSJames Wright #include "utils.h"
19bb8a0c61SJames Wright 
200d850f2eSLeila Ghaffari #define BLASIUS_MAX_N_CHEBYSHEV 50
210d850f2eSLeila Ghaffari 
22bb8a0c61SJames Wright typedef struct BlasiusContext_ *BlasiusContext;
23bb8a0c61SJames Wright struct BlasiusContext_ {
24bb8a0c61SJames Wright   bool                             implicit;                              // !< Using implicit timesteping or not
252acc7cbcSKenneth E. Jansen   bool                             weakT;                                 // !< flag to set Temperature weakly at inflow
26bb8a0c61SJames Wright   CeedScalar                       delta0;                                // !< Boundary layer height at inflow
27aef1eb53SLeila Ghaffari   CeedScalar                       U_inf;                                 // !< Velocity at boundary layer edge
28aef1eb53SLeila Ghaffari   CeedScalar                       T_inf;                                 // !< Temperature at boundary layer edge
29e0d1a4dfSLeila Ghaffari   CeedScalar                       T_wall;                                // !< Temperature at the wall
30bb8a0c61SJames Wright   CeedScalar                       P0;                                    // !< Pressure at outflow
31ef2c71fdSJames Wright   CeedScalar                       x_inflow;                              // !< Location of inflow in x
32e0d1a4dfSLeila Ghaffari   CeedScalar                       n_cheb;                                // !< Number of Chebyshev terms
330d850f2eSLeila Ghaffari   CeedScalar                      *X;                                     // !< Chebyshev polynomial coordinate vector (CPU only)
34e0d1a4dfSLeila Ghaffari   CeedScalar                       eta_max;                               // !< Maximum eta in the domain
350d850f2eSLeila Ghaffari   CeedScalar                       Tf_cheb[BLASIUS_MAX_N_CHEBYSHEV];      // !< Chebyshev coefficient for f
360d850f2eSLeila Ghaffari   CeedScalar                       Th_cheb[BLASIUS_MAX_N_CHEBYSHEV - 1];  // !< Chebyshev coefficient for h
37bb8a0c61SJames Wright   struct NewtonianIdealGasContext_ newtonian_ctx;
38bb8a0c61SJames Wright };
39bb8a0c61SJames Wright 
40e0d1a4dfSLeila Ghaffari // *****************************************************************************
41*04e40bb6SJeremy L Thompson // This helper function evaluates Chebyshev polynomials with a set of coefficients with all their derivatives represented as a recurrence table.
42e0d1a4dfSLeila Ghaffari // *****************************************************************************
432b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void ChebyshevEval(int N, const double *Tf, double x, double eta_max, double *f) {
44e0d1a4dfSLeila Ghaffari   double dX_deta     = 2 / eta_max;
45e0d1a4dfSLeila Ghaffari   double table[4][3] = {
46e0d1a4dfSLeila Ghaffari   // Chebyshev polynomials T_0, T_1, T_2 of the first kind in (-1,1)
472b916ea7SJeremy L Thompson       {1, x, 2 * x * x - 1},
482b916ea7SJeremy L Thompson       {0, 1, 4 * x        },
492b916ea7SJeremy L Thompson       {0, 0, 4            },
502b916ea7SJeremy L Thompson       {0, 0, 0            }
51e0d1a4dfSLeila Ghaffari   };
52e0d1a4dfSLeila Ghaffari   for (int i = 0; i < 4; i++) {
53e0d1a4dfSLeila Ghaffari     // i-th derivative of f
54e0d1a4dfSLeila Ghaffari     f[i] = table[i][0] * Tf[0] + table[i][1] * Tf[1] + table[i][2] * Tf[2];
55e0d1a4dfSLeila Ghaffari   }
56e0d1a4dfSLeila Ghaffari   for (int i = 3; i < N; i++) {
57e0d1a4dfSLeila Ghaffari     // T_n(x) = 2xT_{n-1}(x) - T_{n-2}(x)
58e0d1a4dfSLeila Ghaffari     table[0][i % 3] = 2 * x * table[0][(i - 1) % 3] - table[0][(i - 2) % 3];
59e0d1a4dfSLeila Ghaffari     // Differentiate Chebyshev polynomials with the recurrence relation
60e0d1a4dfSLeila Ghaffari     for (int j = 1; j < 4; j++) {
61e0d1a4dfSLeila Ghaffari       // T'_{n}(x)/n = 2T_{n-1}(x) + T'_{n-2}(x)/n-2
62e0d1a4dfSLeila Ghaffari       table[j][i % 3] = i * (2 * table[j - 1][(i - 1) % 3] + table[j][(i - 2) % 3] / (i - 2));
63e0d1a4dfSLeila Ghaffari     }
64e0d1a4dfSLeila Ghaffari     for (int j = 0; j < 4; j++) {
65e0d1a4dfSLeila Ghaffari       f[j] += table[j][i % 3] * Tf[i];
66bb8a0c61SJames Wright     }
67bb8a0c61SJames Wright   }
68e0d1a4dfSLeila Ghaffari   for (int i = 1; i < 4; i++) {
69e0d1a4dfSLeila Ghaffari     // Transform derivatives from Chebyshev [-1, 1] to [0, eta_max].
70e0d1a4dfSLeila Ghaffari     for (int j = 0; j < i; j++) f[i] *= dX_deta;
71e0d1a4dfSLeila Ghaffari   }
72bb8a0c61SJames Wright }
73bb8a0c61SJames Wright 
74e0d1a4dfSLeila Ghaffari // *****************************************************************************
75e0d1a4dfSLeila Ghaffari // This helper function computes the Blasius boundary layer solution.
76e0d1a4dfSLeila Ghaffari // *****************************************************************************
772b916ea7SJeremy L Thompson State CEED_QFUNCTION_HELPER(BlasiusSolution)(const BlasiusContext blasius, const CeedScalar x[3], const CeedScalar x0, const CeedScalar x_inflow,
780d850f2eSLeila Ghaffari                                              const CeedScalar rho_infty, CeedScalar *t12) {
79e0d1a4dfSLeila Ghaffari   CeedInt    N     = blasius->n_cheb;
800d850f2eSLeila Ghaffari   CeedScalar mu    = blasius->newtonian_ctx.mu;
810d850f2eSLeila Ghaffari   CeedScalar nu    = mu / rho_infty;
82aef1eb53SLeila Ghaffari   CeedScalar eta   = x[1] * sqrt(blasius->U_inf / (nu * (x0 + x[0] - x_inflow)));
83e0d1a4dfSLeila Ghaffari   CeedScalar X     = 2 * (eta / blasius->eta_max) - 1.;
84aef1eb53SLeila Ghaffari   CeedScalar U_inf = blasius->U_inf;
85e0d1a4dfSLeila Ghaffari   CeedScalar Rd    = GasConstant(&blasius->newtonian_ctx);
86e0d1a4dfSLeila Ghaffari 
87e0d1a4dfSLeila Ghaffari   CeedScalar f[4], h[4];
88e0d1a4dfSLeila Ghaffari   ChebyshevEval(N, blasius->Tf_cheb, X, blasius->eta_max, f);
89e0d1a4dfSLeila Ghaffari   ChebyshevEval(N - 1, blasius->Th_cheb, X, blasius->eta_max, h);
90e0d1a4dfSLeila Ghaffari 
910d850f2eSLeila Ghaffari   *t12 = mu * U_inf * f[2] * sqrt(U_inf / (nu * (x0 + x[0] - x_inflow)));
92e0d1a4dfSLeila Ghaffari 
93e0d1a4dfSLeila Ghaffari   CeedScalar Y[5];
94aef1eb53SLeila Ghaffari   Y[1] = U_inf * f[1];
95aef1eb53SLeila Ghaffari   Y[2] = 0.5 * sqrt(nu * U_inf / (x0 + x[0] - x_inflow)) * (eta * f[1] - f[0]);
96e0d1a4dfSLeila Ghaffari   Y[3] = 0.;
97aef1eb53SLeila Ghaffari   Y[4] = blasius->T_inf * h[0];
980d850f2eSLeila Ghaffari   Y[0] = rho_infty / h[0] * Rd * Y[4];
99e0d1a4dfSLeila Ghaffari   return StateFromY(&blasius->newtonian_ctx, Y, x);
100bb8a0c61SJames Wright }
101bb8a0c61SJames Wright 
102bb8a0c61SJames Wright // *****************************************************************************
103bb8a0c61SJames Wright // This QFunction sets a Blasius boundary layer for the initial condition
104bb8a0c61SJames Wright // *****************************************************************************
1052b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsBlasius)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
106bb8a0c61SJames Wright   // Inputs
107bb8a0c61SJames Wright   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
108bb8a0c61SJames Wright 
109bb8a0c61SJames Wright   // Outputs
110bb8a0c61SJames Wright   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
111bb8a0c61SJames Wright 
112bb8a0c61SJames Wright   const BlasiusContext context    = (BlasiusContext)ctx;
113bb8a0c61SJames Wright   const CeedScalar     cv         = context->newtonian_ctx.cv;
114bb8a0c61SJames Wright   const CeedScalar     mu         = context->newtonian_ctx.mu;
115aef1eb53SLeila Ghaffari   const CeedScalar     T_inf      = context->T_inf;
116bb8a0c61SJames Wright   const CeedScalar     P0         = context->P0;
117bb8a0c61SJames Wright   const CeedScalar     delta0     = context->delta0;
118aef1eb53SLeila Ghaffari   const CeedScalar     U_inf      = context->U_inf;
119ef2c71fdSJames Wright   const CeedScalar     x_inflow   = context->x_inflow;
120e0d1a4dfSLeila Ghaffari   const CeedScalar     gamma      = HeatCapacityRatio(&context->newtonian_ctx);
121aef1eb53SLeila Ghaffari   const CeedScalar     e_internal = cv * T_inf;
122bb8a0c61SJames Wright   const CeedScalar     rho        = P0 / ((gamma - 1) * e_internal);
123aef1eb53SLeila Ghaffari   const CeedScalar     x0         = U_inf * rho / (mu * 25 / (delta0 * delta0));
124e0d1a4dfSLeila Ghaffari   CeedScalar           t12;
125bb8a0c61SJames Wright 
126bb8a0c61SJames Wright   // Quadrature Point Loop
1272b916ea7SJeremy L Thompson   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
128e0d1a4dfSLeila Ghaffari     const CeedScalar x[3] = {X[0][i], X[1][i], 0.};
129e0d1a4dfSLeila Ghaffari     State            s    = BlasiusSolution(context, x, x0, x_inflow, rho, &t12);
130e0d1a4dfSLeila Ghaffari     CeedScalar       q[5] = {0};
131e0d1a4dfSLeila Ghaffari     UnpackState_U(s.U, q);
132e0d1a4dfSLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
133bb8a0c61SJames Wright 
134bb8a0c61SJames Wright   }  // End of Quadrature Point Loop
135bb8a0c61SJames Wright   return 0;
136bb8a0c61SJames Wright }
137bb8a0c61SJames Wright 
138bb8a0c61SJames Wright // *****************************************************************************
1392b916ea7SJeremy L Thompson CEED_QFUNCTION(Blasius_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
140bb8a0c61SJames Wright   // Inputs
1413d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1423d65b166SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
1433d65b166SJames Wright   const CeedScalar(*X)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
144bb8a0c61SJames Wright 
145bb8a0c61SJames Wright   // Outputs
146bb8a0c61SJames Wright   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
1473d65b166SJames Wright 
148bb8a0c61SJames Wright   const BlasiusContext     context  = (BlasiusContext)ctx;
149bb8a0c61SJames Wright   const bool               implicit = context->implicit;
1500d850f2eSLeila Ghaffari   NewtonianIdealGasContext gas      = &context->newtonian_ctx;
151bb8a0c61SJames Wright   const CeedScalar         mu       = context->newtonian_ctx.mu;
152e0d1a4dfSLeila Ghaffari   const CeedScalar         Rd       = GasConstant(&context->newtonian_ctx);
153aef1eb53SLeila Ghaffari   const CeedScalar         T_inf    = context->T_inf;
154bb8a0c61SJames Wright   const CeedScalar         P0       = context->P0;
155bb8a0c61SJames Wright   const CeedScalar         delta0   = context->delta0;
156aef1eb53SLeila Ghaffari   const CeedScalar         U_inf    = context->U_inf;
157ef2c71fdSJames Wright   const CeedScalar         x_inflow = context->x_inflow;
1582acc7cbcSKenneth E. Jansen   const bool               weakT    = context->weakT;
159aef1eb53SLeila Ghaffari   const CeedScalar         rho_0    = P0 / (Rd * T_inf);
160aef1eb53SLeila Ghaffari   const CeedScalar         x0       = U_inf * rho_0 / (mu * 25 / Square(delta0));
161bb8a0c61SJames Wright 
162bb8a0c61SJames Wright   // Quadrature Point Loop
1633d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
164bb8a0c61SJames Wright     // Setup
165bb8a0c61SJames Wright     // -- Interp-to-Interp q_data
166bb8a0c61SJames Wright     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
167bb8a0c61SJames Wright     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
168bb8a0c61SJames Wright     // We can effect this by swapping the sign on this weight
169bb8a0c61SJames Wright     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
170bb8a0c61SJames Wright 
1712acc7cbcSKenneth E. Jansen     // Calculate inflow values
172e0d1a4dfSLeila Ghaffari     const CeedScalar x[3] = {X[0][i], X[1][i], 0.};
173bb8a0c61SJames Wright     CeedScalar       t12;
174e0d1a4dfSLeila Ghaffari     State            s = BlasiusSolution(context, x, x0, x_inflow, rho_0, &t12);
1750d850f2eSLeila Ghaffari     CeedScalar       qi[5];
1760d850f2eSLeila Ghaffari     for (CeedInt j = 0; j < 5; j++) qi[j] = q[j][i];
1770d850f2eSLeila Ghaffari     State s_int = StateFromU(gas, qi, x);
178bb8a0c61SJames Wright 
1792acc7cbcSKenneth E. Jansen     // enabling user to choose between weak T and weak rho inflow
1800d850f2eSLeila Ghaffari     if (weakT) {  // density from the current solution
1810d850f2eSLeila Ghaffari       s.U.density = s_int.U.density;
1820d850f2eSLeila Ghaffari       s.Y         = StatePrimitiveFromConservative(gas, s.U, x);
1830d850f2eSLeila Ghaffari     } else {  // Total energy from current solution
1840d850f2eSLeila Ghaffari       s.U.E_total = s_int.U.E_total;
1850d850f2eSLeila Ghaffari       s.Y         = StatePrimitiveFromConservative(gas, s.U, x);
1862acc7cbcSKenneth E. Jansen     }
1870d850f2eSLeila Ghaffari 
188bb8a0c61SJames Wright     // ---- Normal vect
1892b916ea7SJeremy L Thompson     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
190bb8a0c61SJames Wright 
1910d850f2eSLeila Ghaffari     StateConservative Flux_inviscid[3];
1920d850f2eSLeila Ghaffari     FluxInviscid(&context->newtonian_ctx, s, Flux_inviscid);
193bb8a0c61SJames Wright 
1942b916ea7SJeremy L Thompson     const CeedScalar stress[3][3] = {
1952b916ea7SJeremy L Thompson         {0,   t12, 0},
1962b916ea7SJeremy L Thompson         {t12, 0,   0},
1972b916ea7SJeremy L Thompson         {0,   0,   0}
1982b916ea7SJeremy L Thompson     };
1990d850f2eSLeila Ghaffari     const CeedScalar Fe[3] = {0};  // TODO: viscous energy flux needs grad temperature
2000d850f2eSLeila Ghaffari     CeedScalar       Flux[5];
2010d850f2eSLeila Ghaffari     FluxTotal_Boundary(Flux_inviscid, stress, Fe, norm, Flux);
2022b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
203bb8a0c61SJames Wright   }  // End Quadrature Point Loop
204bb8a0c61SJames Wright   return 0;
205bb8a0c61SJames Wright }
206bb8a0c61SJames Wright 
207e0d1a4dfSLeila Ghaffari // *****************************************************************************
2082b916ea7SJeremy L Thompson CEED_QFUNCTION(Blasius_Inflow_Jacobian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
209f0b65372SJed Brown   // Inputs
2103d65b166SJames Wright   const CeedScalar(*dq)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2113d65b166SJames Wright   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
2123d65b166SJames Wright   const CeedScalar(*X)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
213f0b65372SJed Brown 
214f0b65372SJed Brown   // Outputs
215f0b65372SJed Brown   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
2163d65b166SJames Wright 
217f0b65372SJed Brown   const BlasiusContext context  = (BlasiusContext)ctx;
218f0b65372SJed Brown   const bool           implicit = context->implicit;
219f0b65372SJed Brown   const CeedScalar     mu       = context->newtonian_ctx.mu;
220f0b65372SJed Brown   const CeedScalar     cv       = context->newtonian_ctx.cv;
221e0d1a4dfSLeila Ghaffari   const CeedScalar     Rd       = GasConstant(&context->newtonian_ctx);
222e0d1a4dfSLeila Ghaffari   const CeedScalar     gamma    = HeatCapacityRatio(&context->newtonian_ctx);
223aef1eb53SLeila Ghaffari   const CeedScalar     T_inf    = context->T_inf;
224f0b65372SJed Brown   const CeedScalar     P0       = context->P0;
225f0b65372SJed Brown   const CeedScalar     delta0   = context->delta0;
226aef1eb53SLeila Ghaffari   const CeedScalar     U_inf    = context->U_inf;
227f0b65372SJed Brown   const bool           weakT    = context->weakT;
228aef1eb53SLeila Ghaffari   const CeedScalar     rho_0    = P0 / (Rd * T_inf);
229aef1eb53SLeila Ghaffari   const CeedScalar     x0       = U_inf * rho_0 / (mu * 25 / (delta0 * delta0));
230f0b65372SJed Brown 
231f0b65372SJed Brown   // Quadrature Point Loop
2323d65b166SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
233f0b65372SJed Brown     // Setup
234f0b65372SJed Brown     // -- Interp-to-Interp q_data
235f0b65372SJed Brown     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
236f0b65372SJed Brown     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
237f0b65372SJed Brown     // We can effect this by swapping the sign on this weight
238f0b65372SJed Brown     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
239f0b65372SJed Brown 
240f0b65372SJed Brown     // Calculate inflow values
2410d850f2eSLeila Ghaffari     const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
242f0b65372SJed Brown     CeedScalar       t12;
243e0d1a4dfSLeila Ghaffari     State            s = BlasiusSolution(context, x, x0, 0, rho_0, &t12);
244f0b65372SJed Brown 
245f0b65372SJed Brown     // enabling user to choose between weak T and weak rho inflow
246f0b65372SJed Brown     CeedScalar drho, dE, dP;
247f0b65372SJed Brown     if (weakT) {
248f0b65372SJed Brown       // rho should be from the current solution
249f0b65372SJed Brown       drho                   = dq[0][i];
250aef1eb53SLeila Ghaffari       CeedScalar dE_internal = drho * cv * T_inf;
251e0d1a4dfSLeila Ghaffari       CeedScalar dE_kinetic  = .5 * drho * Dot3(s.Y.velocity, s.Y.velocity);
252f0b65372SJed Brown       dE                     = dE_internal + dE_kinetic;
253aef1eb53SLeila Ghaffari       dP                     = drho * Rd * T_inf;  // interior rho with exterior T
254f0b65372SJed Brown     } else {                                       // rho specified, E_internal from solution
255f0b65372SJed Brown       drho = 0;
256f0b65372SJed Brown       dE   = dq[4][i];
257f0b65372SJed Brown       dP   = dE * (gamma - 1.);
258f0b65372SJed Brown     }
2592b916ea7SJeremy L Thompson     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
260f0b65372SJed Brown 
261e0d1a4dfSLeila Ghaffari     const CeedScalar u_normal = Dot3(norm, s.Y.velocity);
262f0b65372SJed Brown 
263f0b65372SJed Brown     v[0][i] = -wdetJb * drho * u_normal;
2642b916ea7SJeremy L Thompson     for (int j = 0; j < 3; j++) {
265e0d1a4dfSLeila Ghaffari       v[j + 1][i] = -wdetJb * (drho * u_normal * s.Y.velocity[j] + norm[j] * dP);
2662b916ea7SJeremy L Thompson     }
267f0b65372SJed Brown     v[4][i] = -wdetJb * u_normal * (dE + dP);
268f0b65372SJed Brown   }  // End Quadrature Point Loop
269f0b65372SJed Brown   return 0;
270f0b65372SJed Brown }
271f0b65372SJed Brown 
272bb8a0c61SJames Wright #endif  // blasius_h
273