1493642f1SJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2493642f1SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3493642f1SJames Wright // 4493642f1SJames Wright // SPDX-License-Identifier: BSD-2-Clause 5493642f1SJames Wright // 6493642f1SJames Wright // This file is part of CEED: http://github.com/ceed 7493642f1SJames Wright 8493642f1SJames Wright /// @file 9493642f1SJames Wright /// Implementation of the Synthetic Turbulence Generation (STG) algorithm 10493642f1SJames Wright /// presented in Shur et al. 2014 11493642f1SJames Wright // 12493642f1SJames Wright /// SetupSTG_Rand reads in the input files and fills in STGShur14Context. Then 13493642f1SJames Wright /// STGShur14_CalcQF is run over quadrature points. Before the program exits, 14493642f1SJames Wright /// TearDownSTG is run to free the memory of the allocated arrays. 15493642f1SJames Wright 16493642f1SJames Wright #ifndef stg_shur14_h 17493642f1SJames Wright #define stg_shur14_h 18493642f1SJames Wright 19493642f1SJames Wright #include <ceed.h> 20d0cce58aSJeremy L Thompson #include <math.h> 21493642f1SJames Wright #include <stdlib.h> 22*2b916ea7SJeremy L Thompson 23493642f1SJames Wright #include "stg_shur14_type.h" 24704b8bbeSJames Wright #include "utils.h" 25493642f1SJames Wright 26493642f1SJames Wright #define STG_NMODES_MAX 1024 27493642f1SJames Wright 28493642f1SJames Wright /* 29493642f1SJames Wright * @brief Interpolate quantities from input profile to given location 30493642f1SJames Wright * 31c77f3192SJames Wright * Assumed that prof_wd[i+1] > prof_wd[i] and prof_wd[0] = 0 32c77f3192SJames Wright * If wall_dist > prof_wd[-1], then the interpolation takes the values at prof_wd[-1] 33493642f1SJames Wright * 34c77f3192SJames Wright * @param[in] wall_dist Distance to the nearest wall 35c77f3192SJames Wright * @param[out] ubar Mean velocity at wall_dist 36c77f3192SJames Wright * @param[out] cij Cholesky decomposition at wall_dist 37c77f3192SJames Wright * @param[out] eps Turbulent dissipation at wall_dist 38c77f3192SJames Wright * @param[out] lt Turbulent length scale at wall_dist 39493642f1SJames Wright * @param[in] stg_ctx STGShur14Context for the problem 40493642f1SJames Wright */ 41*2b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void InterpolateProfile(const CeedScalar wall_dist, CeedScalar ubar[3], CeedScalar cij[6], CeedScalar *eps, CeedScalar *lt, 42493642f1SJames Wright const STGShur14Context stg_ctx) { 43493642f1SJames Wright const CeedInt nprofs = stg_ctx->nprofs; 44c77f3192SJames Wright const CeedScalar *prof_wd = &stg_ctx->data[stg_ctx->offsets.wall_dist]; 45493642f1SJames Wright const CeedScalar *prof_eps = &stg_ctx->data[stg_ctx->offsets.eps]; 46493642f1SJames Wright const CeedScalar *prof_lt = &stg_ctx->data[stg_ctx->offsets.lt]; 47493642f1SJames Wright const CeedScalar *prof_ubar = &stg_ctx->data[stg_ctx->offsets.ubar]; 48493642f1SJames Wright const CeedScalar *prof_cij = &stg_ctx->data[stg_ctx->offsets.cij]; 49493642f1SJames Wright CeedInt idx = -1; 50493642f1SJames Wright 51493642f1SJames Wright for (CeedInt i = 0; i < nprofs; i++) { 52c77f3192SJames Wright if (wall_dist < prof_wd[i]) { 53493642f1SJames Wright idx = i; 54493642f1SJames Wright break; 55493642f1SJames Wright } 56493642f1SJames Wright } 57493642f1SJames Wright 58c77f3192SJames Wright if (idx > 0) { // y within the bounds of prof_wd 59493642f1SJames Wright //*INDENT-OFF* 60c77f3192SJames Wright CeedScalar coeff = (wall_dist - prof_wd[idx - 1]) / (prof_wd[idx] - prof_wd[idx - 1]); 61c77f3192SJames Wright 62493642f1SJames Wright ubar[0] = prof_ubar[0 * nprofs + idx - 1] + coeff * (prof_ubar[0 * nprofs + idx] - prof_ubar[0 * nprofs + idx - 1]); 63493642f1SJames Wright ubar[1] = prof_ubar[1 * nprofs + idx - 1] + coeff * (prof_ubar[1 * nprofs + idx] - prof_ubar[1 * nprofs + idx - 1]); 64493642f1SJames Wright ubar[2] = prof_ubar[2 * nprofs + idx - 1] + coeff * (prof_ubar[2 * nprofs + idx] - prof_ubar[2 * nprofs + idx - 1]); 65493642f1SJames Wright cij[0] = prof_cij[0 * nprofs + idx - 1] + coeff * (prof_cij[0 * nprofs + idx] - prof_cij[0 * nprofs + idx - 1]); 66493642f1SJames Wright cij[1] = prof_cij[1 * nprofs + idx - 1] + coeff * (prof_cij[1 * nprofs + idx] - prof_cij[1 * nprofs + idx - 1]); 67493642f1SJames Wright cij[2] = prof_cij[2 * nprofs + idx - 1] + coeff * (prof_cij[2 * nprofs + idx] - prof_cij[2 * nprofs + idx - 1]); 68493642f1SJames Wright cij[3] = prof_cij[3 * nprofs + idx - 1] + coeff * (prof_cij[3 * nprofs + idx] - prof_cij[3 * nprofs + idx - 1]); 69493642f1SJames Wright cij[4] = prof_cij[4 * nprofs + idx - 1] + coeff * (prof_cij[4 * nprofs + idx] - prof_cij[4 * nprofs + idx - 1]); 70493642f1SJames Wright cij[5] = prof_cij[5 * nprofs + idx - 1] + coeff * (prof_cij[5 * nprofs + idx] - prof_cij[5 * nprofs + idx - 1]); 71493642f1SJames Wright *eps = prof_eps[idx - 1] + coeff * (prof_eps[idx] - prof_eps[idx - 1]); 72493642f1SJames Wright *lt = prof_lt[idx - 1] + coeff * (prof_lt[idx] - prof_lt[idx - 1]); 73493642f1SJames Wright //*INDENT-ON* 74c77f3192SJames Wright } else { // y outside bounds of prof_wd 75493642f1SJames Wright ubar[0] = prof_ubar[1 * nprofs - 1]; 76493642f1SJames Wright ubar[1] = prof_ubar[2 * nprofs - 1]; 77493642f1SJames Wright ubar[2] = prof_ubar[3 * nprofs - 1]; 78493642f1SJames Wright cij[0] = prof_cij[1 * nprofs - 1]; 79493642f1SJames Wright cij[1] = prof_cij[2 * nprofs - 1]; 80493642f1SJames Wright cij[2] = prof_cij[3 * nprofs - 1]; 81493642f1SJames Wright cij[3] = prof_cij[4 * nprofs - 1]; 82493642f1SJames Wright cij[4] = prof_cij[5 * nprofs - 1]; 83493642f1SJames Wright cij[5] = prof_cij[6 * nprofs - 1]; 84493642f1SJames Wright *eps = prof_eps[nprofs - 1]; 85493642f1SJames Wright *lt = prof_lt[nprofs - 1]; 86493642f1SJames Wright } 87493642f1SJames Wright } 88493642f1SJames Wright 89493642f1SJames Wright /* 9071cd6200SJames Wright * @brief Calculate spectrum coefficient, qn 9171cd6200SJames Wright * 9271cd6200SJames Wright * Calculates q_n at a given distance to the wall 9371cd6200SJames Wright * 9471cd6200SJames Wright * @param[in] kappa nth wavenumber 9571cd6200SJames Wright * @param[in] dkappa Difference between wavenumbers 9671cd6200SJames Wright * @param[in] keta Dissipation wavenumber 9771cd6200SJames Wright * @param[in] kcut Mesh-induced cutoff wavenumber 9871cd6200SJames Wright * @param[in] ke Energy-containing wavenumber 9971cd6200SJames Wright * @param[in] Ektot Total turbulent kinetic energy of spectrum 10071cd6200SJames Wright * @returns qn Spectrum coefficient 10171cd6200SJames Wright */ 102*2b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Calc_qn(const CeedScalar kappa, const CeedScalar dkappa, const CeedScalar keta, const CeedScalar kcut, 10370b0cb14SJames Wright const CeedScalar ke, const CeedScalar Ektot_inv) { 104*2b916ea7SJeremy L Thompson const CeedScalar feta_x_fcut = exp(-Square(12 * kappa / keta) - Cube(4 * Max(kappa - 0.9 * kcut, 0) / kcut)); 105*2b916ea7SJeremy L Thompson return pow(kappa / ke, 4.) * pow(1 + 2.4 * Square(kappa / ke), -17. / 6) * feta_x_fcut * dkappa * Ektot_inv; 10671cd6200SJames Wright } 10771cd6200SJames Wright 10871cd6200SJames Wright // Calculate hmax, ke, keta, and kcut 109*2b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void SpectrumConstants(const CeedScalar wall_dist, const CeedScalar eps, const CeedScalar lt, const CeedScalar h[3], 110*2b916ea7SJeremy L Thompson const CeedScalar nu, CeedScalar *hmax, CeedScalar *ke, CeedScalar *keta, CeedScalar *kcut) { 11171cd6200SJames Wright *hmax = Max(Max(h[0], h[1]), h[2]); 112c77f3192SJames Wright *ke = wall_dist == 0 ? 1e16 : 2 * M_PI / Min(2 * wall_dist, 3 * lt); 11371cd6200SJames Wright *keta = 2 * M_PI * pow(Cube(nu) / eps, -0.25); 114c77f3192SJames Wright *kcut = M_PI / Min(Max(Max(h[1], h[2]), 0.3 * (*hmax)) + 0.1 * wall_dist, *hmax); 11571cd6200SJames Wright } 11671cd6200SJames Wright 11771cd6200SJames Wright /* 118493642f1SJames Wright * @brief Calculate spectrum coefficients for STG 119493642f1SJames Wright * 120493642f1SJames Wright * Calculates q_n at a given distance to the wall 121493642f1SJames Wright * 122c77f3192SJames Wright * @param[in] wall_dist Distance to the nearest wall 123c77f3192SJames Wright * @param[in] eps Turbulent dissipation w/rt wall_dist 124c77f3192SJames Wright * @param[in] lt Turbulent length scale w/rt wall_dist 125493642f1SJames Wright * @param[in] h Element lengths in coordinate directions 126493642f1SJames Wright * @param[in] nu Dynamic Viscosity; 127493642f1SJames Wright * @param[in] stg_ctx STGShur14Context for the problem 128493642f1SJames Wright * @param[out] qn Spectrum coefficients, [nmodes] 129493642f1SJames Wright */ 130*2b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void CalcSpectrum(const CeedScalar wall_dist, const CeedScalar eps, const CeedScalar lt, const CeedScalar h[3], 131493642f1SJames Wright const CeedScalar nu, CeedScalar qn[], const STGShur14Context stg_ctx) { 132493642f1SJames Wright const CeedInt nmodes = stg_ctx->nmodes; 133493642f1SJames Wright const CeedScalar *kappa = &stg_ctx->data[stg_ctx->offsets.kappa]; 13471cd6200SJames Wright CeedScalar hmax, ke, keta, kcut, Ektot = 0.0; 135*2b916ea7SJeremy L Thompson 136c77f3192SJames Wright SpectrumConstants(wall_dist, eps, lt, h, nu, &hmax, &ke, &keta, &kcut); 137493642f1SJames Wright 138493642f1SJames Wright for (CeedInt n = 0; n < nmodes; n++) { 13971cd6200SJames Wright const CeedScalar dkappa = n == 0 ? kappa[0] : kappa[n] - kappa[n - 1]; 14071cd6200SJames Wright qn[n] = Calc_qn(kappa[n], dkappa, keta, kcut, ke, 1.0); 141493642f1SJames Wright Ektot += qn[n]; 142493642f1SJames Wright } 143493642f1SJames Wright 1440a8dc919SJames Wright if (Ektot == 0) return; 145493642f1SJames Wright for (CeedInt n = 0; n < nmodes; n++) qn[n] /= Ektot; 146493642f1SJames Wright } 147493642f1SJames Wright 148493642f1SJames Wright /****************************************************** 149493642f1SJames Wright * @brief Calculate u(x,t) for STG inflow condition 150493642f1SJames Wright * 151493642f1SJames Wright * @param[in] X Location to evaluate u(X,t) 152493642f1SJames Wright * @param[in] t Time to evaluate u(X,t) 153493642f1SJames Wright * @param[in] ubar Mean velocity at X 154493642f1SJames Wright * @param[in] cij Cholesky decomposition at X 155493642f1SJames Wright * @param[in] qn Wavemode amplitudes at X, [nmodes] 156493642f1SJames Wright * @param[out] u Velocity at X and t 157493642f1SJames Wright * @param[in] stg_ctx STGShur14Context for the problem 158493642f1SJames Wright */ 159*2b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void STGShur14_Calc(const CeedScalar X[3], const CeedScalar t, const CeedScalar ubar[3], const CeedScalar cij[6], 160*2b916ea7SJeremy L Thompson const CeedScalar qn[], CeedScalar u[3], const STGShur14Context stg_ctx) { 161493642f1SJames Wright //*INDENT-OFF* 162493642f1SJames Wright const CeedInt nmodes = stg_ctx->nmodes; 163493642f1SJames Wright const CeedScalar *kappa = &stg_ctx->data[stg_ctx->offsets.kappa]; 164493642f1SJames Wright const CeedScalar *phi = &stg_ctx->data[stg_ctx->offsets.phi]; 165493642f1SJames Wright const CeedScalar *sigma = &stg_ctx->data[stg_ctx->offsets.sigma]; 166493642f1SJames Wright const CeedScalar *d = &stg_ctx->data[stg_ctx->offsets.d]; 167493642f1SJames Wright //*INDENT-ON* 168493642f1SJames Wright CeedScalar xdotd, vp[3] = {0.}; 169493642f1SJames Wright CeedScalar xhat[] = {0., X[1], X[2]}; 170493642f1SJames Wright 171*2b916ea7SJeremy L Thompson CeedPragmaSIMD for (CeedInt n = 0; n < nmodes; n++) { 172493642f1SJames Wright xhat[0] = (X[0] - stg_ctx->u0 * t) * Max(2 * kappa[0] / kappa[n], 0.1); 173493642f1SJames Wright xdotd = 0.; 174493642f1SJames Wright for (CeedInt i = 0; i < 3; i++) xdotd += d[i * nmodes + n] * xhat[i]; 175493642f1SJames Wright const CeedScalar cos_kxdp = cos(kappa[n] * xdotd + phi[n]); 1760a8dc919SJames Wright vp[0] += sqrt(qn[n]) * sigma[0 * nmodes + n] * cos_kxdp; 1770a8dc919SJames Wright vp[1] += sqrt(qn[n]) * sigma[1 * nmodes + n] * cos_kxdp; 1780a8dc919SJames Wright vp[2] += sqrt(qn[n]) * sigma[2 * nmodes + n] * cos_kxdp; 179493642f1SJames Wright } 1800a8dc919SJames Wright for (CeedInt i = 0; i < 3; i++) vp[i] *= 2 * sqrt(1.5); 181493642f1SJames Wright 182493642f1SJames Wright u[0] = ubar[0] + cij[0] * vp[0]; 183493642f1SJames Wright u[1] = ubar[1] + cij[3] * vp[0] + cij[1] * vp[1]; 184493642f1SJames Wright u[2] = ubar[2] + cij[4] * vp[0] + cij[5] * vp[1] + cij[2] * vp[2]; 185493642f1SJames Wright } 186493642f1SJames Wright 1878eea80fcSJames Wright /****************************************************** 1888eea80fcSJames Wright * @brief Calculate u(x,t) for STG inflow condition 1898eea80fcSJames Wright * 1908eea80fcSJames Wright * @param[in] X Location to evaluate u(X,t) 1918eea80fcSJames Wright * @param[in] t Time to evaluate u(X,t) 1928eea80fcSJames Wright * @param[in] ubar Mean velocity at X 1938eea80fcSJames Wright * @param[in] cij Cholesky decomposition at X 194c77f3192SJames Wright * @param[in] Ektot Total spectrum energy at this location 195c77f3192SJames Wright * @param[in] h Element size in 3 directions 196c77f3192SJames Wright * @param[in] wall_dist Distance to closest wall 197c77f3192SJames Wright * @param[in] eps Turbulent dissipation 198c77f3192SJames Wright * @param[in] lt Turbulent length scale 1998eea80fcSJames Wright * @param[out] u Velocity at X and t 2008eea80fcSJames Wright * @param[in] stg_ctx STGShur14Context for the problem 2018eea80fcSJames Wright */ 202*2b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void STGShur14_Calc_PrecompEktot(const CeedScalar X[3], const CeedScalar t, const CeedScalar ubar[3], const CeedScalar cij[6], 203c77f3192SJames Wright const CeedScalar Ektot, const CeedScalar h[3], const CeedScalar wall_dist, 2048eea80fcSJames Wright const CeedScalar eps, const CeedScalar lt, const CeedScalar nu, CeedScalar u[3], 2058eea80fcSJames Wright const STGShur14Context stg_ctx) { 2068eea80fcSJames Wright //*INDENT-OFF* 2078eea80fcSJames Wright const CeedInt nmodes = stg_ctx->nmodes; 2088eea80fcSJames Wright const CeedScalar *kappa = &stg_ctx->data[stg_ctx->offsets.kappa]; 2098eea80fcSJames Wright const CeedScalar *phi = &stg_ctx->data[stg_ctx->offsets.phi]; 2108eea80fcSJames Wright const CeedScalar *sigma = &stg_ctx->data[stg_ctx->offsets.sigma]; 2118eea80fcSJames Wright const CeedScalar *d = &stg_ctx->data[stg_ctx->offsets.d]; 2128eea80fcSJames Wright //*INDENT-ON* 2138eea80fcSJames Wright CeedScalar hmax, ke, keta, kcut; 214c77f3192SJames Wright SpectrumConstants(wall_dist, eps, lt, h, nu, &hmax, &ke, &keta, &kcut); 2158eea80fcSJames Wright CeedScalar xdotd, vp[3] = {0.}; 2168eea80fcSJames Wright CeedScalar xhat[] = {0., X[1], X[2]}; 2178eea80fcSJames Wright 218*2b916ea7SJeremy L Thompson CeedPragmaSIMD for (CeedInt n = 0; n < nmodes; n++) { 2198eea80fcSJames Wright xhat[0] = (X[0] - stg_ctx->u0 * t) * Max(2 * kappa[0] / kappa[n], 0.1); 2208eea80fcSJames Wright xdotd = 0.; 2218eea80fcSJames Wright for (CeedInt i = 0; i < 3; i++) xdotd += d[i * nmodes + n] * xhat[i]; 2228eea80fcSJames Wright const CeedScalar cos_kxdp = cos(kappa[n] * xdotd + phi[n]); 2238eea80fcSJames Wright const CeedScalar dkappa = n == 0 ? kappa[0] : kappa[n] - kappa[n - 1]; 2248eea80fcSJames Wright const CeedScalar qn = Calc_qn(kappa[n], dkappa, keta, kcut, ke, Ektot); 2258eea80fcSJames Wright vp[0] += sqrt(qn) * sigma[0 * nmodes + n] * cos_kxdp; 2268eea80fcSJames Wright vp[1] += sqrt(qn) * sigma[1 * nmodes + n] * cos_kxdp; 2278eea80fcSJames Wright vp[2] += sqrt(qn) * sigma[2 * nmodes + n] * cos_kxdp; 2288eea80fcSJames Wright } 2298eea80fcSJames Wright for (CeedInt i = 0; i < 3; i++) vp[i] *= 2 * sqrt(1.5); 2308eea80fcSJames Wright 2318eea80fcSJames Wright u[0] = ubar[0] + cij[0] * vp[0]; 2328eea80fcSJames Wright u[1] = ubar[1] + cij[3] * vp[0] + cij[1] * vp[1]; 2338eea80fcSJames Wright u[2] = ubar[2] + cij[4] * vp[0] + cij[5] * vp[1] + cij[2] * vp[2]; 2348eea80fcSJames Wright } 2358eea80fcSJames Wright 23670b0cb14SJames Wright // Create preprocessed input for the stg calculation 23770b0cb14SJames Wright // 23870b0cb14SJames Wright // stg_data[0] = 1 / Ektot (inverse of total spectrum energy) 239*2b916ea7SJeremy L Thompson CEED_QFUNCTION(Preprocess_STGShur14)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 2408eea80fcSJames Wright //*INDENT-OFF* 241*2b916ea7SJeremy L Thompson const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], (*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1]; 2428eea80fcSJames Wright 2438eea80fcSJames Wright CeedScalar(*stg_data) = (CeedScalar(*))out[0]; 2448eea80fcSJames Wright 2458eea80fcSJames Wright //*INDENT-ON* 2468eea80fcSJames Wright CeedScalar ubar[3], cij[6], eps, lt; 2478eea80fcSJames Wright const STGShur14Context stg_ctx = (STGShur14Context)ctx; 2488eea80fcSJames Wright const CeedScalar dx = stg_ctx->dx; 2498eea80fcSJames Wright const CeedScalar mu = stg_ctx->newtonian_ctx.mu; 2508eea80fcSJames Wright const CeedScalar theta0 = stg_ctx->theta0; 2518eea80fcSJames Wright const CeedScalar P0 = stg_ctx->P0; 2528eea80fcSJames Wright const CeedScalar cv = stg_ctx->newtonian_ctx.cv; 2538eea80fcSJames Wright const CeedScalar cp = stg_ctx->newtonian_ctx.cp; 2548eea80fcSJames Wright const CeedScalar Rd = cp - cv; 2558eea80fcSJames Wright const CeedScalar rho = P0 / (Rd * theta0); 2568eea80fcSJames Wright const CeedScalar nu = mu / rho; 2578eea80fcSJames Wright 2588eea80fcSJames Wright const CeedInt nmodes = stg_ctx->nmodes; 2598eea80fcSJames Wright const CeedScalar *kappa = &stg_ctx->data[stg_ctx->offsets.kappa]; 2609eeef72bSJames Wright CeedScalar hmax, ke, keta, kcut; 2618eea80fcSJames Wright 262*2b916ea7SJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 263c77f3192SJames Wright const CeedScalar wall_dist = x[1][i]; 2648eea80fcSJames Wright const CeedScalar dXdx[2][3] = { 2658eea80fcSJames Wright {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]}, 2668eea80fcSJames Wright {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]} 2678eea80fcSJames Wright }; 2688eea80fcSJames Wright 2698eea80fcSJames Wright CeedScalar h[3]; 2708eea80fcSJames Wright h[0] = dx; 271*2b916ea7SJeremy L Thompson for (CeedInt j = 1; j < 3; j++) h[j] = 2 / sqrt(dXdx[0][j] * dXdx[0][j] + dXdx[1][j] * dXdx[1][j]); 2728eea80fcSJames Wright 273c77f3192SJames Wright InterpolateProfile(wall_dist, ubar, cij, &eps, <, stg_ctx); 274c77f3192SJames Wright SpectrumConstants(wall_dist, eps, lt, h, nu, &hmax, &ke, &keta, &kcut); 2758eea80fcSJames Wright 2768eea80fcSJames Wright // Calculate total TKE per spectrum 2772f638ed2SJames Wright CeedScalar Ek_tot = 0; 278*2b916ea7SJeremy L Thompson CeedPragmaSIMD for (CeedInt n = 0; n < nmodes; n++) { 2798eea80fcSJames Wright const CeedScalar dkappa = n == 0 ? kappa[0] : kappa[n] - kappa[n - 1]; 2802f638ed2SJames Wright Ek_tot += Calc_qn(kappa[n], dkappa, keta, kcut, ke, 1.0); 2818eea80fcSJames Wright } 2822f638ed2SJames Wright // avoid underflowed and poorly defined spectrum coefficients 2832f638ed2SJames Wright stg_data[i] = Ek_tot != 0 ? 1 / Ek_tot : 0; 2848eea80fcSJames Wright } 2858eea80fcSJames Wright return 0; 2868eea80fcSJames Wright } 2878eea80fcSJames Wright 28843bff553SJames Wright // Extrude the STGInflow profile through out the domain for an initial condition 289*2b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsSTG)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 29043bff553SJames Wright // Inputs 291*2b916ea7SJeremy L Thompson const CeedScalar(*x)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1]; 29243bff553SJames Wright // Outputs 29343bff553SJames Wright CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 29443bff553SJames Wright 29543bff553SJames Wright const STGShur14Context stg_ctx = (STGShur14Context)ctx; 296d4e0f297SJames Wright CeedScalar qn[STG_NMODES_MAX], u[3], ubar[3], cij[6], eps, lt; 297d4e0f297SJames Wright const CeedScalar dx = stg_ctx->dx; 298d4e0f297SJames Wright const CeedScalar time = stg_ctx->time; 29943bff553SJames Wright const CeedScalar theta0 = stg_ctx->theta0; 30043bff553SJames Wright const CeedScalar P0 = stg_ctx->P0; 301d4e0f297SJames Wright const CeedScalar mu = stg_ctx->newtonian_ctx.mu; 30243bff553SJames Wright const CeedScalar cv = stg_ctx->newtonian_ctx.cv; 30343bff553SJames Wright const CeedScalar cp = stg_ctx->newtonian_ctx.cp; 30443bff553SJames Wright const CeedScalar Rd = cp - cv; 30543bff553SJames Wright const CeedScalar rho = P0 / (Rd * theta0); 30643bff553SJames Wright 307*2b916ea7SJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 308d4e0f297SJames Wright const CeedScalar x_i[3] = {x[0][i], x[1][i], x[2][i]}; 309d4e0f297SJames Wright // *INDENT-OFF* 310*2b916ea7SJeremy L Thompson const CeedScalar dXdx[3][3] = { 311*2b916ea7SJeremy L Thompson {q_data[1][i], q_data[2][i], q_data[3][i]}, 312d4e0f297SJames Wright {q_data[4][i], q_data[5][i], q_data[6][i]}, 313d4e0f297SJames Wright {q_data[7][i], q_data[8][i], q_data[9][i]} 314d4e0f297SJames Wright }; 315d4e0f297SJames Wright // *INDENT-ON* 316d4e0f297SJames Wright 317d4e0f297SJames Wright CeedScalar h[3]; 318d4e0f297SJames Wright h[0] = dx; 319*2b916ea7SJeremy L Thompson for (CeedInt j = 1; j < 3; j++) h[j] = 2 / sqrt(Square(dXdx[0][j]) + Square(dXdx[1][j]) + Square(dXdx[2][j])); 320d4e0f297SJames Wright 321d4e0f297SJames Wright InterpolateProfile(x_i[1], ubar, cij, &eps, <, stg_ctx); 322d4e0f297SJames Wright if (stg_ctx->use_fluctuating_IC) { 323d4e0f297SJames Wright CalcSpectrum(x_i[1], eps, lt, h, mu / rho, qn, stg_ctx); 324d4e0f297SJames Wright STGShur14_Calc(x_i, time, ubar, cij, qn, u, stg_ctx); 325d4e0f297SJames Wright } else { 326d4e0f297SJames Wright for (CeedInt j = 0; j < 3; j++) u[j] = ubar[j]; 327d4e0f297SJames Wright } 32843bff553SJames Wright 3293636f6a4SJames Wright switch (stg_ctx->newtonian_ctx.state_var) { 3303636f6a4SJames Wright case STATEVAR_CONSERVATIVE: 33143bff553SJames Wright q0[0][i] = rho; 33243bff553SJames Wright q0[1][i] = u[0] * rho; 33343bff553SJames Wright q0[2][i] = u[1] * rho; 33443bff553SJames Wright q0[3][i] = u[2] * rho; 33543bff553SJames Wright q0[4][i] = rho * (0.5 * Dot3(u, u) + cv * theta0); 3363636f6a4SJames Wright break; 3373636f6a4SJames Wright 3383636f6a4SJames Wright case STATEVAR_PRIMITIVE: 3393636f6a4SJames Wright q0[0][i] = P0; 3403636f6a4SJames Wright q0[1][i] = u[0]; 3413636f6a4SJames Wright q0[2][i] = u[1]; 3423636f6a4SJames Wright q0[3][i] = u[2]; 3433636f6a4SJames Wright q0[4][i] = theta0; 3443636f6a4SJames Wright break; 34588243482SJames Wright } 34643bff553SJames Wright } // End of Quadrature Point Loop 34743bff553SJames Wright return 0; 34843bff553SJames Wright } 34943bff553SJames Wright 350493642f1SJames Wright /******************************************************************** 351493642f1SJames Wright * @brief QFunction to calculate the inflow boundary condition 352493642f1SJames Wright * 353493642f1SJames Wright * This will loop through quadrature points, calculate the wavemode amplitudes 354493642f1SJames Wright * at each location, then calculate the actual velocity. 355493642f1SJames Wright */ 356*2b916ea7SJeremy L Thompson CEED_QFUNCTION(STGShur14_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 357493642f1SJames Wright //*INDENT-OFF* 358*2b916ea7SJeremy L Thompson 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], 359dd64951cSJames Wright (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3]; 360493642f1SJames Wright 361*2b916ea7SJeremy L Thompson CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0], (*jac_data_sur)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[1]; 362493642f1SJames Wright 363493642f1SJames Wright //*INDENT-ON* 364493642f1SJames Wright 365493642f1SJames Wright const STGShur14Context stg_ctx = (STGShur14Context)ctx; 366493642f1SJames Wright CeedScalar qn[STG_NMODES_MAX], u[3], ubar[3], cij[6], eps, lt; 367493642f1SJames Wright const bool is_implicit = stg_ctx->is_implicit; 368493642f1SJames Wright const bool mean_only = stg_ctx->mean_only; 369493642f1SJames Wright const bool prescribe_T = stg_ctx->prescribe_T; 370493642f1SJames Wright const CeedScalar dx = stg_ctx->dx; 371493642f1SJames Wright const CeedScalar mu = stg_ctx->newtonian_ctx.mu; 372493642f1SJames Wright const CeedScalar time = stg_ctx->time; 373493642f1SJames Wright const CeedScalar theta0 = stg_ctx->theta0; 374493642f1SJames Wright const CeedScalar P0 = stg_ctx->P0; 375493642f1SJames Wright const CeedScalar cv = stg_ctx->newtonian_ctx.cv; 376493642f1SJames Wright const CeedScalar cp = stg_ctx->newtonian_ctx.cp; 377493642f1SJames Wright const CeedScalar Rd = cp - cv; 378493642f1SJames Wright const CeedScalar gamma = cp / cv; 379493642f1SJames Wright 380*2b916ea7SJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 381493642f1SJames Wright const CeedScalar rho = prescribe_T ? q[0][i] : P0 / (Rd * theta0); 382493642f1SJames Wright const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]}; 383493642f1SJames Wright const CeedScalar dXdx[2][3] = { 384493642f1SJames Wright {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]}, 385493642f1SJames Wright {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]} 386493642f1SJames Wright }; 387493642f1SJames Wright 388493642f1SJames Wright CeedScalar h[3]; 389493642f1SJames Wright h[0] = dx; 390*2b916ea7SJeremy L Thompson for (CeedInt j = 1; j < 3; j++) h[j] = 2 / sqrt(Square(dXdx[0][j]) + Square(dXdx[1][j])); 391493642f1SJames Wright 392493642f1SJames Wright InterpolateProfile(X[1][i], ubar, cij, &eps, <, stg_ctx); 393493642f1SJames Wright if (!mean_only) { 394493642f1SJames Wright CalcSpectrum(X[1][i], eps, lt, h, mu / rho, qn, stg_ctx); 395493642f1SJames Wright STGShur14_Calc(x, time, ubar, cij, qn, u, stg_ctx); 396493642f1SJames Wright } else { 397493642f1SJames Wright for (CeedInt j = 0; j < 3; j++) u[j] = ubar[j]; 398493642f1SJames Wright } 399493642f1SJames Wright 400a6e8f989SJames Wright const CeedScalar E_kinetic = .5 * rho * Dot3(u, u); 401493642f1SJames Wright CeedScalar E_internal, P; 402493642f1SJames Wright if (prescribe_T) { 403493642f1SJames Wright // Temperature is being set weakly (theta0) and for constant cv this sets E_internal 404493642f1SJames Wright E_internal = rho * cv * theta0; 405493642f1SJames Wright // Find pressure using 406493642f1SJames Wright P = rho * Rd * theta0; // interior rho with exterior T 407493642f1SJames Wright } else { 408493642f1SJames Wright E_internal = q[4][i] - E_kinetic; // uses prescribed rho and u, E from solution 409493642f1SJames Wright P = E_internal * (gamma - 1.); 410493642f1SJames Wright } 411493642f1SJames Wright 412493642f1SJames Wright const CeedScalar wdetJb = (is_implicit ? -1. : 1.) * q_data_sur[0][i]; 413493642f1SJames Wright // ---- Normal vect 414*2b916ea7SJeremy L Thompson const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]}; 415493642f1SJames Wright 416493642f1SJames Wright const CeedScalar E = E_internal + E_kinetic; 417493642f1SJames Wright 418493642f1SJames Wright // Velocity normal to the boundary 419a6e8f989SJames Wright const CeedScalar u_normal = Dot3(norm, u); 420a6e8f989SJames Wright 421493642f1SJames Wright // The Physics 422493642f1SJames Wright // Zero v so all future terms can safely sum into it 423493642f1SJames Wright for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.; 424493642f1SJames Wright 425493642f1SJames Wright // The Physics 426493642f1SJames Wright // -- Density 427493642f1SJames Wright v[0][i] -= wdetJb * rho * u_normal; 428493642f1SJames Wright 429493642f1SJames Wright // -- Momentum 430*2b916ea7SJeremy L Thompson for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho * u_normal * u[j] + norm[j] * P); 431493642f1SJames Wright 432493642f1SJames Wright // -- Total Energy Density 433493642f1SJames Wright v[4][i] -= wdetJb * u_normal * (E + P); 434a6e8f989SJames Wright 435a6e8f989SJames Wright jac_data_sur[0][i] = rho; 436a6e8f989SJames Wright jac_data_sur[1][i] = u[0]; 437a6e8f989SJames Wright jac_data_sur[2][i] = u[1]; 438a6e8f989SJames Wright jac_data_sur[3][i] = u[2]; 439a6e8f989SJames Wright jac_data_sur[4][i] = E; 440a6e8f989SJames Wright for (int j = 0; j < 6; j++) jac_data_sur[5 + j][i] = 0.; 441493642f1SJames Wright } 442493642f1SJames Wright return 0; 443493642f1SJames Wright } 444493642f1SJames Wright 445*2b916ea7SJeremy L Thompson CEED_QFUNCTION(STGShur14_Inflow_Jacobian)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 446a6e8f989SJames Wright // *INDENT-OFF* 447a6e8f989SJames Wright // Inputs 448*2b916ea7SJeremy L Thompson 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], 449a6e8f989SJames Wright (*jac_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4]; 450a6e8f989SJames Wright // Outputs 451a6e8f989SJames Wright CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 452a6e8f989SJames Wright // *INDENT-ON* 453a6e8f989SJames Wright const STGShur14Context stg_ctx = (STGShur14Context)ctx; 454a6e8f989SJames Wright const bool implicit = stg_ctx->is_implicit; 455a6e8f989SJames Wright const CeedScalar cv = stg_ctx->newtonian_ctx.cv; 456a6e8f989SJames Wright const CeedScalar cp = stg_ctx->newtonian_ctx.cp; 457a6e8f989SJames Wright const CeedScalar Rd = cp - cv; 458a6e8f989SJames Wright const CeedScalar gamma = cp / cv; 459a6e8f989SJames Wright 460a6e8f989SJames Wright const CeedScalar theta0 = stg_ctx->theta0; 461a6e8f989SJames Wright const bool prescribe_T = stg_ctx->prescribe_T; 462a6e8f989SJames Wright 463a6e8f989SJames Wright CeedPragmaSIMD 464a6e8f989SJames Wright // Quadrature Point Loop 465a6e8f989SJames Wright for (CeedInt i = 0; i < Q; i++) { 466a6e8f989SJames Wright // Setup 467a6e8f989SJames Wright // -- Interp-to-Interp q_data 468a6e8f989SJames Wright // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q). 469a6e8f989SJames Wright // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q). 470a6e8f989SJames Wright // We can effect this by swapping the sign on this weight 471a6e8f989SJames Wright const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i]; 472a6e8f989SJames Wright 473a6e8f989SJames Wright // Calculate inflow values 474a6e8f989SJames Wright CeedScalar velocity[3]; 475a6e8f989SJames Wright for (CeedInt j = 0; j < 3; j++) velocity[j] = jac_data_sur[5 + j][i]; 476a6e8f989SJames Wright 477a6e8f989SJames Wright // enabling user to choose between weak T and weak rho inflow 478a6e8f989SJames Wright CeedScalar drho, dE, dP; 479a6e8f989SJames Wright if (prescribe_T) { 480a6e8f989SJames Wright // rho should be from the current solution 481a6e8f989SJames Wright drho = dq[0][i]; 482a6e8f989SJames Wright CeedScalar dE_internal = drho * cv * theta0; 483a6e8f989SJames Wright CeedScalar dE_kinetic = .5 * drho * Dot3(velocity, velocity); 484a6e8f989SJames Wright dE = dE_internal + dE_kinetic; 485a6e8f989SJames Wright dP = drho * Rd * theta0; // interior rho with exterior T 486a6e8f989SJames Wright } else { // rho specified, E_internal from solution 487a6e8f989SJames Wright drho = 0; 488a6e8f989SJames Wright dE = dq[4][i]; 489a6e8f989SJames Wright dP = dE * (gamma - 1.); 490a6e8f989SJames Wright } 491*2b916ea7SJeremy L Thompson const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]}; 492a6e8f989SJames Wright 493a6e8f989SJames Wright const CeedScalar u_normal = Dot3(norm, velocity); 494a6e8f989SJames Wright 495a6e8f989SJames Wright v[0][i] = -wdetJb * drho * u_normal; 496*2b916ea7SJeremy L Thompson for (int j = 0; j < 3; j++) v[j + 1][i] = -wdetJb * (drho * u_normal * velocity[j] + norm[j] * dP); 497a6e8f989SJames Wright v[4][i] = -wdetJb * u_normal * (dE + dP); 498a6e8f989SJames Wright } // End Quadrature Point Loop 499a6e8f989SJames Wright return 0; 500a6e8f989SJames Wright } 501a6e8f989SJames Wright 502b7190ff7SJames Wright /******************************************************************** 503b7190ff7SJames Wright * @brief QFunction to calculate the strongly enforce inflow BC 504b7190ff7SJames Wright * 505b7190ff7SJames Wright * This QF is for the strong application of STG via libCEED (rather than 506b7190ff7SJames Wright * through the native PETSc `DMAddBoundary` -> `bcFunc` method. 507b7190ff7SJames Wright */ 508*2b916ea7SJeremy L Thompson CEED_QFUNCTION(STGShur14_Inflow_StrongQF)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 509b7190ff7SJames Wright //*INDENT-OFF* 510*2b916ea7SJeremy L Thompson const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0], (*coords)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1], 511*2b916ea7SJeremy L Thompson (*scale) = (const CeedScalar(*))in[2], (*stg_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3]; 512b7190ff7SJames Wright 513b7190ff7SJames Wright CeedScalar(*bcval)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 514b7190ff7SJames Wright //*INDENT-ON* 515b7190ff7SJames Wright 516b7190ff7SJames Wright const STGShur14Context stg_ctx = (STGShur14Context)ctx; 51770b0cb14SJames Wright CeedScalar u[3], ubar[3], cij[6], eps, lt; 518b7190ff7SJames Wright const bool mean_only = stg_ctx->mean_only; 519b7190ff7SJames Wright const CeedScalar dx = stg_ctx->dx; 520b7190ff7SJames Wright const CeedScalar mu = stg_ctx->newtonian_ctx.mu; 521b7190ff7SJames Wright const CeedScalar time = stg_ctx->time; 522b7190ff7SJames Wright const CeedScalar theta0 = stg_ctx->theta0; 523b7190ff7SJames Wright const CeedScalar P0 = stg_ctx->P0; 524b7190ff7SJames Wright const CeedScalar cv = stg_ctx->newtonian_ctx.cv; 525b7190ff7SJames Wright const CeedScalar cp = stg_ctx->newtonian_ctx.cp; 526b7190ff7SJames Wright const CeedScalar Rd = cp - cv; 527b7190ff7SJames Wright const CeedScalar rho = P0 / (Rd * theta0); 528b7190ff7SJames Wright 529*2b916ea7SJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 530b7190ff7SJames Wright const CeedScalar x[] = {coords[0][i], coords[1][i], coords[2][i]}; 531b7190ff7SJames Wright const CeedScalar dXdx[2][3] = { 532b7190ff7SJames Wright {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]}, 533b7190ff7SJames Wright {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]} 534b7190ff7SJames Wright }; 535b7190ff7SJames Wright 536b7190ff7SJames Wright CeedScalar h[3]; 537b7190ff7SJames Wright h[0] = dx; 538*2b916ea7SJeremy L Thompson for (CeedInt j = 1; j < 3; j++) h[j] = 2 / sqrt(Square(dXdx[0][j]) + Square(dXdx[1][j])); 539b7190ff7SJames Wright 540b7190ff7SJames Wright InterpolateProfile(coords[1][i], ubar, cij, &eps, <, stg_ctx); 541b7190ff7SJames Wright if (!mean_only) { 54270b0cb14SJames Wright if (1) { 543*2b916ea7SJeremy L Thompson STGShur14_Calc_PrecompEktot(x, time, ubar, cij, stg_data[0][i], h, x[1], eps, lt, mu / rho, u, stg_ctx); 54470b0cb14SJames Wright } else { // Original way 54570b0cb14SJames Wright CeedScalar qn[STG_NMODES_MAX]; 54670b0cb14SJames Wright CalcSpectrum(coords[1][i], eps, lt, h, mu / rho, qn, stg_ctx); 54770b0cb14SJames Wright STGShur14_Calc(x, time, ubar, cij, qn, u, stg_ctx); 54870b0cb14SJames Wright } 549b7190ff7SJames Wright } else { 550b7190ff7SJames Wright for (CeedInt j = 0; j < 3; j++) u[j] = ubar[j]; 551b7190ff7SJames Wright } 552b7190ff7SJames Wright 5533636f6a4SJames Wright switch (stg_ctx->newtonian_ctx.state_var) { 5543636f6a4SJames Wright case STATEVAR_CONSERVATIVE: 555b7190ff7SJames Wright bcval[0][i] = scale[i] * rho; 556b7190ff7SJames Wright bcval[1][i] = scale[i] * rho * u[0]; 557b7190ff7SJames Wright bcval[2][i] = scale[i] * rho * u[1]; 558b7190ff7SJames Wright bcval[3][i] = scale[i] * rho * u[2]; 55966531c8bSJames Wright bcval[4][i] = 0.; 5603636f6a4SJames Wright break; 5613636f6a4SJames Wright 5623636f6a4SJames Wright case STATEVAR_PRIMITIVE: 5633636f6a4SJames Wright bcval[0][i] = 0; 5643636f6a4SJames Wright bcval[1][i] = scale[i] * u[0]; 5653636f6a4SJames Wright bcval[2][i] = scale[i] * u[1]; 5663636f6a4SJames Wright bcval[3][i] = scale[i] * u[2]; 5673636f6a4SJames Wright bcval[4][i] = scale[i] * theta0; 5683636f6a4SJames Wright break; 569b7190ff7SJames Wright } 57088243482SJames Wright } 571b7190ff7SJames Wright return 0; 572b7190ff7SJames Wright } 573b7190ff7SJames Wright 574493642f1SJames Wright #endif // stg_shur14_h 575