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