xref: /libCEED/examples/fluids/qfunctions/blasius.h (revision 2518f336a2bcc61653a1548f8a480d533f99d612)
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 blasius_h
1388626eedSJames Wright #define blasius_h
1488626eedSJames Wright 
1588626eedSJames Wright #include <ceed.h>
16*2518f336SLeila Ghaffari #include "newtonian_state.h"
17841e4c73SJed Brown #include "newtonian_types.h"
1813fa47b2SJames Wright #include "utils.h"
1988626eedSJames Wright 
2088626eedSJames Wright typedef struct BlasiusContext_ *BlasiusContext;
2188626eedSJames Wright struct BlasiusContext_ {
2288626eedSJames Wright   bool       implicit; // !< Using implicit timesteping or not
23871db79fSKenneth E. Jansen   bool       weakT;    // !< flag to set Temperature weakly at inflow
2488626eedSJames Wright   CeedScalar delta0;   // !< Boundary layer height at inflow
2588626eedSJames Wright   CeedScalar Uinf;     // !< Velocity at boundary layer edge
26*2518f336SLeila Ghaffari   CeedScalar Tinf;     // !< Temperature at boundary layer edge
27*2518f336SLeila Ghaffari   CeedScalar T_wall;   // !< Temperature at the wall
2888626eedSJames Wright   CeedScalar P0;       // !< Pressure at outflow
2988626eedSJames Wright   CeedScalar theta0;   // !< Temperature at inflow
30f1122ed0SJames Wright   CeedScalar x_inflow; // !< Location of inflow in x
31*2518f336SLeila Ghaffari   CeedScalar n_cheb;   // !< Number of Chebyshev terms
32*2518f336SLeila Ghaffari   CeedScalar *X;       // !< Chebyshev polynomial coordinate vector
33*2518f336SLeila Ghaffari   CeedScalar eta_max;  // !< Maximum eta in the domain
34*2518f336SLeila Ghaffari   CeedScalar *Tf_cheb; // !< Chebyshev coefficient for f
35*2518f336SLeila Ghaffari   CeedScalar *Th_cheb; // !< Chebyshev coefficient for h
3688626eedSJames Wright   struct NewtonianIdealGasContext_ newtonian_ctx;
3788626eedSJames Wright };
3888626eedSJames Wright 
39*2518f336SLeila Ghaffari // *****************************************************************************
40*2518f336SLeila Ghaffari // This helper function evaluates Chebyshev polynomials with a set of
41*2518f336SLeila Ghaffari //  coefficients with all their derivatives represented as a recurrence table.
42*2518f336SLeila Ghaffari // *****************************************************************************
43*2518f336SLeila Ghaffari CEED_QFUNCTION_HELPER void ChebyshevEval(int N, const double *Tf, double x,
44*2518f336SLeila Ghaffari     double eta_max, double *f) {
45*2518f336SLeila Ghaffari   double dX_deta   = 2 / eta_max;
46*2518f336SLeila Ghaffari   double table[4][3] = {
47*2518f336SLeila Ghaffari     // Chebyshev polynomials T_0, T_1, T_2 of the first kind in (-1,1)
48*2518f336SLeila Ghaffari     {1, x, 2*x *x - 1}, {0, 1, 4*x}, {0, 0, 4}, {0, 0, 0}
49*2518f336SLeila Ghaffari   };
50*2518f336SLeila Ghaffari   for (int i=0; i<4; i++) {
51*2518f336SLeila Ghaffari     // i-th derivative of f
52*2518f336SLeila Ghaffari     f[i] = table[i][0] * Tf[0] + table[i][1] * Tf[1] + table[i][2] * Tf[2];
53*2518f336SLeila Ghaffari   }
54*2518f336SLeila Ghaffari   for (int i=3; i<N; i++) {
55*2518f336SLeila Ghaffari     // T_n(x) = 2xT_{n-1}(x) - T_{n-2}(x)
56*2518f336SLeila Ghaffari     table[0][i%3] = 2 * x * table[0][(i-1) % 3] - table[0][(i-2)%3];
57*2518f336SLeila Ghaffari     // Differentiate Chebyshev polynomials with the recurrence relation
58*2518f336SLeila Ghaffari     for (int j=1; j<4; j++) {
59*2518f336SLeila Ghaffari       // T'_{n}(x)/n = 2T_{n-1}(x) + T'_{n-2}(x)/n-2
60*2518f336SLeila Ghaffari       table[j][i%3] = i * (2 * table[j-1][(i-1) % 3] + table[j][(i-2)%3] / (i-2));
61*2518f336SLeila Ghaffari     }
62*2518f336SLeila Ghaffari     for (int j=0; j<4; j++) {
63*2518f336SLeila Ghaffari       f[j] += table[j][i%3] * Tf[i];
6488626eedSJames Wright     }
6588626eedSJames Wright   }
66*2518f336SLeila Ghaffari   for (int i=1; i<4; i++) {
67*2518f336SLeila Ghaffari     // Transform derivatives from Chebyshev [-1, 1] to [0, eta_max].
68*2518f336SLeila Ghaffari     for (int j=0; j<i; j++) f[i] *= dX_deta;
69*2518f336SLeila Ghaffari   }
7088626eedSJames Wright }
7188626eedSJames Wright 
72*2518f336SLeila Ghaffari // *****************************************************************************
73*2518f336SLeila Ghaffari // This helper function computes the Blasius boundary layer solution.
74*2518f336SLeila Ghaffari // *****************************************************************************
75*2518f336SLeila Ghaffari State CEED_QFUNCTION_HELPER(BlasiusSolution)(const BlasiusContext blasius,
76*2518f336SLeila Ghaffari     const CeedScalar x[3], const CeedScalar x0, const CeedScalar x_inflow,
77*2518f336SLeila Ghaffari     const CeedScalar rho, CeedScalar *t12) {
78*2518f336SLeila Ghaffari   CeedInt    N    = blasius->n_cheb;
79*2518f336SLeila Ghaffari   CeedScalar nu   = blasius->newtonian_ctx.mu / rho;
80*2518f336SLeila Ghaffari   CeedScalar eta  = x[1]*sqrt(blasius->Uinf/(nu*(x0+x[0]-x_inflow)));
81*2518f336SLeila Ghaffari   CeedScalar X    = 2 * (eta / blasius->eta_max) - 1.;
82*2518f336SLeila Ghaffari   CeedScalar Uinf = blasius->Uinf;
83*2518f336SLeila Ghaffari   CeedScalar Rd   = GasConstant(&blasius->newtonian_ctx);
84*2518f336SLeila Ghaffari 
85*2518f336SLeila Ghaffari   CeedScalar f[4], h[4];
86*2518f336SLeila Ghaffari   ChebyshevEval(N, blasius->Tf_cheb, X, blasius->eta_max, f);
87*2518f336SLeila Ghaffari   ChebyshevEval(N-1, blasius->Th_cheb, X, blasius->eta_max, h);
88*2518f336SLeila Ghaffari 
89*2518f336SLeila Ghaffari   *t12 = rho*nu*Uinf*f[2]*sqrt(Uinf/(nu*(x0+x[0]-x_inflow)));
90*2518f336SLeila Ghaffari 
91*2518f336SLeila Ghaffari   CeedScalar Y[5];
92*2518f336SLeila Ghaffari   Y[1] = Uinf * f[1];
93*2518f336SLeila Ghaffari   Y[2] = 0.5*sqrt(nu*Uinf/(x0+x[0]-x_inflow))*(eta*f[1] - f[0]);
94*2518f336SLeila Ghaffari   Y[3] = 0.;
95*2518f336SLeila Ghaffari   Y[4] = blasius->Tinf * h[0];
96*2518f336SLeila Ghaffari   Y[0] = rho * Rd * Y[4];
97*2518f336SLeila Ghaffari   return StateFromY(&blasius->newtonian_ctx, Y, x);
9888626eedSJames Wright }
9988626eedSJames Wright 
10088626eedSJames Wright // *****************************************************************************
10188626eedSJames Wright // This QFunction sets a Blasius boundary layer for the initial condition
10288626eedSJames Wright // *****************************************************************************
10388626eedSJames Wright CEED_QFUNCTION(ICsBlasius)(void *ctx, CeedInt Q,
10488626eedSJames Wright                            const CeedScalar *const *in, CeedScalar *const *out) {
10588626eedSJames Wright   // Inputs
10688626eedSJames Wright   const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
10788626eedSJames Wright 
10888626eedSJames Wright   // Outputs
10988626eedSJames Wright   CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
11088626eedSJames Wright 
11188626eedSJames Wright   const BlasiusContext context = (BlasiusContext)ctx;
11288626eedSJames Wright   const CeedScalar cv         = context->newtonian_ctx.cv;
11388626eedSJames Wright   const CeedScalar mu         = context->newtonian_ctx.mu;
11488626eedSJames Wright   const CeedScalar theta0     = context->theta0;
11588626eedSJames Wright   const CeedScalar P0         = context->P0;
11688626eedSJames Wright   const CeedScalar delta0     = context->delta0;
11788626eedSJames Wright   const CeedScalar Uinf       = context->Uinf;
118f1122ed0SJames Wright   const CeedScalar x_inflow   = context->x_inflow;
119*2518f336SLeila Ghaffari   const CeedScalar gamma      = HeatCapacityRatio(&context->newtonian_ctx);
12088626eedSJames Wright   const CeedScalar e_internal = cv * theta0;
12188626eedSJames Wright   const CeedScalar rho        = P0 / ((gamma - 1) * e_internal);
12288626eedSJames Wright   const CeedScalar x0         = Uinf*rho / (mu*25/(delta0*delta0));
123*2518f336SLeila Ghaffari   CeedScalar t12;
12488626eedSJames Wright 
12588626eedSJames Wright   // Quadrature Point Loop
12688626eedSJames Wright   CeedPragmaSIMD
12788626eedSJames Wright   for (CeedInt i=0; i<Q; i++) {
128*2518f336SLeila Ghaffari     const CeedScalar x[3] = {X[0][i], X[1][i], 0.};
129*2518f336SLeila Ghaffari     State s = BlasiusSolution(context, x, x0, x_inflow, rho, &t12);
130*2518f336SLeila Ghaffari     CeedScalar q[5] = {0};
131*2518f336SLeila Ghaffari     UnpackState_U(s.U, q);
132*2518f336SLeila Ghaffari     for (CeedInt j=0; j<5; j++) q0[j][i] = q[j];
13388626eedSJames Wright 
13488626eedSJames Wright   } // End of Quadrature Point Loop
13588626eedSJames Wright   return 0;
13688626eedSJames Wright }
13788626eedSJames Wright 
13888626eedSJames Wright // *****************************************************************************
13988626eedSJames Wright CEED_QFUNCTION(Blasius_Inflow)(void *ctx, CeedInt Q,
14088626eedSJames Wright                                const CeedScalar *const *in,
14188626eedSJames Wright                                CeedScalar *const *out) {
14288626eedSJames Wright   // *INDENT-OFF*
14388626eedSJames Wright   // Inputs
14488626eedSJames Wright   const CeedScalar (*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0],
145e8b03feeSJames Wright                    (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
146e8b03feeSJames Wright                    (*X)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
14788626eedSJames Wright 
14888626eedSJames Wright   // Outputs
14988626eedSJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
15088626eedSJames Wright   // *INDENT-ON*
15188626eedSJames Wright   const BlasiusContext context = (BlasiusContext)ctx;
15288626eedSJames Wright   const bool implicit       = context->implicit;
15388626eedSJames Wright   const CeedScalar mu       = context->newtonian_ctx.mu;
15488626eedSJames Wright   const CeedScalar cv       = context->newtonian_ctx.cv;
155*2518f336SLeila Ghaffari   const CeedScalar Rd       = GasConstant(&context->newtonian_ctx);
156*2518f336SLeila Ghaffari   const CeedScalar gamma    = HeatCapacityRatio(&context->newtonian_ctx);
15788626eedSJames Wright   const CeedScalar theta0   = context->theta0;
15888626eedSJames Wright   const CeedScalar P0       = context->P0;
15988626eedSJames Wright   const CeedScalar delta0   = context->delta0;
16088626eedSJames Wright   const CeedScalar Uinf     = context->Uinf;
161f1122ed0SJames Wright   const CeedScalar x_inflow = context->x_inflow;
162871db79fSKenneth E. Jansen   const bool       weakT    = context->weakT;
16388626eedSJames Wright   const CeedScalar rho_0    = P0 / (Rd * theta0);
16413fa47b2SJames Wright   const CeedScalar x0       = Uinf*rho_0 / (mu*25/ Square(delta0));
16588626eedSJames Wright 
16688626eedSJames Wright   CeedPragmaSIMD
16788626eedSJames Wright   // Quadrature Point Loop
16888626eedSJames Wright   for (CeedInt i=0; i<Q; i++) {
16988626eedSJames Wright     // Setup
17088626eedSJames Wright     // -- Interp-to-Interp q_data
17188626eedSJames Wright     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
17288626eedSJames Wright     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
17388626eedSJames Wright     // We can effect this by swapping the sign on this weight
17488626eedSJames Wright     const CeedScalar wdetJb  = (implicit ? -1. : 1.) * q_data_sur[0][i];
17588626eedSJames Wright 
176871db79fSKenneth E. Jansen     // Calculate inflow values
177*2518f336SLeila Ghaffari     const CeedScalar x[3] = {X[0][i], X[1][i], 0.};
17888626eedSJames Wright     CeedScalar t12;
179*2518f336SLeila Ghaffari     State s = BlasiusSolution(context, x, x0, x_inflow, rho_0, &t12);
18088626eedSJames Wright 
181871db79fSKenneth E. Jansen     // enabling user to choose between weak T and weak rho inflow
182871db79fSKenneth E. Jansen     CeedScalar rho,E_internal, P, E_kinetic;
183871db79fSKenneth E. Jansen     if (weakT) {
184871db79fSKenneth E. Jansen       // rho should be from the current solution
185871db79fSKenneth E. Jansen       rho = q[0][i];
186871db79fSKenneth E. Jansen       // Temperature is being set weakly (theta0) and for constant cv this sets E_internal
187871db79fSKenneth E. Jansen       E_internal = rho * cv * theta0;
188871db79fSKenneth E. Jansen       // Find pressure using
189871db79fSKenneth E. Jansen       P = rho*Rd*theta0; // interior rho with exterior T
190*2518f336SLeila Ghaffari       E_kinetic = .5 * rho * Dot3(s.Y.velocity, s.Y.velocity);
191871db79fSKenneth E. Jansen     } else {
192871db79fSKenneth E. Jansen       //  Fixing rho weakly on the inflow to a value consistent with theta0 and P0
193871db79fSKenneth E. Jansen       rho =  rho_0;
194*2518f336SLeila Ghaffari       E_kinetic = .5 * rho * Dot3(s.Y.velocity, s.Y.velocity);
195871db79fSKenneth E. Jansen       E_internal = q[4][i] - E_kinetic; // uses set rho and u but E from solution
196871db79fSKenneth E. Jansen       P = E_internal * (gamma - 1.);
197871db79fSKenneth E. Jansen     }
198871db79fSKenneth E. Jansen     const CeedScalar E = E_internal + E_kinetic;
19988626eedSJames Wright     // ---- Normal vect
20088626eedSJames Wright     const CeedScalar norm[3] = {q_data_sur[1][i],
20188626eedSJames Wright                                 q_data_sur[2][i],
20288626eedSJames Wright                                 q_data_sur[3][i]
20388626eedSJames Wright                                };
20488626eedSJames Wright 
20588626eedSJames Wright     // The Physics
20688626eedSJames Wright     // Zero v so all future terms can safely sum into it
207ba6664aeSJames Wright     for (CeedInt j=0; j<5; j++) v[j][i] = 0.;
20888626eedSJames Wright 
209*2518f336SLeila Ghaffari     const CeedScalar u_normal = Dot3(norm, s.Y.velocity);
21030ccfdeaSJed Brown     const CeedScalar viscous_flux[3] = {-t12 *norm[1], -t12 *norm[0], 0};
21188626eedSJames Wright 
21288626eedSJames Wright     // The Physics
21388626eedSJames Wright     // -- Density
21488626eedSJames Wright     v[0][i] -= wdetJb * rho * u_normal; // interior rho
21588626eedSJames Wright 
21688626eedSJames Wright     // -- Momentum
217ba6664aeSJames Wright     for (CeedInt j=0; j<3; j++)
218*2518f336SLeila Ghaffari       v[j+1][i] -= wdetJb * (rho * u_normal * s.Y.velocity[j] // interior rho
21930ccfdeaSJed Brown                              + norm[j] * P // mixed P
22030ccfdeaSJed Brown                              + viscous_flux[j]);
22188626eedSJames Wright 
22288626eedSJames Wright     // -- Total Energy Density
223*2518f336SLeila Ghaffari     v[4][i] -= wdetJb * (u_normal * (E + P) + Dot3(viscous_flux, s.Y.velocity));
22488626eedSJames Wright 
22588626eedSJames Wright   } // End Quadrature Point Loop
22688626eedSJames Wright   return 0;
22788626eedSJames Wright }
22888626eedSJames Wright 
229*2518f336SLeila Ghaffari // *****************************************************************************
230e334ad8fSJed Brown CEED_QFUNCTION(Blasius_Inflow_Jacobian)(void *ctx, CeedInt Q,
231e334ad8fSJed Brown                                         const CeedScalar *const *in,
232e334ad8fSJed Brown                                         CeedScalar *const *out) {
233e334ad8fSJed Brown   // *INDENT-OFF*
234e334ad8fSJed Brown   // Inputs
235e334ad8fSJed Brown   const CeedScalar (*dq)[CEED_Q_VLA]         = (const CeedScalar(*)[CEED_Q_VLA])in[0],
236b55ac660SJames Wright                    (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2],
237b55ac660SJames Wright                    (*X)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[3];
238e334ad8fSJed Brown 
239e334ad8fSJed Brown   // Outputs
240e334ad8fSJed Brown   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
241e334ad8fSJed Brown   // *INDENT-ON*
242e334ad8fSJed Brown   const BlasiusContext context = (BlasiusContext)ctx;
243e334ad8fSJed Brown   const bool implicit     = context->implicit;
244e334ad8fSJed Brown   const CeedScalar mu     = context->newtonian_ctx.mu;
245e334ad8fSJed Brown   const CeedScalar cv     = context->newtonian_ctx.cv;
246*2518f336SLeila Ghaffari   const CeedScalar Rd     = GasConstant(&context->newtonian_ctx);
247*2518f336SLeila Ghaffari   const CeedScalar gamma  = HeatCapacityRatio(&context->newtonian_ctx);
248e334ad8fSJed Brown   const CeedScalar theta0 = context->theta0;
249e334ad8fSJed Brown   const CeedScalar P0     = context->P0;
250e334ad8fSJed Brown   const CeedScalar delta0 = context->delta0;
251e334ad8fSJed Brown   const CeedScalar Uinf   = context->Uinf;
252e334ad8fSJed Brown   const bool       weakT  = context->weakT;
253e334ad8fSJed Brown   const CeedScalar rho_0  = P0 / (Rd * theta0);
254e334ad8fSJed Brown   const CeedScalar x0     = Uinf*rho_0 / (mu*25/ (delta0*delta0));
255e334ad8fSJed Brown 
256e334ad8fSJed Brown   CeedPragmaSIMD
257e334ad8fSJed Brown   // Quadrature Point Loop
258e334ad8fSJed Brown   for (CeedInt i=0; i<Q; i++) {
259e334ad8fSJed Brown     // Setup
260e334ad8fSJed Brown     // -- Interp-to-Interp q_data
261e334ad8fSJed Brown     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
262e334ad8fSJed Brown     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
263e334ad8fSJed Brown     // We can effect this by swapping the sign on this weight
264e334ad8fSJed Brown     const CeedScalar wdetJb  = (implicit ? -1. : 1.) * q_data_sur[0][i];
265e334ad8fSJed Brown 
266e334ad8fSJed Brown     // Calculate inflow values
267*2518f336SLeila Ghaffari     const CeedScalar x[3] = {X[0][i], X[1][i], 0.};
268e334ad8fSJed Brown     CeedScalar t12;
269*2518f336SLeila Ghaffari     State s = BlasiusSolution(context, x, x0, 0, rho_0, &t12);
270e334ad8fSJed Brown 
271e334ad8fSJed Brown     // enabling user to choose between weak T and weak rho inflow
272e334ad8fSJed Brown     CeedScalar drho, dE, dP;
273e334ad8fSJed Brown     if (weakT) {
274e334ad8fSJed Brown       // rho should be from the current solution
275e334ad8fSJed Brown       drho = dq[0][i];
276e334ad8fSJed Brown       CeedScalar dE_internal = drho * cv * theta0;
277*2518f336SLeila Ghaffari       CeedScalar dE_kinetic = .5 * drho * Dot3(s.Y.velocity, s.Y.velocity);
278e334ad8fSJed Brown       dE = dE_internal + dE_kinetic;
279e334ad8fSJed Brown       dP = drho * Rd * theta0; // interior rho with exterior T
280e334ad8fSJed Brown     } else { // rho specified, E_internal from solution
281e334ad8fSJed Brown       drho = 0;
282e334ad8fSJed Brown       dE = dq[4][i];
283e334ad8fSJed Brown       dP = dE * (gamma - 1.);
284e334ad8fSJed Brown     }
285e334ad8fSJed Brown     const CeedScalar norm[3] = {q_data_sur[1][i],
286e334ad8fSJed Brown                                 q_data_sur[2][i],
287e334ad8fSJed Brown                                 q_data_sur[3][i]
288e334ad8fSJed Brown                                };
289e334ad8fSJed Brown 
290*2518f336SLeila Ghaffari     const CeedScalar u_normal = Dot3(norm, s.Y.velocity);
291e334ad8fSJed Brown 
292e334ad8fSJed Brown     v[0][i] = - wdetJb * drho * u_normal;
293e334ad8fSJed Brown     for (int j=0; j<3; j++)
294*2518f336SLeila Ghaffari       v[j+1][i] = -wdetJb * (drho * u_normal * s.Y.velocity[j] + norm[j] * dP);
295e334ad8fSJed Brown     v[4][i] = - wdetJb * u_normal * (dE + dP);
296e334ad8fSJed Brown   } // End Quadrature Point Loop
297e334ad8fSJed Brown   return 0;
298e334ad8fSJed Brown }
299e334ad8fSJed Brown 
30088626eedSJames Wright #endif // blasius_h
301