1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3bb8a0c61SJames Wright
4bb8a0c61SJames Wright /// @file
5ea615d4cSJames Wright /// Operator for HONEE
63e17a7a1SJames Wright #include <ceed/types.h>
72b916ea7SJeremy L Thompson
8e0d1a4dfSLeila Ghaffari #include "newtonian_state.h"
915a3537eSJed Brown #include "newtonian_types.h"
10704b8bbeSJames Wright #include "utils.h"
11bb8a0c61SJames Wright
120d850f2eSLeila Ghaffari #define BLASIUS_MAX_N_CHEBYSHEV 50
130d850f2eSLeila Ghaffari
14bb8a0c61SJames Wright typedef struct BlasiusContext_ *BlasiusContext;
15bb8a0c61SJames Wright struct BlasiusContext_ {
16bb8a0c61SJames Wright bool implicit; // !< Using implicit timesteping or not
172acc7cbcSKenneth E. Jansen bool weakT; // !< flag to set Temperature weakly at inflow
18bb8a0c61SJames Wright CeedScalar delta0; // !< Boundary layer height at inflow
19fcb2c22aSJames Wright State S_infty;
20e0d1a4dfSLeila Ghaffari CeedScalar T_wall; // !< Temperature at the wall
21ef2c71fdSJames Wright CeedScalar x_inflow; // !< Location of inflow in x
22e0d1a4dfSLeila Ghaffari CeedScalar n_cheb; // !< Number of Chebyshev terms
230d850f2eSLeila Ghaffari CeedScalar *X; // !< Chebyshev polynomial coordinate vector (CPU only)
24e0d1a4dfSLeila Ghaffari CeedScalar eta_max; // !< Maximum eta in the domain
250d850f2eSLeila Ghaffari CeedScalar Tf_cheb[BLASIUS_MAX_N_CHEBYSHEV]; // !< Chebyshev coefficient for f
260d850f2eSLeila Ghaffari CeedScalar Th_cheb[BLASIUS_MAX_N_CHEBYSHEV - 1]; // !< Chebyshev coefficient for h
27*cde3d787SJames Wright struct NewtonianIdealGasContext_ newt_ctx;
28bb8a0c61SJames Wright };
29bb8a0c61SJames Wright
30e0d1a4dfSLeila Ghaffari // *****************************************************************************
3104e40bb6SJeremy L Thompson // This helper function evaluates Chebyshev polynomials with a set of coefficients with all their derivatives represented as a recurrence table.
32e0d1a4dfSLeila Ghaffari // *****************************************************************************
ChebyshevEval(int N,const double * Tf,double x,double eta_max,double * f)332b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void ChebyshevEval(int N, const double *Tf, double x, double eta_max, double *f) {
34e0d1a4dfSLeila Ghaffari double dX_deta = 2 / eta_max;
35e0d1a4dfSLeila Ghaffari // Chebyshev polynomials T_0, T_1, T_2 of the first kind in (-1,1)
36f701dcc9SJames Wright double table[4][3] = {
372b916ea7SJeremy L Thompson {1, x, 2 * x * x - 1},
382b916ea7SJeremy L Thompson {0, 1, 4 * x },
392b916ea7SJeremy L Thompson {0, 0, 4 },
402b916ea7SJeremy L Thompson {0, 0, 0 }
41e0d1a4dfSLeila Ghaffari };
42e0d1a4dfSLeila Ghaffari for (int i = 0; i < 4; i++) {
43e0d1a4dfSLeila Ghaffari // i-th derivative of f
44e0d1a4dfSLeila Ghaffari f[i] = table[i][0] * Tf[0] + table[i][1] * Tf[1] + table[i][2] * Tf[2];
45e0d1a4dfSLeila Ghaffari }
46e0d1a4dfSLeila Ghaffari for (int i = 3; i < N; i++) {
47e0d1a4dfSLeila Ghaffari // T_n(x) = 2xT_{n-1}(x) - T_{n-2}(x)
48e0d1a4dfSLeila Ghaffari table[0][i % 3] = 2 * x * table[0][(i - 1) % 3] - table[0][(i - 2) % 3];
49e0d1a4dfSLeila Ghaffari // Differentiate Chebyshev polynomials with the recurrence relation
50e0d1a4dfSLeila Ghaffari for (int j = 1; j < 4; j++) {
51e0d1a4dfSLeila Ghaffari // T'_{n}(x)/n = 2T_{n-1}(x) + T'_{n-2}(x)/n-2
52e0d1a4dfSLeila Ghaffari table[j][i % 3] = i * (2 * table[j - 1][(i - 1) % 3] + table[j][(i - 2) % 3] / (i - 2));
53e0d1a4dfSLeila Ghaffari }
54e0d1a4dfSLeila Ghaffari for (int j = 0; j < 4; j++) {
55e0d1a4dfSLeila Ghaffari f[j] += table[j][i % 3] * Tf[i];
56bb8a0c61SJames Wright }
57bb8a0c61SJames Wright }
58e0d1a4dfSLeila Ghaffari for (int i = 1; i < 4; i++) {
59e0d1a4dfSLeila Ghaffari // Transform derivatives from Chebyshev [-1, 1] to [0, eta_max].
60e0d1a4dfSLeila Ghaffari for (int j = 0; j < i; j++) f[i] *= dX_deta;
61e0d1a4dfSLeila Ghaffari }
62bb8a0c61SJames Wright }
63bb8a0c61SJames Wright
64e0d1a4dfSLeila Ghaffari // *****************************************************************************
65e0d1a4dfSLeila Ghaffari // This helper function computes the Blasius boundary layer solution.
66e0d1a4dfSLeila Ghaffari // *****************************************************************************
BlasiusSolution(const BlasiusContext blasius,const CeedScalar x[3],const CeedScalar x0,const CeedScalar x_inflow,const CeedScalar rho_infty,CeedScalar * t12)672b916ea7SJeremy L Thompson State CEED_QFUNCTION_HELPER(BlasiusSolution)(const BlasiusContext blasius, const CeedScalar x[3], const CeedScalar x0, const CeedScalar x_inflow,
680d850f2eSLeila Ghaffari const CeedScalar rho_infty, CeedScalar *t12) {
69e0d1a4dfSLeila Ghaffari CeedInt N = blasius->n_cheb;
70*cde3d787SJames Wright CeedScalar mu = blasius->newt_ctx.gas.mu;
71fcb2c22aSJames Wright State S_infty = blasius->S_infty;
720d850f2eSLeila Ghaffari CeedScalar nu = mu / rho_infty;
7364667825SJames Wright CeedScalar U_infty = Norm3(S_infty.Y.velocity);
74fcb2c22aSJames Wright CeedScalar eta = x[1] * sqrt(U_infty / (nu * (x0 + x[0] - x_inflow)));
75e0d1a4dfSLeila Ghaffari CeedScalar X = 2 * (eta / blasius->eta_max) - 1.;
76*cde3d787SJames Wright CeedScalar Rd = GasConstant(blasius->newt_ctx.gas);
77e0d1a4dfSLeila Ghaffari
78e0d1a4dfSLeila Ghaffari CeedScalar f[4], h[4];
79e0d1a4dfSLeila Ghaffari ChebyshevEval(N, blasius->Tf_cheb, X, blasius->eta_max, f);
80e0d1a4dfSLeila Ghaffari ChebyshevEval(N - 1, blasius->Th_cheb, X, blasius->eta_max, h);
81e0d1a4dfSLeila Ghaffari
82fcb2c22aSJames Wright *t12 = mu * U_infty * f[2] * sqrt(U_infty / (nu * (x0 + x[0] - x_inflow)));
83e0d1a4dfSLeila Ghaffari
84e0d1a4dfSLeila Ghaffari CeedScalar Y[5];
85fcb2c22aSJames Wright Y[1] = U_infty * f[1];
86fcb2c22aSJames Wright Y[2] = 0.5 * sqrt(nu * U_infty / (x0 + x[0] - x_inflow)) * (eta * f[1] - f[0]);
87e0d1a4dfSLeila Ghaffari Y[3] = 0.;
88fcb2c22aSJames Wright Y[4] = S_infty.Y.temperature * h[0];
890d850f2eSLeila Ghaffari Y[0] = rho_infty / h[0] * Rd * Y[4];
90*cde3d787SJames Wright return StateFromY(blasius->newt_ctx.gas, Y);
91bb8a0c61SJames Wright }
92bb8a0c61SJames Wright
93bb8a0c61SJames Wright // *****************************************************************************
94bb8a0c61SJames Wright // This QFunction sets a Blasius boundary layer for the initial condition
95bb8a0c61SJames Wright // *****************************************************************************
ICsBlasius(void * ctx,CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)962b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsBlasius)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
97bb8a0c61SJames Wright const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
98bb8a0c61SJames Wright CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
99bb8a0c61SJames Wright
100bb8a0c61SJames Wright const BlasiusContext context = (BlasiusContext)ctx;
101*cde3d787SJames Wright const NewtonianIGProperties gas = context->newt_ctx.gas;
102*cde3d787SJames Wright const CeedScalar mu = context->newt_ctx.gas.mu;
103bb8a0c61SJames Wright const CeedScalar delta0 = context->delta0;
104ef2c71fdSJames Wright const CeedScalar x_inflow = context->x_inflow;
105e0d1a4dfSLeila Ghaffari CeedScalar t12;
106bb8a0c61SJames Wright
107fcb2c22aSJames Wright const State S_infty = context->S_infty;
10864667825SJames Wright const CeedScalar U_infty = Norm3(S_infty.Y.velocity);
109bb8a0c61SJames Wright
110fcb2c22aSJames Wright const CeedScalar x0 = U_infty * S_infty.U.density / (mu * 25 / Square(delta0));
11133796533SJames Wright
11233796533SJames Wright CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
11333796533SJames Wright const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
114fcb2c22aSJames Wright State s = BlasiusSolution(context, x, x0, x_inflow, S_infty.U.density, &t12);
115a541e550SJames Wright CeedScalar q[5];
11633796533SJames Wright
117*cde3d787SJames Wright StateToQ(gas, s, q, context->newt_ctx.state_var);
11833796533SJames Wright for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
11933796533SJames Wright }
120bb8a0c61SJames Wright return 0;
121bb8a0c61SJames Wright }
122bb8a0c61SJames Wright
123bb8a0c61SJames Wright // *****************************************************************************
Blasius_Inflow(void * ctx,CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)1242b916ea7SJeremy L Thompson CEED_QFUNCTION(Blasius_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1254b96a86bSJames Wright const BlasiusContext context = (BlasiusContext)ctx;
1263d65b166SJames Wright const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
127ade49511SJames Wright const CeedScalar(*q_data_sur) = in[2];
1283d65b166SJames Wright const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
129bb8a0c61SJames Wright CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
1303d65b166SJames Wright
131ade49511SJames Wright const bool is_implicit = context->implicit;
132*cde3d787SJames Wright const NewtonianIGProperties gas = context->newt_ctx.gas;
133fcb2c22aSJames Wright State S_infty = context->S_infty;
134fcb2c22aSJames Wright const CeedScalar rho_0 = S_infty.U.density;
13564667825SJames Wright const CeedScalar U_infty = Norm3(S_infty.Y.velocity);
136*cde3d787SJames Wright const CeedScalar x0 = U_infty * rho_0 / (gas.mu * 25 / Square(context->delta0));
137bb8a0c61SJames Wright
1383d65b166SJames Wright CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
13978e8b7daSJames Wright CeedScalar wdetJb, normal[3];
14078e8b7daSJames Wright QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, normal);
141ade49511SJames Wright wdetJb *= is_implicit ? -1. : 1.;
142bb8a0c61SJames Wright
1432acc7cbcSKenneth E. Jansen // Calculate inflow values
144e0d1a4dfSLeila Ghaffari const CeedScalar x[3] = {X[0][i], X[1][i], 0.};
145bb8a0c61SJames Wright CeedScalar t12;
146512c8ec7SJames Wright State s = BlasiusSolution(context, x, x0, context->x_inflow, rho_0, &t12);
1470d850f2eSLeila Ghaffari CeedScalar qi[5];
1480d850f2eSLeila Ghaffari for (CeedInt j = 0; j < 5; j++) qi[j] = q[j][i];
149edcfef1bSKenneth E. Jansen State s_int = StateFromU(gas, qi);
150bb8a0c61SJames Wright
1512acc7cbcSKenneth E. Jansen // enabling user to choose between weak T and weak rho inflow
152512c8ec7SJames Wright if (context->weakT) { // density from the current solution
1530d850f2eSLeila Ghaffari s.U.density = s_int.U.density;
154edcfef1bSKenneth E. Jansen s.Y = StatePrimitiveFromConservative(gas, s.U);
1550d850f2eSLeila Ghaffari } else { // Total energy from current solution
1560d850f2eSLeila Ghaffari s.U.E_total = s_int.U.E_total;
157edcfef1bSKenneth E. Jansen s.Y = StatePrimitiveFromConservative(gas, s.U);
1582acc7cbcSKenneth E. Jansen }
1590d850f2eSLeila Ghaffari
1600d850f2eSLeila Ghaffari StateConservative Flux_inviscid[3];
161*cde3d787SJames Wright FluxInviscid(gas, s, Flux_inviscid);
162bb8a0c61SJames Wright
1632b916ea7SJeremy L Thompson const CeedScalar stress[3][3] = {
1642b916ea7SJeremy L Thompson {0, t12, 0},
1652b916ea7SJeremy L Thompson {t12, 0, 0},
1662b916ea7SJeremy L Thompson {0, 0, 0}
1672b916ea7SJeremy L Thompson };
1680d850f2eSLeila Ghaffari const CeedScalar Fe[3] = {0}; // TODO: viscous energy flux needs grad temperature
1690d850f2eSLeila Ghaffari CeedScalar Flux[5];
17078e8b7daSJames Wright FluxTotal_Boundary(Flux_inviscid, stress, Fe, normal, Flux);
1712b916ea7SJeremy L Thompson for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j];
172ade49511SJames Wright }
173bb8a0c61SJames Wright return 0;
174bb8a0c61SJames Wright }
175bb8a0c61SJames Wright
176e0d1a4dfSLeila Ghaffari // *****************************************************************************
Blasius_Inflow_Jacobian(void * ctx,CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)1772b916ea7SJeremy L Thompson CEED_QFUNCTION(Blasius_Inflow_Jacobian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1783d65b166SJames Wright const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
179ade49511SJames Wright const CeedScalar(*q_data_sur) = in[2];
1803d65b166SJames Wright const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
181f0b65372SJed Brown CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
1823d65b166SJames Wright
183f0b65372SJed Brown const BlasiusContext context = (BlasiusContext)ctx;
184*cde3d787SJames Wright const NewtonianIGProperties gas = context->newt_ctx.gas;
185ade49511SJames Wright const bool is_implicit = context->implicit;
186512c8ec7SJames Wright const CeedScalar Rd = GasConstant(gas);
187512c8ec7SJames Wright const CeedScalar gamma = HeatCapacityRatio(gas);
188fcb2c22aSJames Wright const State S_infty = context->S_infty;
189fcb2c22aSJames Wright const CeedScalar rho_0 = S_infty.U.density;
19064667825SJames Wright const CeedScalar U_infty = Norm3(S_infty.Y.velocity);
191*cde3d787SJames Wright const CeedScalar x0 = U_infty * rho_0 / (gas.mu * 25 / Square(context->delta0));
192f0b65372SJed Brown
1933d65b166SJames Wright CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
19478e8b7daSJames Wright CeedScalar wdetJb, normal[3];
19578e8b7daSJames Wright QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, normal);
196ade49511SJames Wright wdetJb *= is_implicit ? -1. : 1.;
197f0b65372SJed Brown
198f0b65372SJed Brown // Calculate inflow values
1990d850f2eSLeila Ghaffari const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]};
200f0b65372SJed Brown CeedScalar t12;
201e0d1a4dfSLeila Ghaffari State s = BlasiusSolution(context, x, x0, 0, rho_0, &t12);
202f0b65372SJed Brown
203f0b65372SJed Brown // enabling user to choose between weak T and weak rho inflow
204f0b65372SJed Brown CeedScalar drho, dE, dP;
205512c8ec7SJames Wright if (context->weakT) {
206f0b65372SJed Brown // rho should be from the current solution
207f0b65372SJed Brown drho = dq[0][i];
208*cde3d787SJames Wright CeedScalar dE_internal = drho * gas.cv * S_infty.Y.temperature;
209e0d1a4dfSLeila Ghaffari CeedScalar dE_kinetic = .5 * drho * Dot3(s.Y.velocity, s.Y.velocity);
210f0b65372SJed Brown dE = dE_internal + dE_kinetic;
211fcb2c22aSJames Wright dP = drho * Rd * S_infty.Y.temperature; // interior rho with exterior T
212fcb2c22aSJames Wright } else {
213fcb2c22aSJames Wright // rho specified, E_internal from solution
214f0b65372SJed Brown drho = 0;
215f0b65372SJed Brown dE = dq[4][i];
216f0b65372SJed Brown dP = dE * (gamma - 1.);
217f0b65372SJed Brown }
218f0b65372SJed Brown
21978e8b7daSJames Wright const CeedScalar u_normal = Dot3(normal, s.Y.velocity);
220f0b65372SJed Brown
221f0b65372SJed Brown v[0][i] = -wdetJb * drho * u_normal;
2222b916ea7SJeremy L Thompson for (int j = 0; j < 3; j++) {
22378e8b7daSJames Wright v[j + 1][i] = -wdetJb * (drho * u_normal * s.Y.velocity[j] + normal[j] * dP);
2242b916ea7SJeremy L Thompson }
225f0b65372SJed Brown v[4][i] = -wdetJb * u_normal * (dE + dP);
226ade49511SJames Wright }
227f0b65372SJed Brown return 0;
228f0b65372SJed Brown }
229