1dc936754SJeremy L Thompson // Copyright (c) 2017-2024, 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 #include <ceed.h> 112b916ea7SJeremy L Thompson 12e0d1a4dfSLeila Ghaffari #include "newtonian_state.h" 1315a3537eSJed Brown #include "newtonian_types.h" 14704b8bbeSJames Wright #include "utils.h" 15bb8a0c61SJames Wright 160d850f2eSLeila Ghaffari #define BLASIUS_MAX_N_CHEBYSHEV 50 170d850f2eSLeila Ghaffari 18bb8a0c61SJames Wright typedef struct BlasiusContext_ *BlasiusContext; 19bb8a0c61SJames Wright struct BlasiusContext_ { 20bb8a0c61SJames Wright bool implicit; // !< Using implicit timesteping or not 212acc7cbcSKenneth E. Jansen bool weakT; // !< flag to set Temperature weakly at inflow 22bb8a0c61SJames Wright CeedScalar delta0; // !< Boundary layer height at inflow 23fcb2c22aSJames Wright State S_infty; 24e0d1a4dfSLeila Ghaffari CeedScalar T_wall; // !< Temperature at the wall 25ef2c71fdSJames Wright CeedScalar x_inflow; // !< Location of inflow in x 26e0d1a4dfSLeila Ghaffari CeedScalar n_cheb; // !< Number of Chebyshev terms 270d850f2eSLeila Ghaffari CeedScalar *X; // !< Chebyshev polynomial coordinate vector (CPU only) 28e0d1a4dfSLeila Ghaffari CeedScalar eta_max; // !< Maximum eta in the domain 290d850f2eSLeila Ghaffari CeedScalar Tf_cheb[BLASIUS_MAX_N_CHEBYSHEV]; // !< Chebyshev coefficient for f 300d850f2eSLeila Ghaffari CeedScalar Th_cheb[BLASIUS_MAX_N_CHEBYSHEV - 1]; // !< Chebyshev coefficient for h 31bb8a0c61SJames Wright struct NewtonianIdealGasContext_ newtonian_ctx; 32bb8a0c61SJames Wright }; 33bb8a0c61SJames Wright 34e0d1a4dfSLeila Ghaffari // ***************************************************************************** 3504e40bb6SJeremy L Thompson // This helper function evaluates Chebyshev polynomials with a set of coefficients with all their derivatives represented as a recurrence table. 36e0d1a4dfSLeila Ghaffari // ***************************************************************************** 372b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void ChebyshevEval(int N, const double *Tf, double x, double eta_max, double *f) { 38e0d1a4dfSLeila Ghaffari double dX_deta = 2 / eta_max; 39e0d1a4dfSLeila Ghaffari double table[4][3] = { 40e0d1a4dfSLeila Ghaffari // Chebyshev polynomials T_0, T_1, T_2 of the first kind in (-1,1) 412b916ea7SJeremy L Thompson {1, x, 2 * x * x - 1}, 422b916ea7SJeremy L Thompson {0, 1, 4 * x }, 432b916ea7SJeremy L Thompson {0, 0, 4 }, 442b916ea7SJeremy L Thompson {0, 0, 0 } 45e0d1a4dfSLeila Ghaffari }; 46e0d1a4dfSLeila Ghaffari for (int i = 0; i < 4; i++) { 47e0d1a4dfSLeila Ghaffari // i-th derivative of f 48e0d1a4dfSLeila Ghaffari f[i] = table[i][0] * Tf[0] + table[i][1] * Tf[1] + table[i][2] * Tf[2]; 49e0d1a4dfSLeila Ghaffari } 50e0d1a4dfSLeila Ghaffari for (int i = 3; i < N; i++) { 51e0d1a4dfSLeila Ghaffari // T_n(x) = 2xT_{n-1}(x) - T_{n-2}(x) 52e0d1a4dfSLeila Ghaffari table[0][i % 3] = 2 * x * table[0][(i - 1) % 3] - table[0][(i - 2) % 3]; 53e0d1a4dfSLeila Ghaffari // Differentiate Chebyshev polynomials with the recurrence relation 54e0d1a4dfSLeila Ghaffari for (int j = 1; j < 4; j++) { 55e0d1a4dfSLeila Ghaffari // T'_{n}(x)/n = 2T_{n-1}(x) + T'_{n-2}(x)/n-2 56e0d1a4dfSLeila Ghaffari table[j][i % 3] = i * (2 * table[j - 1][(i - 1) % 3] + table[j][(i - 2) % 3] / (i - 2)); 57e0d1a4dfSLeila Ghaffari } 58e0d1a4dfSLeila Ghaffari for (int j = 0; j < 4; j++) { 59e0d1a4dfSLeila Ghaffari f[j] += table[j][i % 3] * Tf[i]; 60bb8a0c61SJames Wright } 61bb8a0c61SJames Wright } 62e0d1a4dfSLeila Ghaffari for (int i = 1; i < 4; i++) { 63e0d1a4dfSLeila Ghaffari // Transform derivatives from Chebyshev [-1, 1] to [0, eta_max]. 64e0d1a4dfSLeila Ghaffari for (int j = 0; j < i; j++) f[i] *= dX_deta; 65e0d1a4dfSLeila Ghaffari } 66bb8a0c61SJames Wright } 67bb8a0c61SJames Wright 68e0d1a4dfSLeila Ghaffari // ***************************************************************************** 69e0d1a4dfSLeila Ghaffari // This helper function computes the Blasius boundary layer solution. 70e0d1a4dfSLeila Ghaffari // ***************************************************************************** 712b916ea7SJeremy L Thompson State CEED_QFUNCTION_HELPER(BlasiusSolution)(const BlasiusContext blasius, const CeedScalar x[3], const CeedScalar x0, const CeedScalar x_inflow, 720d850f2eSLeila Ghaffari const CeedScalar rho_infty, CeedScalar *t12) { 73e0d1a4dfSLeila Ghaffari CeedInt N = blasius->n_cheb; 740d850f2eSLeila Ghaffari CeedScalar mu = blasius->newtonian_ctx.mu; 75fcb2c22aSJames Wright State S_infty = blasius->S_infty; 760d850f2eSLeila Ghaffari CeedScalar nu = mu / rho_infty; 77*64667825SJames Wright CeedScalar U_infty = Norm3(S_infty.Y.velocity); 78fcb2c22aSJames Wright CeedScalar eta = x[1] * sqrt(U_infty / (nu * (x0 + x[0] - x_inflow))); 79e0d1a4dfSLeila Ghaffari CeedScalar X = 2 * (eta / blasius->eta_max) - 1.; 80e0d1a4dfSLeila Ghaffari CeedScalar Rd = GasConstant(&blasius->newtonian_ctx); 81e0d1a4dfSLeila Ghaffari 82e0d1a4dfSLeila Ghaffari CeedScalar f[4], h[4]; 83e0d1a4dfSLeila Ghaffari ChebyshevEval(N, blasius->Tf_cheb, X, blasius->eta_max, f); 84e0d1a4dfSLeila Ghaffari ChebyshevEval(N - 1, blasius->Th_cheb, X, blasius->eta_max, h); 85e0d1a4dfSLeila Ghaffari 86fcb2c22aSJames Wright *t12 = mu * U_infty * f[2] * sqrt(U_infty / (nu * (x0 + x[0] - x_inflow))); 87e0d1a4dfSLeila Ghaffari 88e0d1a4dfSLeila Ghaffari CeedScalar Y[5]; 89fcb2c22aSJames Wright Y[1] = U_infty * f[1]; 90fcb2c22aSJames Wright Y[2] = 0.5 * sqrt(nu * U_infty / (x0 + x[0] - x_inflow)) * (eta * f[1] - f[0]); 91e0d1a4dfSLeila Ghaffari Y[3] = 0.; 92fcb2c22aSJames Wright Y[4] = S_infty.Y.temperature * h[0]; 930d850f2eSLeila Ghaffari Y[0] = rho_infty / h[0] * Rd * Y[4]; 94edcfef1bSKenneth E. Jansen return StateFromY(&blasius->newtonian_ctx, Y); 95bb8a0c61SJames Wright } 96bb8a0c61SJames Wright 97bb8a0c61SJames Wright // ***************************************************************************** 98bb8a0c61SJames Wright // This QFunction sets a Blasius boundary layer for the initial condition 99bb8a0c61SJames Wright // ***************************************************************************** 1002b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsBlasius)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 101bb8a0c61SJames Wright const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 102bb8a0c61SJames Wright CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 103bb8a0c61SJames Wright 104bb8a0c61SJames Wright const BlasiusContext context = (BlasiusContext)ctx; 10533796533SJames Wright const NewtonianIdealGasContext gas = &context->newtonian_ctx; 106bb8a0c61SJames Wright const CeedScalar mu = context->newtonian_ctx.mu; 107bb8a0c61SJames Wright const CeedScalar delta0 = context->delta0; 108ef2c71fdSJames Wright const CeedScalar x_inflow = context->x_inflow; 109e0d1a4dfSLeila Ghaffari CeedScalar t12; 110bb8a0c61SJames Wright 111fcb2c22aSJames Wright const State S_infty = context->S_infty; 112*64667825SJames Wright const CeedScalar U_infty = Norm3(S_infty.Y.velocity); 113bb8a0c61SJames Wright 114fcb2c22aSJames Wright const CeedScalar x0 = U_infty * S_infty.U.density / (mu * 25 / Square(delta0)); 11533796533SJames Wright 11633796533SJames Wright CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 11733796533SJames Wright const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]}; 118fcb2c22aSJames Wright State s = BlasiusSolution(context, x, x0, x_inflow, S_infty.U.density, &t12); 119a541e550SJames Wright CeedScalar q[5]; 12033796533SJames Wright 1219b103f75SJames Wright StateToQ(gas, s, q, gas->state_var); 12233796533SJames Wright for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j]; 12333796533SJames Wright } 124bb8a0c61SJames Wright return 0; 125bb8a0c61SJames Wright } 126bb8a0c61SJames Wright 127bb8a0c61SJames Wright // ***************************************************************************** 1282b916ea7SJeremy L Thompson CEED_QFUNCTION(Blasius_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 1294b96a86bSJames Wright const BlasiusContext context = (BlasiusContext)ctx; 1303d65b166SJames Wright const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 131ade49511SJames Wright const CeedScalar(*q_data_sur) = in[2]; 1323d65b166SJames Wright const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3]; 133bb8a0c61SJames Wright CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 1344b96a86bSJames Wright CeedScalar(*jac_data_sur) = context->newtonian_ctx.is_implicit ? out[1] : NULL; 1353d65b166SJames Wright 136ade49511SJames Wright const bool is_implicit = context->implicit; 137512c8ec7SJames Wright const NewtonianIdealGasContext gas = &context->newtonian_ctx; 138fcb2c22aSJames Wright State S_infty = context->S_infty; 139fcb2c22aSJames Wright const CeedScalar rho_0 = S_infty.U.density; 140*64667825SJames Wright const CeedScalar U_infty = Norm3(S_infty.Y.velocity); 141fcb2c22aSJames Wright const CeedScalar x0 = U_infty * rho_0 / (gas->mu * 25 / Square(context->delta0)); 14280f5d3cbSJames Wright const CeedScalar zeros[11] = {0.}; 143bb8a0c61SJames Wright 1443d65b166SJames Wright CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 145ade49511SJames Wright CeedScalar wdetJb, norm[3]; 146ade49511SJames Wright QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm); 147ade49511SJames Wright wdetJb *= is_implicit ? -1. : 1.; 148bb8a0c61SJames Wright 1492acc7cbcSKenneth E. Jansen // Calculate inflow values 150e0d1a4dfSLeila Ghaffari const CeedScalar x[3] = {X[0][i], X[1][i], 0.}; 151bb8a0c61SJames Wright CeedScalar t12; 152512c8ec7SJames Wright State s = BlasiusSolution(context, x, x0, context->x_inflow, rho_0, &t12); 1530d850f2eSLeila Ghaffari CeedScalar qi[5]; 1540d850f2eSLeila Ghaffari for (CeedInt j = 0; j < 5; j++) qi[j] = q[j][i]; 155edcfef1bSKenneth E. Jansen State s_int = StateFromU(gas, qi); 156bb8a0c61SJames Wright 1572acc7cbcSKenneth E. Jansen // enabling user to choose between weak T and weak rho inflow 158512c8ec7SJames Wright if (context->weakT) { // density from the current solution 1590d850f2eSLeila Ghaffari s.U.density = s_int.U.density; 160edcfef1bSKenneth E. Jansen s.Y = StatePrimitiveFromConservative(gas, s.U); 1610d850f2eSLeila Ghaffari } else { // Total energy from current solution 1620d850f2eSLeila Ghaffari s.U.E_total = s_int.U.E_total; 163edcfef1bSKenneth E. Jansen s.Y = StatePrimitiveFromConservative(gas, s.U); 1642acc7cbcSKenneth E. Jansen } 1650d850f2eSLeila Ghaffari 1660d850f2eSLeila Ghaffari StateConservative Flux_inviscid[3]; 1670d850f2eSLeila Ghaffari FluxInviscid(&context->newtonian_ctx, s, Flux_inviscid); 168bb8a0c61SJames Wright 1692b916ea7SJeremy L Thompson const CeedScalar stress[3][3] = { 1702b916ea7SJeremy L Thompson {0, t12, 0}, 1712b916ea7SJeremy L Thompson {t12, 0, 0}, 1722b916ea7SJeremy L Thompson {0, 0, 0} 1732b916ea7SJeremy L Thompson }; 1740d850f2eSLeila Ghaffari const CeedScalar Fe[3] = {0}; // TODO: viscous energy flux needs grad temperature 1750d850f2eSLeila Ghaffari CeedScalar Flux[5]; 1760d850f2eSLeila Ghaffari FluxTotal_Boundary(Flux_inviscid, stress, Fe, norm, Flux); 1772b916ea7SJeremy L Thompson for (CeedInt j = 0; j < 5; j++) v[j][i] = -wdetJb * Flux[j]; 1784b96a86bSJames Wright if (is_implicit) StoredValuesPack(Q, i, 0, 11, zeros, jac_data_sur); 179ade49511SJames Wright } 180bb8a0c61SJames Wright return 0; 181bb8a0c61SJames Wright } 182bb8a0c61SJames Wright 183e0d1a4dfSLeila Ghaffari // ***************************************************************************** 1842b916ea7SJeremy L Thompson CEED_QFUNCTION(Blasius_Inflow_Jacobian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 1853d65b166SJames Wright const CeedScalar(*dq)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0]; 186ade49511SJames Wright const CeedScalar(*q_data_sur) = in[2]; 1873d65b166SJames Wright const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3]; 188f0b65372SJed Brown CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 1893d65b166SJames Wright 190f0b65372SJed Brown const BlasiusContext context = (BlasiusContext)ctx; 191512c8ec7SJames Wright const NewtonianIdealGasContext gas = &context->newtonian_ctx; 192ade49511SJames Wright const bool is_implicit = context->implicit; 193512c8ec7SJames Wright const CeedScalar Rd = GasConstant(gas); 194512c8ec7SJames Wright const CeedScalar gamma = HeatCapacityRatio(gas); 195fcb2c22aSJames Wright const State S_infty = context->S_infty; 196fcb2c22aSJames Wright const CeedScalar rho_0 = S_infty.U.density; 197*64667825SJames Wright const CeedScalar U_infty = Norm3(S_infty.Y.velocity); 198fcb2c22aSJames Wright const CeedScalar x0 = U_infty * rho_0 / (gas->mu * 25 / Square(context->delta0)); 199f0b65372SJed Brown 2003d65b166SJames Wright CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 201ade49511SJames Wright CeedScalar wdetJb, norm[3]; 202ade49511SJames Wright QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm); 203ade49511SJames Wright wdetJb *= is_implicit ? -1. : 1.; 204f0b65372SJed Brown 205f0b65372SJed Brown // Calculate inflow values 2060d850f2eSLeila Ghaffari const CeedScalar x[3] = {X[0][i], X[1][i], X[2][i]}; 207f0b65372SJed Brown CeedScalar t12; 208e0d1a4dfSLeila Ghaffari State s = BlasiusSolution(context, x, x0, 0, rho_0, &t12); 209f0b65372SJed Brown 210f0b65372SJed Brown // enabling user to choose between weak T and weak rho inflow 211f0b65372SJed Brown CeedScalar drho, dE, dP; 212512c8ec7SJames Wright if (context->weakT) { 213f0b65372SJed Brown // rho should be from the current solution 214f0b65372SJed Brown drho = dq[0][i]; 215fcb2c22aSJames Wright CeedScalar dE_internal = drho * gas->cv * S_infty.Y.temperature; 216e0d1a4dfSLeila Ghaffari CeedScalar dE_kinetic = .5 * drho * Dot3(s.Y.velocity, s.Y.velocity); 217f0b65372SJed Brown dE = dE_internal + dE_kinetic; 218fcb2c22aSJames Wright dP = drho * Rd * S_infty.Y.temperature; // interior rho with exterior T 219fcb2c22aSJames Wright } else { 220fcb2c22aSJames Wright // rho specified, E_internal from solution 221f0b65372SJed Brown drho = 0; 222f0b65372SJed Brown dE = dq[4][i]; 223f0b65372SJed Brown dP = dE * (gamma - 1.); 224f0b65372SJed Brown } 225f0b65372SJed Brown 226e0d1a4dfSLeila Ghaffari const CeedScalar u_normal = Dot3(norm, s.Y.velocity); 227f0b65372SJed Brown 228f0b65372SJed Brown v[0][i] = -wdetJb * drho * u_normal; 2292b916ea7SJeremy L Thompson for (int j = 0; j < 3; j++) { 230e0d1a4dfSLeila Ghaffari v[j + 1][i] = -wdetJb * (drho * u_normal * s.Y.velocity[j] + norm[j] * dP); 2312b916ea7SJeremy L Thompson } 232f0b65372SJed Brown v[4][i] = -wdetJb * u_normal * (dE + dP); 233ade49511SJames Wright } 234f0b65372SJed Brown return 0; 235f0b65372SJed Brown } 236