xref: /honee/qfunctions/blasius.h (revision 2b916ea7fa53b5c2584160b9274b1b14ca18ff4f)
1 // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 /// @file
9 /// Operator for Navier-Stokes example using PETSc
10 
11 #ifndef blasius_h
12 #define blasius_h
13 
14 #include <ceed.h>
15 
16 #include "newtonian_state.h"
17 #include "newtonian_types.h"
18 #include "utils.h"
19 
20 #define BLASIUS_MAX_N_CHEBYSHEV 50
21 
22 typedef struct BlasiusContext_ *BlasiusContext;
23 struct BlasiusContext_ {
24   bool                             implicit;                              // !< Using implicit timesteping or not
25   bool                             weakT;                                 // !< flag to set Temperature weakly at inflow
26   CeedScalar                       delta0;                                // !< Boundary layer height at inflow
27   CeedScalar                       U_inf;                                 // !< Velocity at boundary layer edge
28   CeedScalar                       T_inf;                                 // !< Temperature at boundary layer edge
29   CeedScalar                       T_wall;                                // !< Temperature at the wall
30   CeedScalar                       P0;                                    // !< Pressure at outflow
31   CeedScalar                       x_inflow;                              // !< Location of inflow in x
32   CeedScalar                       n_cheb;                                // !< Number of Chebyshev terms
33   CeedScalar                      *X;                                     // !< Chebyshev polynomial coordinate vector (CPU only)
34   CeedScalar                       eta_max;                               // !< Maximum eta in the domain
35   CeedScalar                       Tf_cheb[BLASIUS_MAX_N_CHEBYSHEV];      // !< Chebyshev coefficient for f
36   CeedScalar                       Th_cheb[BLASIUS_MAX_N_CHEBYSHEV - 1];  // !< Chebyshev coefficient for h
37   struct NewtonianIdealGasContext_ newtonian_ctx;
38 };
39 
40 // *****************************************************************************
41 // This helper function evaluates Chebyshev polynomials with a set of
42 //  coefficients with all their derivatives represented as a recurrence table.
43 // *****************************************************************************
44 CEED_QFUNCTION_HELPER void ChebyshevEval(int N, const double *Tf, double x, double eta_max, double *f) {
45   double dX_deta     = 2 / eta_max;
46   double table[4][3] = {
47   // Chebyshev polynomials T_0, T_1, T_2 of the first kind in (-1,1)
48       {1, x, 2 * x * x - 1},
49       {0, 1, 4 * x        },
50       {0, 0, 4            },
51       {0, 0, 0            }
52   };
53   for (int i = 0; i < 4; i++) {
54     // i-th derivative of f
55     f[i] = table[i][0] * Tf[0] + table[i][1] * Tf[1] + table[i][2] * Tf[2];
56   }
57   for (int i = 3; i < N; i++) {
58     // T_n(x) = 2xT_{n-1}(x) - T_{n-2}(x)
59     table[0][i % 3] = 2 * x * table[0][(i - 1) % 3] - table[0][(i - 2) % 3];
60     // Differentiate Chebyshev polynomials with the recurrence relation
61     for (int j = 1; j < 4; j++) {
62       // T'_{n}(x)/n = 2T_{n-1}(x) + T'_{n-2}(x)/n-2
63       table[j][i % 3] = i * (2 * table[j - 1][(i - 1) % 3] + table[j][(i - 2) % 3] / (i - 2));
64     }
65     for (int j = 0; j < 4; j++) {
66       f[j] += table[j][i % 3] * Tf[i];
67     }
68   }
69   for (int i = 1; i < 4; i++) {
70     // Transform derivatives from Chebyshev [-1, 1] to [0, eta_max].
71     for (int j = 0; j < i; j++) f[i] *= dX_deta;
72   }
73 }
74 
75 // *****************************************************************************
76 // This helper function computes the Blasius boundary layer solution.
77 // *****************************************************************************
78 State CEED_QFUNCTION_HELPER(BlasiusSolution)(const BlasiusContext blasius, const CeedScalar x[3], const CeedScalar x0, const CeedScalar x_inflow,
79                                              const CeedScalar rho_infty, CeedScalar *t12) {
80   CeedInt    N     = blasius->n_cheb;
81   CeedScalar mu    = blasius->newtonian_ctx.mu;
82   CeedScalar nu    = mu / rho_infty;
83   CeedScalar eta   = x[1] * sqrt(blasius->U_inf / (nu * (x0 + x[0] - x_inflow)));
84   CeedScalar X     = 2 * (eta / blasius->eta_max) - 1.;
85   CeedScalar U_inf = blasius->U_inf;
86   CeedScalar Rd    = GasConstant(&blasius->newtonian_ctx);
87 
88   CeedScalar f[4], h[4];
89   ChebyshevEval(N, blasius->Tf_cheb, X, blasius->eta_max, f);
90   ChebyshevEval(N - 1, blasius->Th_cheb, X, blasius->eta_max, h);
91 
92   *t12 = mu * U_inf * f[2] * sqrt(U_inf / (nu * (x0 + x[0] - x_inflow)));
93 
94   CeedScalar Y[5];
95   Y[1] = U_inf * f[1];
96   Y[2] = 0.5 * sqrt(nu * U_inf / (x0 + x[0] - x_inflow)) * (eta * f[1] - f[0]);
97   Y[3] = 0.;
98   Y[4] = blasius->T_inf * h[0];
99   Y[0] = rho_infty / h[0] * Rd * Y[4];
100   return StateFromY(&blasius->newtonian_ctx, Y, x);
101 }
102 
103 // *****************************************************************************
104 // This QFunction sets a Blasius boundary layer for the initial condition
105 // *****************************************************************************
106 CEED_QFUNCTION(ICsBlasius)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
107   // Inputs
108   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
109 
110   // Outputs
111   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
112 
113   const BlasiusContext context    = (BlasiusContext)ctx;
114   const CeedScalar     cv         = context->newtonian_ctx.cv;
115   const CeedScalar     mu         = context->newtonian_ctx.mu;
116   const CeedScalar     T_inf      = context->T_inf;
117   const CeedScalar     P0         = context->P0;
118   const CeedScalar     delta0     = context->delta0;
119   const CeedScalar     U_inf      = context->U_inf;
120   const CeedScalar     x_inflow   = context->x_inflow;
121   const CeedScalar     gamma      = HeatCapacityRatio(&context->newtonian_ctx);
122   const CeedScalar     e_internal = cv * T_inf;
123   const CeedScalar     rho        = P0 / ((gamma - 1) * e_internal);
124   const CeedScalar     x0         = U_inf * rho / (mu * 25 / (delta0 * delta0));
125   CeedScalar           t12;
126 
127   // Quadrature Point Loop
128   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
129     const CeedScalar x[3] = {X[0][i], X[1][i], 0.};
130     State            s    = BlasiusSolution(context, x, x0, x_inflow, rho, &t12);
131     CeedScalar       q[5] = {0};
132     UnpackState_U(s.U, q);
133     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
134 
135   }  // End of Quadrature Point Loop
136   return 0;
137 }
138 
139 // *****************************************************************************
140 CEED_QFUNCTION(Blasius_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
141   // *INDENT-OFF*
142   // Inputs
143   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
144         (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
145 
146   // Outputs
147   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
148   // *INDENT-ON*
149   const BlasiusContext     context  = (BlasiusContext)ctx;
150   const bool               implicit = context->implicit;
151   NewtonianIdealGasContext gas      = &context->newtonian_ctx;
152   const CeedScalar         mu       = context->newtonian_ctx.mu;
153   const CeedScalar         Rd       = GasConstant(&context->newtonian_ctx);
154   const CeedScalar         T_inf    = context->T_inf;
155   const CeedScalar         P0       = context->P0;
156   const CeedScalar         delta0   = context->delta0;
157   const CeedScalar         U_inf    = context->U_inf;
158   const CeedScalar         x_inflow = context->x_inflow;
159   const bool               weakT    = context->weakT;
160   const CeedScalar         rho_0    = P0 / (Rd * T_inf);
161   const CeedScalar         x0       = U_inf * rho_0 / (mu * 25 / Square(delta0));
162 
163   CeedPragmaSIMD
164       // Quadrature Point Loop
165       for (CeedInt i = 0; i < Q; i++) {
166     // Setup
167     // -- Interp-to-Interp q_data
168     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
169     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
170     // We can effect this by swapping the sign on this weight
171     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
172 
173     // Calculate inflow values
174     const CeedScalar x[3] = {X[0][i], X[1][i], 0.};
175     CeedScalar       t12;
176     State            s = BlasiusSolution(context, x, x0, x_inflow, rho_0, &t12);
177     CeedScalar       qi[5];
178     for (CeedInt j = 0; j < 5; j++) qi[j] = q[j][i];
179     State s_int = StateFromU(gas, qi, x);
180 
181     // enabling user to choose between weak T and weak rho inflow
182     if (weakT) {  // density from the current solution
183       s.U.density = s_int.U.density;
184       s.Y         = StatePrimitiveFromConservative(gas, s.U, x);
185     } else {  // Total energy from current solution
186       s.U.E_total = s_int.U.E_total;
187       s.Y         = StatePrimitiveFromConservative(gas, s.U, x);
188     }
189 
190     // ---- Normal vect
191     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
192 
193     StateConservative Flux_inviscid[3];
194     FluxInviscid(&context->newtonian_ctx, s, Flux_inviscid);
195 
196     const CeedScalar stress[3][3] = {
197         {0,   t12, 0},
198         {t12, 0,   0},
199         {0,   0,   0}
200     };
201     const CeedScalar Fe[3] = {0};  // TODO: viscous energy flux needs grad temperature
202     CeedScalar       Flux[5];
203     FluxTotal_Boundary(Flux_inviscid, stress, Fe, norm, Flux);
204     for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
205   }  // End Quadrature Point Loop
206   return 0;
207 }
208 
209 // *****************************************************************************
210 CEED_QFUNCTION(Blasius_Inflow_Jacobian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
211   // *INDENT-OFF*
212   // Inputs
213   const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
214         (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
215 
216   // Outputs
217   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
218   // *INDENT-ON*
219   const BlasiusContext context  = (BlasiusContext)ctx;
220   const bool           implicit = context->implicit;
221   const CeedScalar     mu       = context->newtonian_ctx.mu;
222   const CeedScalar     cv       = context->newtonian_ctx.cv;
223   const CeedScalar     Rd       = GasConstant(&context->newtonian_ctx);
224   const CeedScalar     gamma    = HeatCapacityRatio(&context->newtonian_ctx);
225   const CeedScalar     T_inf    = context->T_inf;
226   const CeedScalar     P0       = context->P0;
227   const CeedScalar     delta0   = context->delta0;
228   const CeedScalar     U_inf    = context->U_inf;
229   const bool           weakT    = context->weakT;
230   const CeedScalar     rho_0    = P0 / (Rd * T_inf);
231   const CeedScalar     x0       = U_inf * rho_0 / (mu * 25 / (delta0 * delta0));
232 
233   CeedPragmaSIMD
234       // Quadrature Point Loop
235       for (CeedInt i = 0; i < Q; i++) {
236     // Setup
237     // -- Interp-to-Interp q_data
238     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
239     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
240     // We can effect this by swapping the sign on this weight
241     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
242 
243     // Calculate inflow values
244     const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
245     CeedScalar       t12;
246     State            s = BlasiusSolution(context, x, x0, 0, rho_0, &t12);
247 
248     // enabling user to choose between weak T and weak rho inflow
249     CeedScalar drho, dE, dP;
250     if (weakT) {
251       // rho should be from the current solution
252       drho                   = dq[0][i];
253       CeedScalar dE_internal = drho * cv * T_inf;
254       CeedScalar dE_kinetic  = .5 * drho * Dot3(s.Y.velocity, s.Y.velocity);
255       dE                     = dE_internal + dE_kinetic;
256       dP                     = drho * Rd * T_inf;  // interior rho with exterior T
257     } else {                                       // rho specified, E_internal from solution
258       drho = 0;
259       dE   = dq[4][i];
260       dP   = dE * (gamma - 1.);
261     }
262     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
263 
264     const CeedScalar u_normal = Dot3(norm, s.Y.velocity);
265 
266     v[0][i] = -wdetJb * drho * u_normal;
267     for (int j = 0; j < 3; j++) {
268       v[j + 1][i] = -wdetJb * (drho * u_normal * s.Y.velocity[j] + norm[j] * dP);
269     }
270     v[4][i] = -wdetJb * u_normal * (dE + dP);
271   }  // End Quadrature Point Loop
272   return 0;
273 }
274 
275 #endif  // blasius_h
276