1*493642f1SJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors. 2*493642f1SJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details. 3*493642f1SJames Wright // 4*493642f1SJames Wright // SPDX-License-Identifier: BSD-2-Clause 5*493642f1SJames Wright // 6*493642f1SJames Wright // This file is part of CEED: http://github.com/ceed 7*493642f1SJames Wright 8*493642f1SJames Wright /// @file 9*493642f1SJames Wright /// Implementation of the Synthetic Turbulence Generation (STG) algorithm 10*493642f1SJames Wright /// presented in Shur et al. 2014 11*493642f1SJames Wright // 12*493642f1SJames Wright /// SetupSTG_Rand reads in the input files and fills in STGShur14Context. Then 13*493642f1SJames Wright /// STGShur14_CalcQF is run over quadrature points. Before the program exits, 14*493642f1SJames Wright /// TearDownSTG is run to free the memory of the allocated arrays. 15*493642f1SJames Wright 16*493642f1SJames Wright #ifndef stg_shur14_h 17*493642f1SJames Wright #define stg_shur14_h 18*493642f1SJames Wright 19*493642f1SJames Wright #include <math.h> 20*493642f1SJames Wright #include <ceed.h> 21*493642f1SJames Wright #include <stdlib.h> 22*493642f1SJames Wright #include "stg_shur14_type.h" 23*493642f1SJames Wright 24*493642f1SJames Wright #ifndef M_PI 25*493642f1SJames Wright #define M_PI 3.14159265358979323846 26*493642f1SJames Wright #endif 27*493642f1SJames Wright 28*493642f1SJames Wright #define STG_NMODES_MAX 1024 29*493642f1SJames Wright 30*493642f1SJames Wright CEED_QFUNCTION_HELPER CeedScalar Max(CeedScalar a, CeedScalar b) { return a < b ? b : a; } 31*493642f1SJames Wright CEED_QFUNCTION_HELPER CeedScalar Min(CeedScalar a, CeedScalar b) { return a < b ? a : b; } 32*493642f1SJames Wright 33*493642f1SJames Wright /* 34*493642f1SJames Wright * @brief Interpolate quantities from input profile to given location 35*493642f1SJames Wright * 36*493642f1SJames Wright * Assumed that prof_dw[i+1] > prof_dw[i] and prof_dw[0] = 0 37*493642f1SJames Wright * If dw > prof_dw[-1], then the interpolation takes the values at prof_dw[-1] 38*493642f1SJames Wright * 39*493642f1SJames Wright * @param[in] dw Distance to the nearest wall 40*493642f1SJames Wright * @param[out] ubar Mean velocity at dw 41*493642f1SJames Wright * @param[out] cij Cholesky decomposition at dw 42*493642f1SJames Wright * @param[out] eps Turbulent dissipation at dw 43*493642f1SJames Wright * @param[out] lt Turbulent length scale at dw 44*493642f1SJames Wright * @param[in] stg_ctx STGShur14Context for the problem 45*493642f1SJames Wright */ 46*493642f1SJames Wright CEED_QFUNCTION_HELPER void InterpolateProfile(const CeedScalar dw, 47*493642f1SJames Wright CeedScalar ubar[3], CeedScalar cij[6], CeedScalar *eps, CeedScalar *lt, 48*493642f1SJames Wright const STGShur14Context stg_ctx) { 49*493642f1SJames Wright 50*493642f1SJames Wright const CeedInt nprofs = stg_ctx->nprofs; 51*493642f1SJames Wright const CeedScalar *prof_dw = &stg_ctx->data[stg_ctx->offsets.prof_dw]; 52*493642f1SJames Wright const CeedScalar *prof_eps = &stg_ctx->data[stg_ctx->offsets.eps]; 53*493642f1SJames Wright const CeedScalar *prof_lt = &stg_ctx->data[stg_ctx->offsets.lt]; 54*493642f1SJames Wright const CeedScalar *prof_ubar = &stg_ctx->data[stg_ctx->offsets.ubar]; 55*493642f1SJames Wright const CeedScalar *prof_cij = &stg_ctx->data[stg_ctx->offsets.cij]; 56*493642f1SJames Wright CeedInt idx=-1; 57*493642f1SJames Wright 58*493642f1SJames Wright for(CeedInt i=0; i<nprofs; i++) { 59*493642f1SJames Wright if (dw < prof_dw[i]) { 60*493642f1SJames Wright idx = i; 61*493642f1SJames Wright break; 62*493642f1SJames Wright } 63*493642f1SJames Wright } 64*493642f1SJames Wright 65*493642f1SJames Wright if (idx > 0) { // y within the bounds of prof_dw 66*493642f1SJames Wright CeedScalar coeff = (dw - prof_dw[idx-1]) / (prof_dw[idx] - prof_dw[idx-1]); 67*493642f1SJames Wright 68*493642f1SJames Wright //*INDENT-OFF* 69*493642f1SJames Wright ubar[0] = prof_ubar[0*nprofs+idx-1] + coeff*( prof_ubar[0*nprofs+idx] - prof_ubar[0*nprofs+idx-1] ); 70*493642f1SJames Wright ubar[1] = prof_ubar[1*nprofs+idx-1] + coeff*( prof_ubar[1*nprofs+idx] - prof_ubar[1*nprofs+idx-1] ); 71*493642f1SJames Wright ubar[2] = prof_ubar[2*nprofs+idx-1] + coeff*( prof_ubar[2*nprofs+idx] - prof_ubar[2*nprofs+idx-1] ); 72*493642f1SJames Wright cij[0] = prof_cij[0*nprofs+idx-1] + coeff*( prof_cij[0*nprofs+idx] - prof_cij[0*nprofs+idx-1] ); 73*493642f1SJames Wright cij[1] = prof_cij[1*nprofs+idx-1] + coeff*( prof_cij[1*nprofs+idx] - prof_cij[1*nprofs+idx-1] ); 74*493642f1SJames Wright cij[2] = prof_cij[2*nprofs+idx-1] + coeff*( prof_cij[2*nprofs+idx] - prof_cij[2*nprofs+idx-1] ); 75*493642f1SJames Wright cij[3] = prof_cij[3*nprofs+idx-1] + coeff*( prof_cij[3*nprofs+idx] - prof_cij[3*nprofs+idx-1] ); 76*493642f1SJames Wright cij[4] = prof_cij[4*nprofs+idx-1] + coeff*( prof_cij[4*nprofs+idx] - prof_cij[4*nprofs+idx-1] ); 77*493642f1SJames Wright cij[5] = prof_cij[5*nprofs+idx-1] + coeff*( prof_cij[5*nprofs+idx] - prof_cij[5*nprofs+idx-1] ); 78*493642f1SJames Wright *eps = prof_eps[idx-1] + coeff*( prof_eps[idx] - prof_eps[idx-1] ); 79*493642f1SJames Wright *lt = prof_lt[idx-1] + coeff*( prof_lt[idx] - prof_lt[idx-1] ); 80*493642f1SJames Wright //*INDENT-ON* 81*493642f1SJames Wright } else { // y outside bounds of prof_dw 82*493642f1SJames Wright ubar[0] = prof_ubar[1*nprofs-1]; 83*493642f1SJames Wright ubar[1] = prof_ubar[2*nprofs-1]; 84*493642f1SJames Wright ubar[2] = prof_ubar[3*nprofs-1]; 85*493642f1SJames Wright cij[0] = prof_cij[1*nprofs-1]; 86*493642f1SJames Wright cij[1] = prof_cij[2*nprofs-1]; 87*493642f1SJames Wright cij[2] = prof_cij[3*nprofs-1]; 88*493642f1SJames Wright cij[3] = prof_cij[4*nprofs-1]; 89*493642f1SJames Wright cij[4] = prof_cij[5*nprofs-1]; 90*493642f1SJames Wright cij[5] = prof_cij[6*nprofs-1]; 91*493642f1SJames Wright *eps = prof_eps[nprofs-1]; 92*493642f1SJames Wright *lt = prof_lt[nprofs-1]; 93*493642f1SJames Wright } 94*493642f1SJames Wright } 95*493642f1SJames Wright 96*493642f1SJames Wright /* 97*493642f1SJames Wright * @brief Calculate spectrum coefficients for STG 98*493642f1SJames Wright * 99*493642f1SJames Wright * Calculates q_n at a given distance to the wall 100*493642f1SJames Wright * 101*493642f1SJames Wright * @param[in] dw Distance to the nearest wall 102*493642f1SJames Wright * @param[in] eps Turbulent dissipation w/rt dw 103*493642f1SJames Wright * @param[in] lt Turbulent length scale w/rt dw 104*493642f1SJames Wright * @param[in] h Element lengths in coordinate directions 105*493642f1SJames Wright * @param[in] nu Dynamic Viscosity; 106*493642f1SJames Wright * @param[in] stg_ctx STGShur14Context for the problem 107*493642f1SJames Wright * @param[out] qn Spectrum coefficients, [nmodes] 108*493642f1SJames Wright */ 109*493642f1SJames Wright void CEED_QFUNCTION_HELPER(CalcSpectrum)(const CeedScalar dw, 110*493642f1SJames Wright const CeedScalar eps, const CeedScalar lt, const CeedScalar h[3], 111*493642f1SJames Wright const CeedScalar nu, CeedScalar qn[], const STGShur14Context stg_ctx) { 112*493642f1SJames Wright 113*493642f1SJames Wright const CeedInt nmodes = stg_ctx->nmodes; 114*493642f1SJames Wright const CeedScalar *kappa = &stg_ctx->data[stg_ctx->offsets.kappa]; 115*493642f1SJames Wright 116*493642f1SJames Wright const CeedScalar hmax = Max( Max(h[0], h[1]), h[2]); 117*493642f1SJames Wright const CeedScalar ke = 2*M_PI/Min(2*dw, 3*lt); 118*493642f1SJames Wright const CeedScalar keta = 2*M_PI*pow(pow(nu,3.0)/eps, -0.25); 119*493642f1SJames Wright const CeedScalar kcut = 120*493642f1SJames Wright M_PI/ Min( Max(Max(h[1], h[2]), 0.3*hmax) + 0.1*dw, hmax ); 121*493642f1SJames Wright CeedScalar fcut, feta, Ektot=0.0; 122*493642f1SJames Wright 123*493642f1SJames Wright for(CeedInt n=0; n<nmodes; n++) { 124*493642f1SJames Wright feta = exp(-Square(12*kappa[n]/keta)); 125*493642f1SJames Wright fcut = exp( -pow(4*Max(kappa[n] - 0.9*kcut, 0)/kcut, 3.) ); 126*493642f1SJames Wright qn[n] = pow(kappa[n]/ke, 4.) 127*493642f1SJames Wright * pow(1 + 2.4*Square(kappa[n]/ke),-17./6)*feta*fcut; 128*493642f1SJames Wright qn[n] *= n==0 ? kappa[0] : kappa[n] - kappa[n-1]; 129*493642f1SJames Wright Ektot += qn[n]; 130*493642f1SJames Wright } 131*493642f1SJames Wright 132*493642f1SJames Wright for(CeedInt n=0; n<nmodes; n++) qn[n] /= Ektot; 133*493642f1SJames Wright } 134*493642f1SJames Wright 135*493642f1SJames Wright /****************************************************** 136*493642f1SJames Wright * @brief Calculate u(x,t) for STG inflow condition 137*493642f1SJames Wright * 138*493642f1SJames Wright * @param[in] X Location to evaluate u(X,t) 139*493642f1SJames Wright * @param[in] t Time to evaluate u(X,t) 140*493642f1SJames Wright * @param[in] ubar Mean velocity at X 141*493642f1SJames Wright * @param[in] cij Cholesky decomposition at X 142*493642f1SJames Wright * @param[in] qn Wavemode amplitudes at X, [nmodes] 143*493642f1SJames Wright * @param[out] u Velocity at X and t 144*493642f1SJames Wright * @param[in] stg_ctx STGShur14Context for the problem 145*493642f1SJames Wright */ 146*493642f1SJames Wright void CEED_QFUNCTION_HELPER(STGShur14_Calc)(const CeedScalar X[3], 147*493642f1SJames Wright const CeedScalar t, const CeedScalar ubar[3], const CeedScalar cij[6], 148*493642f1SJames Wright const CeedScalar qn[], CeedScalar u[3], 149*493642f1SJames Wright const STGShur14Context stg_ctx) { 150*493642f1SJames Wright 151*493642f1SJames Wright //*INDENT-OFF* 152*493642f1SJames Wright const CeedInt nmodes = stg_ctx->nmodes; 153*493642f1SJames Wright const CeedScalar *kappa = &stg_ctx->data[stg_ctx->offsets.kappa]; 154*493642f1SJames Wright const CeedScalar *phi = &stg_ctx->data[stg_ctx->offsets.phi]; 155*493642f1SJames Wright const CeedScalar *sigma = &stg_ctx->data[stg_ctx->offsets.sigma]; 156*493642f1SJames Wright const CeedScalar *d = &stg_ctx->data[stg_ctx->offsets.d]; 157*493642f1SJames Wright //*INDENT-ON* 158*493642f1SJames Wright const CeedScalar tworoot1p5 = 2*sqrt(1.5); 159*493642f1SJames Wright CeedScalar xdotd, vp[3] = {0.}; 160*493642f1SJames Wright CeedScalar xhat[] = {0., X[1], X[2]}; 161*493642f1SJames Wright 162*493642f1SJames Wright CeedPragmaSIMD 163*493642f1SJames Wright for(CeedInt n=0; n<nmodes; n++) { 164*493642f1SJames Wright xhat[0] = (X[0] - stg_ctx->u0*t)*Max(2*kappa[0]/kappa[n], 0.1); 165*493642f1SJames Wright xdotd = 0.; 166*493642f1SJames Wright for(CeedInt i=0; i<3; i++) xdotd += d[i*nmodes+n]*xhat[i]; 167*493642f1SJames Wright const CeedScalar cos_kxdp = cos(kappa[n]*xdotd + phi[n]); 168*493642f1SJames Wright vp[0] += tworoot1p5*sqrt(qn[n])*sigma[0*nmodes+n] * cos_kxdp; 169*493642f1SJames Wright vp[1] += tworoot1p5*sqrt(qn[n])*sigma[1*nmodes+n] * cos_kxdp; 170*493642f1SJames Wright vp[2] += tworoot1p5*sqrt(qn[n])*sigma[2*nmodes+n] * cos_kxdp; 171*493642f1SJames Wright } 172*493642f1SJames Wright 173*493642f1SJames Wright u[0] = ubar[0] + cij[0]*vp[0]; 174*493642f1SJames Wright u[1] = ubar[1] + cij[3]*vp[0] + cij[1]*vp[1]; 175*493642f1SJames Wright u[2] = ubar[2] + cij[4]*vp[0] + cij[5]*vp[1] + cij[2]*vp[2]; 176*493642f1SJames Wright } 177*493642f1SJames Wright 178*493642f1SJames Wright /******************************************************************** 179*493642f1SJames Wright * @brief QFunction to calculate the inflow boundary condition 180*493642f1SJames Wright * 181*493642f1SJames Wright * This will loop through quadrature points, calculate the wavemode amplitudes 182*493642f1SJames Wright * at each location, then calculate the actual velocity. 183*493642f1SJames Wright */ 184*493642f1SJames Wright CEED_QFUNCTION(STGShur14_Inflow)(void *ctx, CeedInt Q, 185*493642f1SJames Wright const CeedScalar *const *in, 186*493642f1SJames Wright CeedScalar *const *out) { 187*493642f1SJames Wright 188*493642f1SJames Wright //*INDENT-OFF* 189*493642f1SJames Wright const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA]) in[0], 190*493642f1SJames Wright (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA]) in[1], 191*493642f1SJames Wright (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA]) in[2]; 192*493642f1SJames Wright 193*493642f1SJames Wright CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA]) out[0]; 194*493642f1SJames Wright 195*493642f1SJames Wright //*INDENT-ON* 196*493642f1SJames Wright 197*493642f1SJames Wright const STGShur14Context stg_ctx = (STGShur14Context) ctx; 198*493642f1SJames Wright CeedScalar qn[STG_NMODES_MAX], u[3], ubar[3], cij[6], eps, lt; 199*493642f1SJames Wright const bool is_implicit = stg_ctx->is_implicit; 200*493642f1SJames Wright const bool mean_only = stg_ctx->mean_only; 201*493642f1SJames Wright const bool prescribe_T = stg_ctx->prescribe_T; 202*493642f1SJames Wright const CeedScalar dx = stg_ctx->dx; 203*493642f1SJames Wright const CeedScalar mu = stg_ctx->newtonian_ctx.mu; 204*493642f1SJames Wright const CeedScalar time = stg_ctx->time; 205*493642f1SJames Wright const CeedScalar theta0 = stg_ctx->theta0; 206*493642f1SJames Wright const CeedScalar P0 = stg_ctx->P0; 207*493642f1SJames Wright const CeedScalar cv = stg_ctx->newtonian_ctx.cv; 208*493642f1SJames Wright const CeedScalar cp = stg_ctx->newtonian_ctx.cp; 209*493642f1SJames Wright const CeedScalar Rd = cp - cv; 210*493642f1SJames Wright const CeedScalar gamma = cp/cv; 211*493642f1SJames Wright 212*493642f1SJames Wright CeedPragmaSIMD 213*493642f1SJames Wright for(CeedInt i=0; i<Q; i++) { 214*493642f1SJames Wright const CeedScalar rho = prescribe_T ? q[0][i] : P0 / (Rd * theta0); 215*493642f1SJames Wright const CeedScalar x[] = { X[0][i], X[1][i], X[2][i] }; 216*493642f1SJames Wright const CeedScalar dXdx[2][3] = { 217*493642f1SJames Wright {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]}, 218*493642f1SJames Wright {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]} 219*493642f1SJames Wright }; 220*493642f1SJames Wright 221*493642f1SJames Wright CeedScalar h[3]; 222*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 223*493642f1SJames Wright h[j] = 2/sqrt(dXdx[0][j]*dXdx[0][j] + dXdx[1][j]*dXdx[1][j]); 224*493642f1SJames Wright h[0] = dx; 225*493642f1SJames Wright 226*493642f1SJames Wright InterpolateProfile(X[1][i], ubar, cij, &eps, <, stg_ctx); 227*493642f1SJames Wright if (!mean_only) { 228*493642f1SJames Wright CalcSpectrum(X[1][i], eps, lt, h, mu/rho, qn, stg_ctx); 229*493642f1SJames Wright STGShur14_Calc(x, time, ubar, cij, qn, u, stg_ctx); 230*493642f1SJames Wright } else { 231*493642f1SJames Wright for (CeedInt j=0; j<3; j++) u[j] = ubar[j]; 232*493642f1SJames Wright } 233*493642f1SJames Wright 234*493642f1SJames Wright const CeedScalar E_kinetic = .5 * rho * (u[0]*u[0] + 235*493642f1SJames Wright u[1]*u[1] + 236*493642f1SJames Wright u[2]*u[2]); 237*493642f1SJames Wright CeedScalar E_internal, P; 238*493642f1SJames Wright if (prescribe_T) { 239*493642f1SJames Wright // Temperature is being set weakly (theta0) and for constant cv this sets E_internal 240*493642f1SJames Wright E_internal = rho * cv * theta0; 241*493642f1SJames Wright // Find pressure using 242*493642f1SJames Wright P = rho * Rd * theta0; // interior rho with exterior T 243*493642f1SJames Wright } else { 244*493642f1SJames Wright E_internal = q[4][i] - E_kinetic; // uses prescribed rho and u, E from solution 245*493642f1SJames Wright P = E_internal * (gamma - 1.); 246*493642f1SJames Wright } 247*493642f1SJames Wright 248*493642f1SJames Wright const CeedScalar wdetJb = (is_implicit ? -1. : 1.) * q_data_sur[0][i]; 249*493642f1SJames Wright // ---- Normal vect 250*493642f1SJames Wright const CeedScalar norm[3] = {q_data_sur[1][i], 251*493642f1SJames Wright q_data_sur[2][i], 252*493642f1SJames Wright q_data_sur[3][i] 253*493642f1SJames Wright }; 254*493642f1SJames Wright 255*493642f1SJames Wright const CeedScalar E = E_internal + E_kinetic; 256*493642f1SJames Wright 257*493642f1SJames Wright // Velocity normal to the boundary 258*493642f1SJames Wright const CeedScalar u_normal = norm[0]*u[0] + 259*493642f1SJames Wright norm[1]*u[1] + 260*493642f1SJames Wright norm[2]*u[2]; 261*493642f1SJames Wright // The Physics 262*493642f1SJames Wright // Zero v so all future terms can safely sum into it 263*493642f1SJames Wright for (CeedInt j=0; j<5; j++) v[j][i] = 0.; 264*493642f1SJames Wright 265*493642f1SJames Wright // The Physics 266*493642f1SJames Wright // -- Density 267*493642f1SJames Wright v[0][i] -= wdetJb * rho * u_normal; 268*493642f1SJames Wright 269*493642f1SJames Wright // -- Momentum 270*493642f1SJames Wright for (CeedInt j=0; j<3; j++) 271*493642f1SJames Wright v[j+1][i] -= wdetJb *(rho * u_normal * u[j] + 272*493642f1SJames Wright norm[j] * P); 273*493642f1SJames Wright 274*493642f1SJames Wright // -- Total Energy Density 275*493642f1SJames Wright v[4][i] -= wdetJb * u_normal * (E + P); 276*493642f1SJames Wright } 277*493642f1SJames Wright return 0; 278*493642f1SJames Wright } 279*493642f1SJames Wright 280*493642f1SJames Wright 281*493642f1SJames Wright #endif // stg_shur14_h 282