xref: /honee/qfunctions/stg_shur14.h (revision 71cd6200d2f83eb410583bfa41caae9118054d98)
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 <math.h>
20493642f1SJames Wright #include <ceed.h>
21493642f1SJames Wright #include <stdlib.h>
22493642f1SJames Wright #include "stg_shur14_type.h"
23704b8bbeSJames Wright #include "utils.h"
24493642f1SJames Wright 
25493642f1SJames Wright #define STG_NMODES_MAX 1024
26493642f1SJames Wright 
27493642f1SJames Wright /*
28493642f1SJames Wright  * @brief Interpolate quantities from input profile to given location
29493642f1SJames Wright  *
30493642f1SJames Wright  * Assumed that prof_dw[i+1] > prof_dw[i] and prof_dw[0] = 0
31493642f1SJames Wright  * If dw > prof_dw[-1], then the interpolation takes the values at prof_dw[-1]
32493642f1SJames Wright  *
33493642f1SJames Wright  * @param[in]  dw      Distance to the nearest wall
34493642f1SJames Wright  * @param[out] ubar    Mean velocity at dw
35493642f1SJames Wright  * @param[out] cij     Cholesky decomposition at dw
36493642f1SJames Wright  * @param[out] eps     Turbulent dissipation at dw
37493642f1SJames Wright  * @param[out] lt      Turbulent length scale at dw
38493642f1SJames Wright  * @param[in]  stg_ctx STGShur14Context for the problem
39493642f1SJames Wright  */
40493642f1SJames Wright CEED_QFUNCTION_HELPER void InterpolateProfile(const CeedScalar dw,
41493642f1SJames Wright     CeedScalar ubar[3], CeedScalar cij[6], CeedScalar *eps, CeedScalar *lt,
42493642f1SJames Wright     const STGShur14Context stg_ctx) {
43493642f1SJames Wright 
44493642f1SJames Wright   const CeedInt    nprofs    = stg_ctx->nprofs;
45493642f1SJames Wright   const CeedScalar *prof_dw  = &stg_ctx->data[stg_ctx->offsets.prof_dw];
46493642f1SJames Wright   const CeedScalar *prof_eps = &stg_ctx->data[stg_ctx->offsets.eps];
47493642f1SJames Wright   const CeedScalar *prof_lt  = &stg_ctx->data[stg_ctx->offsets.lt];
48493642f1SJames Wright   const CeedScalar *prof_ubar = &stg_ctx->data[stg_ctx->offsets.ubar];
49493642f1SJames Wright   const CeedScalar *prof_cij  = &stg_ctx->data[stg_ctx->offsets.cij];
50493642f1SJames Wright   CeedInt idx=-1;
51493642f1SJames Wright 
52493642f1SJames Wright   for(CeedInt i=0; i<nprofs; i++) {
53493642f1SJames Wright     if (dw < prof_dw[i]) {
54493642f1SJames Wright       idx = i;
55493642f1SJames Wright       break;
56493642f1SJames Wright     }
57493642f1SJames Wright   }
58493642f1SJames Wright 
59493642f1SJames Wright   if (idx > 0) { // y within the bounds of prof_dw
60493642f1SJames Wright     CeedScalar coeff = (dw - prof_dw[idx-1]) / (prof_dw[idx] - prof_dw[idx-1]);
61493642f1SJames Wright 
62493642f1SJames Wright     //*INDENT-OFF*
63493642f1SJames Wright     ubar[0] = prof_ubar[0*nprofs+idx-1] + coeff*( prof_ubar[0*nprofs+idx] - prof_ubar[0*nprofs+idx-1] );
64493642f1SJames Wright     ubar[1] = prof_ubar[1*nprofs+idx-1] + coeff*( prof_ubar[1*nprofs+idx] - prof_ubar[1*nprofs+idx-1] );
65493642f1SJames Wright     ubar[2] = prof_ubar[2*nprofs+idx-1] + coeff*( prof_ubar[2*nprofs+idx] - prof_ubar[2*nprofs+idx-1] );
66493642f1SJames Wright     cij[0]  = prof_cij[0*nprofs+idx-1]  + coeff*( prof_cij[0*nprofs+idx]  - prof_cij[0*nprofs+idx-1] );
67493642f1SJames Wright     cij[1]  = prof_cij[1*nprofs+idx-1]  + coeff*( prof_cij[1*nprofs+idx]  - prof_cij[1*nprofs+idx-1] );
68493642f1SJames Wright     cij[2]  = prof_cij[2*nprofs+idx-1]  + coeff*( prof_cij[2*nprofs+idx]  - prof_cij[2*nprofs+idx-1] );
69493642f1SJames Wright     cij[3]  = prof_cij[3*nprofs+idx-1]  + coeff*( prof_cij[3*nprofs+idx]  - prof_cij[3*nprofs+idx-1] );
70493642f1SJames Wright     cij[4]  = prof_cij[4*nprofs+idx-1]  + coeff*( prof_cij[4*nprofs+idx]  - prof_cij[4*nprofs+idx-1] );
71493642f1SJames Wright     cij[5]  = prof_cij[5*nprofs+idx-1]  + coeff*( prof_cij[5*nprofs+idx]  - prof_cij[5*nprofs+idx-1] );
72493642f1SJames Wright     *eps    = prof_eps[idx-1]           + coeff*( prof_eps[idx]           - prof_eps[idx-1] );
73493642f1SJames Wright     *lt     = prof_lt[idx-1]            + coeff*( prof_lt[idx]            - prof_lt[idx-1] );
74493642f1SJames Wright     //*INDENT-ON*
75493642f1SJames Wright   } else { // y outside bounds of prof_dw
76493642f1SJames Wright     ubar[0] = prof_ubar[1*nprofs-1];
77493642f1SJames Wright     ubar[1] = prof_ubar[2*nprofs-1];
78493642f1SJames Wright     ubar[2] = prof_ubar[3*nprofs-1];
79493642f1SJames Wright     cij[0]  = prof_cij[1*nprofs-1];
80493642f1SJames Wright     cij[1]  = prof_cij[2*nprofs-1];
81493642f1SJames Wright     cij[2]  = prof_cij[3*nprofs-1];
82493642f1SJames Wright     cij[3]  = prof_cij[4*nprofs-1];
83493642f1SJames Wright     cij[4]  = prof_cij[5*nprofs-1];
84493642f1SJames Wright     cij[5]  = prof_cij[6*nprofs-1];
85493642f1SJames Wright     *eps    = prof_eps[nprofs-1];
86493642f1SJames Wright     *lt     = prof_lt[nprofs-1];
87493642f1SJames Wright   }
88493642f1SJames Wright }
89493642f1SJames Wright 
90493642f1SJames Wright /*
91*71cd6200SJames Wright  * @brief Calculate spectrum coefficient, qn
92*71cd6200SJames Wright  *
93*71cd6200SJames Wright  * Calculates q_n at a given distance to the wall
94*71cd6200SJames Wright  *
95*71cd6200SJames Wright  * @param[in]  kappa  nth wavenumber
96*71cd6200SJames Wright  * @param[in]  dkappa Difference between wavenumbers
97*71cd6200SJames Wright  * @param[in]  keta   Dissipation wavenumber
98*71cd6200SJames Wright  * @param[in]  kcut   Mesh-induced cutoff wavenumber
99*71cd6200SJames Wright  * @param[in]  ke     Energy-containing wavenumber
100*71cd6200SJames Wright  * @param[in]  Ektot  Total turbulent kinetic energy of spectrum
101*71cd6200SJames Wright  * @returns    qn     Spectrum coefficient
102*71cd6200SJames Wright  */
103*71cd6200SJames Wright CeedScalar CEED_QFUNCTION_HELPER(Calc_qn)(const CeedScalar kappa,
104*71cd6200SJames Wright     const CeedScalar dkappa, const CeedScalar keta, const CeedScalar kcut,
105*71cd6200SJames Wright     const CeedScalar ke, const CeedScalar Ektot) {
106*71cd6200SJames Wright   const CeedScalar feta_x_fcut   = exp(-Square(12*kappa/keta)
107*71cd6200SJames Wright                                        -Cube(4*Max(kappa - 0.9*kcut, 0)/kcut) );
108*71cd6200SJames Wright   return pow(kappa/ke, 4.) * pow(1 + 2.4*Square(kappa/ke),-17./6)
109*71cd6200SJames Wright          *feta_x_fcut*dkappa/Ektot;
110*71cd6200SJames Wright }
111*71cd6200SJames Wright 
112*71cd6200SJames Wright // Calculate hmax, ke, keta, and kcut
113*71cd6200SJames Wright void CEED_QFUNCTION_HELPER(SpectrumConstants)(const CeedScalar dw,
114*71cd6200SJames Wright     const CeedScalar eps, const CeedScalar lt, const CeedScalar h[3],
115*71cd6200SJames Wright     const CeedScalar nu, CeedScalar *hmax, CeedScalar *ke,
116*71cd6200SJames Wright     CeedScalar *keta, CeedScalar *kcut) {
117*71cd6200SJames Wright   *hmax = Max( Max(h[0], h[1]), h[2]);
118*71cd6200SJames Wright   *ke   = dw==0 ? 1e16 : 2*M_PI/Min(2*dw, 3*lt);
119*71cd6200SJames Wright   *keta = 2*M_PI*pow(Cube(nu)/eps, -0.25);
120*71cd6200SJames Wright   *kcut = M_PI/ Min( Max(Max(h[1], h[2]), 0.3*(*hmax)) + 0.1*dw, *hmax );
121*71cd6200SJames Wright }
122*71cd6200SJames Wright 
123*71cd6200SJames Wright /*
124493642f1SJames Wright  * @brief Calculate spectrum coefficients for STG
125493642f1SJames Wright  *
126493642f1SJames Wright  * Calculates q_n at a given distance to the wall
127493642f1SJames Wright  *
128493642f1SJames Wright  * @param[in]  dw      Distance to the nearest wall
129493642f1SJames Wright  * @param[in]  eps     Turbulent dissipation w/rt dw
130493642f1SJames Wright  * @param[in]  lt      Turbulent length scale w/rt dw
131493642f1SJames Wright  * @param[in]  h       Element lengths in coordinate directions
132493642f1SJames Wright  * @param[in]  nu      Dynamic Viscosity;
133493642f1SJames Wright  * @param[in]  stg_ctx STGShur14Context for the problem
134493642f1SJames Wright  * @param[out] qn      Spectrum coefficients, [nmodes]
135493642f1SJames Wright  */
136493642f1SJames Wright void CEED_QFUNCTION_HELPER(CalcSpectrum)(const CeedScalar dw,
137493642f1SJames Wright     const CeedScalar eps, const CeedScalar lt, const CeedScalar h[3],
138493642f1SJames Wright     const CeedScalar nu, CeedScalar qn[], const STGShur14Context stg_ctx) {
139493642f1SJames Wright 
140493642f1SJames Wright   const CeedInt    nmodes = stg_ctx->nmodes;
141493642f1SJames Wright   const CeedScalar *kappa = &stg_ctx->data[stg_ctx->offsets.kappa];
142*71cd6200SJames Wright   CeedScalar hmax, ke, keta, kcut, Ektot=0.0;
143*71cd6200SJames Wright   SpectrumConstants(dw, eps, lt, h, nu, &hmax, &ke, &keta, &kcut);
144493642f1SJames Wright 
145493642f1SJames Wright   for(CeedInt n=0; n<nmodes; n++) {
146*71cd6200SJames Wright     const CeedScalar dkappa = n==0 ? kappa[0] : kappa[n] - kappa[n-1];
147*71cd6200SJames Wright     qn[n] = Calc_qn(kappa[n], dkappa, keta, kcut, ke, 1.0);
148493642f1SJames Wright     Ektot += qn[n];
149493642f1SJames Wright   }
150493642f1SJames Wright 
1510a8dc919SJames Wright   if (Ektot == 0) return;
152493642f1SJames Wright   for(CeedInt n=0; n<nmodes; n++) qn[n] /= Ektot;
153493642f1SJames Wright }
154493642f1SJames Wright 
155493642f1SJames Wright /******************************************************
156493642f1SJames Wright  * @brief Calculate u(x,t) for STG inflow condition
157493642f1SJames Wright  *
158493642f1SJames Wright  * @param[in]  X       Location to evaluate u(X,t)
159493642f1SJames Wright  * @param[in]  t       Time to evaluate u(X,t)
160493642f1SJames Wright  * @param[in]  ubar    Mean velocity at X
161493642f1SJames Wright  * @param[in]  cij     Cholesky decomposition at X
162493642f1SJames Wright  * @param[in]  qn      Wavemode amplitudes at X, [nmodes]
163493642f1SJames Wright  * @param[out] u       Velocity at X and t
164493642f1SJames Wright  * @param[in]  stg_ctx STGShur14Context for the problem
165493642f1SJames Wright  */
166493642f1SJames Wright void CEED_QFUNCTION_HELPER(STGShur14_Calc)(const CeedScalar X[3],
167493642f1SJames Wright     const CeedScalar t, const CeedScalar ubar[3], const CeedScalar cij[6],
168493642f1SJames Wright     const CeedScalar qn[], CeedScalar u[3],
169493642f1SJames Wright     const STGShur14Context stg_ctx) {
170493642f1SJames Wright 
171493642f1SJames Wright   //*INDENT-OFF*
172493642f1SJames Wright   const CeedInt    nmodes = stg_ctx->nmodes;
173493642f1SJames Wright   const CeedScalar *kappa = &stg_ctx->data[stg_ctx->offsets.kappa];
174493642f1SJames Wright   const CeedScalar *phi   = &stg_ctx->data[stg_ctx->offsets.phi];
175493642f1SJames Wright   const CeedScalar *sigma = &stg_ctx->data[stg_ctx->offsets.sigma];
176493642f1SJames Wright   const CeedScalar *d     = &stg_ctx->data[stg_ctx->offsets.d];
177493642f1SJames Wright   //*INDENT-ON*
178493642f1SJames Wright   CeedScalar xdotd, vp[3] = {0.};
179493642f1SJames Wright   CeedScalar xhat[] = {0., X[1], X[2]};
180493642f1SJames Wright 
181493642f1SJames Wright   CeedPragmaSIMD
182493642f1SJames Wright   for(CeedInt n=0; n<nmodes; n++) {
183493642f1SJames Wright     xhat[0] = (X[0] - stg_ctx->u0*t)*Max(2*kappa[0]/kappa[n], 0.1);
184493642f1SJames Wright     xdotd = 0.;
185493642f1SJames Wright     for(CeedInt i=0; i<3; i++) xdotd += d[i*nmodes+n]*xhat[i];
186493642f1SJames Wright     const CeedScalar cos_kxdp = cos(kappa[n]*xdotd + phi[n]);
1870a8dc919SJames Wright     vp[0] += sqrt(qn[n])*sigma[0*nmodes+n] * cos_kxdp;
1880a8dc919SJames Wright     vp[1] += sqrt(qn[n])*sigma[1*nmodes+n] * cos_kxdp;
1890a8dc919SJames Wright     vp[2] += sqrt(qn[n])*sigma[2*nmodes+n] * cos_kxdp;
190493642f1SJames Wright   }
1910a8dc919SJames Wright   for(CeedInt i=0; i<3; i++) vp[i] *= 2*sqrt(1.5);
192493642f1SJames Wright 
193493642f1SJames Wright   u[0] = ubar[0] + cij[0]*vp[0];
194493642f1SJames Wright   u[1] = ubar[1] + cij[3]*vp[0] + cij[1]*vp[1];
195493642f1SJames Wright   u[2] = ubar[2] + cij[4]*vp[0] + cij[5]*vp[1] + cij[2]*vp[2];
196493642f1SJames Wright }
197493642f1SJames Wright 
19843bff553SJames Wright // Extrude the STGInflow profile through out the domain for an initial condition
19943bff553SJames Wright CEED_QFUNCTION(ICsSTG)(void *ctx, CeedInt Q,
20043bff553SJames Wright                        const CeedScalar *const *in, CeedScalar *const *out) {
20143bff553SJames Wright   // Inputs
20243bff553SJames Wright   const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
20343bff553SJames Wright 
20443bff553SJames Wright   // Outputs
20543bff553SJames Wright   CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
20643bff553SJames Wright 
20743bff553SJames Wright   const STGShur14Context stg_ctx = (STGShur14Context) ctx;
20843bff553SJames Wright   CeedScalar u[3], cij[6], eps, lt;
20943bff553SJames Wright   const CeedScalar theta0 = stg_ctx->theta0;
21043bff553SJames Wright   const CeedScalar P0     = stg_ctx->P0;
21143bff553SJames Wright   const CeedScalar cv     = stg_ctx->newtonian_ctx.cv;
21243bff553SJames Wright   const CeedScalar cp     = stg_ctx->newtonian_ctx.cp;
21343bff553SJames Wright   const CeedScalar Rd     = cp - cv;
21443bff553SJames Wright   const CeedScalar rho = P0 / (Rd * theta0);
21543bff553SJames Wright 
21643bff553SJames Wright   CeedPragmaSIMD
21743bff553SJames Wright   for(CeedInt i=0; i<Q; i++) {
21843bff553SJames Wright     InterpolateProfile(X[1][i], u, cij, &eps, &lt, stg_ctx);
21943bff553SJames Wright 
22043bff553SJames Wright     q0[0][i] = rho;
22143bff553SJames Wright     q0[1][i] = u[0] * rho;
22243bff553SJames Wright     q0[2][i] = u[1] * rho;
22343bff553SJames Wright     q0[3][i] = u[2] * rho;
22443bff553SJames Wright     q0[4][i] = rho * (0.5 * Dot3(u, u) + cv * theta0);
22543bff553SJames Wright   } // End of Quadrature Point Loop
22643bff553SJames Wright   return 0;
22743bff553SJames Wright }
22843bff553SJames Wright 
229493642f1SJames Wright /********************************************************************
230493642f1SJames Wright  * @brief QFunction to calculate the inflow boundary condition
231493642f1SJames Wright  *
232493642f1SJames Wright  * This will loop through quadrature points, calculate the wavemode amplitudes
233493642f1SJames Wright  * at each location, then calculate the actual velocity.
234493642f1SJames Wright  */
235493642f1SJames Wright CEED_QFUNCTION(STGShur14_Inflow)(void *ctx, CeedInt Q,
236493642f1SJames Wright                                  const CeedScalar *const *in,
237493642f1SJames Wright                                  CeedScalar *const *out) {
238493642f1SJames Wright 
239493642f1SJames Wright   //*INDENT-OFF*
240493642f1SJames Wright   const CeedScalar (*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA]) in[0],
241dd64951cSJames Wright                    (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA]) in[2],
242dd64951cSJames Wright                    (*X)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA]) in[3];
243493642f1SJames Wright 
244a6e8f989SJames Wright   CeedScalar(*v)[CEED_Q_VLA]            = (CeedScalar(*)[CEED_Q_VLA]) out[0],
245a6e8f989SJames Wright             (*jac_data_sur)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA]) out[1];
246493642f1SJames Wright 
247493642f1SJames Wright   //*INDENT-ON*
248493642f1SJames Wright 
249493642f1SJames Wright   const STGShur14Context stg_ctx = (STGShur14Context) ctx;
250493642f1SJames Wright   CeedScalar qn[STG_NMODES_MAX], u[3], ubar[3], cij[6], eps, lt;
251493642f1SJames Wright   const bool is_implicit  = stg_ctx->is_implicit;
252493642f1SJames Wright   const bool mean_only    = stg_ctx->mean_only;
253493642f1SJames Wright   const bool prescribe_T  = stg_ctx->prescribe_T;
254493642f1SJames Wright   const CeedScalar dx     = stg_ctx->dx;
255493642f1SJames Wright   const CeedScalar mu     = stg_ctx->newtonian_ctx.mu;
256493642f1SJames Wright   const CeedScalar time   = stg_ctx->time;
257493642f1SJames Wright   const CeedScalar theta0 = stg_ctx->theta0;
258493642f1SJames Wright   const CeedScalar P0     = stg_ctx->P0;
259493642f1SJames Wright   const CeedScalar cv     = stg_ctx->newtonian_ctx.cv;
260493642f1SJames Wright   const CeedScalar cp     = stg_ctx->newtonian_ctx.cp;
261493642f1SJames Wright   const CeedScalar Rd     = cp - cv;
262493642f1SJames Wright   const CeedScalar gamma  = cp/cv;
263493642f1SJames Wright 
264493642f1SJames Wright   CeedPragmaSIMD
265493642f1SJames Wright   for(CeedInt i=0; i<Q; i++) {
266493642f1SJames Wright     const CeedScalar rho = prescribe_T ? q[0][i] : P0 / (Rd * theta0);
267493642f1SJames Wright     const CeedScalar x[] = { X[0][i], X[1][i], X[2][i] };
268493642f1SJames Wright     const CeedScalar dXdx[2][3] = {
269493642f1SJames Wright       {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]},
270493642f1SJames Wright       {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]}
271493642f1SJames Wright     };
272493642f1SJames Wright 
273493642f1SJames Wright     CeedScalar h[3];
274493642f1SJames Wright     h[0] = dx;
275f6438f20SJames Wright     for (CeedInt j=1; j<3; j++)
276f6438f20SJames Wright       h[j] = 2/sqrt(Square(dXdx[0][j]) + Square(dXdx[1][j]));
277493642f1SJames Wright 
278493642f1SJames Wright     InterpolateProfile(X[1][i], ubar, cij, &eps, &lt, stg_ctx);
279493642f1SJames Wright     if (!mean_only) {
280493642f1SJames Wright       CalcSpectrum(X[1][i], eps, lt, h, mu/rho, qn, stg_ctx);
281493642f1SJames Wright       STGShur14_Calc(x, time, ubar, cij, qn, u, stg_ctx);
282493642f1SJames Wright     } else {
283493642f1SJames Wright       for (CeedInt j=0; j<3; j++) u[j] = ubar[j];
284493642f1SJames Wright     }
285493642f1SJames Wright 
286a6e8f989SJames Wright     const CeedScalar E_kinetic = .5 * rho * Dot3(u, u);
287493642f1SJames Wright     CeedScalar E_internal, P;
288493642f1SJames Wright     if (prescribe_T) {
289493642f1SJames Wright       // Temperature is being set weakly (theta0) and for constant cv this sets E_internal
290493642f1SJames Wright       E_internal = rho * cv * theta0;
291493642f1SJames Wright       // Find pressure using
292493642f1SJames Wright       P = rho * Rd * theta0; // interior rho with exterior T
293493642f1SJames Wright     } else {
294493642f1SJames Wright       E_internal = q[4][i] - E_kinetic; // uses prescribed rho and u, E from solution
295493642f1SJames Wright       P = E_internal * (gamma - 1.);
296493642f1SJames Wright     }
297493642f1SJames Wright 
298493642f1SJames Wright     const CeedScalar wdetJb  = (is_implicit ? -1. : 1.) * q_data_sur[0][i];
299493642f1SJames Wright     // ---- Normal vect
300493642f1SJames Wright     const CeedScalar norm[3] = {q_data_sur[1][i],
301493642f1SJames Wright                                 q_data_sur[2][i],
302493642f1SJames Wright                                 q_data_sur[3][i]
303493642f1SJames Wright                                };
304493642f1SJames Wright 
305493642f1SJames Wright     const CeedScalar E = E_internal + E_kinetic;
306493642f1SJames Wright 
307493642f1SJames Wright     // Velocity normal to the boundary
308a6e8f989SJames Wright     const CeedScalar u_normal = Dot3(norm, u);
309a6e8f989SJames Wright 
310493642f1SJames Wright     // The Physics
311493642f1SJames Wright     // Zero v so all future terms can safely sum into it
312493642f1SJames Wright     for (CeedInt j=0; j<5; j++) v[j][i] = 0.;
313493642f1SJames Wright 
314493642f1SJames Wright     // The Physics
315493642f1SJames Wright     // -- Density
316493642f1SJames Wright     v[0][i] -= wdetJb * rho * u_normal;
317493642f1SJames Wright 
318493642f1SJames Wright     // -- Momentum
319493642f1SJames Wright     for (CeedInt j=0; j<3; j++)
320493642f1SJames Wright       v[j+1][i] -= wdetJb *(rho * u_normal * u[j] +
321493642f1SJames Wright                             norm[j] * P);
322493642f1SJames Wright 
323493642f1SJames Wright     // -- Total Energy Density
324493642f1SJames Wright     v[4][i] -= wdetJb * u_normal * (E + P);
325a6e8f989SJames Wright 
326a6e8f989SJames Wright     jac_data_sur[0][i] = rho;
327a6e8f989SJames Wright     jac_data_sur[1][i] = u[0];
328a6e8f989SJames Wright     jac_data_sur[2][i] = u[1];
329a6e8f989SJames Wright     jac_data_sur[3][i] = u[2];
330a6e8f989SJames Wright     jac_data_sur[4][i] = E;
331a6e8f989SJames Wright     for (int j=0; j<6; j++) jac_data_sur[5+j][i] = 0.;
332493642f1SJames Wright   }
333493642f1SJames Wright   return 0;
334493642f1SJames Wright }
335493642f1SJames Wright 
336a6e8f989SJames Wright CEED_QFUNCTION(STGShur14_Inflow_Jacobian)(void *ctx, CeedInt Q,
337a6e8f989SJames Wright     const CeedScalar *const *in,
338a6e8f989SJames Wright     CeedScalar *const *out) {
339a6e8f989SJames Wright   // *INDENT-OFF*
340a6e8f989SJames Wright   // Inputs
341a6e8f989SJames Wright   const CeedScalar (*dq)[CEED_Q_VLA]           = (const CeedScalar(*)[CEED_Q_VLA])in[0],
342a6e8f989SJames Wright                    (*q_data_sur)[CEED_Q_VLA]   = (const CeedScalar(*)[CEED_Q_VLA])in[2],
343a6e8f989SJames Wright                    (*jac_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[4];
344a6e8f989SJames Wright   // Outputs
345a6e8f989SJames Wright   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
346a6e8f989SJames Wright   // *INDENT-ON*
347a6e8f989SJames Wright   const STGShur14Context stg_ctx = (STGShur14Context)ctx;
348a6e8f989SJames Wright   const bool implicit     = stg_ctx->is_implicit;
349a6e8f989SJames Wright   const CeedScalar cv     = stg_ctx->newtonian_ctx.cv;
350a6e8f989SJames Wright   const CeedScalar cp     = stg_ctx->newtonian_ctx.cp;
351a6e8f989SJames Wright   const CeedScalar Rd     = cp - cv;
352a6e8f989SJames Wright   const CeedScalar gamma  = cp/cv;
353a6e8f989SJames Wright 
354a6e8f989SJames Wright   const CeedScalar theta0 = stg_ctx->theta0;
355a6e8f989SJames Wright   const bool prescribe_T  = stg_ctx->prescribe_T;
356a6e8f989SJames Wright 
357a6e8f989SJames Wright   CeedPragmaSIMD
358a6e8f989SJames Wright   // Quadrature Point Loop
359a6e8f989SJames Wright   for (CeedInt i=0; i<Q; i++) {
360a6e8f989SJames Wright     // Setup
361a6e8f989SJames Wright     // Setup
362a6e8f989SJames Wright     // -- Interp-to-Interp q_data
363a6e8f989SJames Wright     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
364a6e8f989SJames Wright     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
365a6e8f989SJames Wright     // We can effect this by swapping the sign on this weight
366a6e8f989SJames Wright     const CeedScalar wdetJb  = (implicit ? -1. : 1.) * q_data_sur[0][i];
367a6e8f989SJames Wright 
368a6e8f989SJames Wright     // Calculate inflow values
369a6e8f989SJames Wright     CeedScalar velocity[3];
370a6e8f989SJames Wright     for (CeedInt j=0; j<3; j++) velocity[j] = jac_data_sur[5+j][i];
371a6e8f989SJames Wright 
372a6e8f989SJames Wright     // enabling user to choose between weak T and weak rho inflow
373a6e8f989SJames Wright     CeedScalar drho, dE, dP;
374a6e8f989SJames Wright     if (prescribe_T) {
375a6e8f989SJames Wright       // rho should be from the current solution
376a6e8f989SJames Wright       drho = dq[0][i];
377a6e8f989SJames Wright       CeedScalar dE_internal = drho * cv * theta0;
378a6e8f989SJames Wright       CeedScalar dE_kinetic = .5 * drho * Dot3(velocity, velocity);
379a6e8f989SJames Wright       dE = dE_internal + dE_kinetic;
380a6e8f989SJames Wright       dP = drho * Rd * theta0; // interior rho with exterior T
381a6e8f989SJames Wright     } else { // rho specified, E_internal from solution
382a6e8f989SJames Wright       drho = 0;
383a6e8f989SJames Wright       dE = dq[4][i];
384a6e8f989SJames Wright       dP = dE * (gamma - 1.);
385a6e8f989SJames Wright     }
386a6e8f989SJames Wright     const CeedScalar norm[3] = {q_data_sur[1][i],
387a6e8f989SJames Wright                                 q_data_sur[2][i],
388a6e8f989SJames Wright                                 q_data_sur[3][i]
389a6e8f989SJames Wright                                };
390a6e8f989SJames Wright 
391a6e8f989SJames Wright     const CeedScalar u_normal = Dot3(norm, velocity);
392a6e8f989SJames Wright 
393a6e8f989SJames Wright     v[0][i] = - wdetJb * drho * u_normal;
394a6e8f989SJames Wright     for (int j=0; j<3; j++)
395a6e8f989SJames Wright       v[j+1][i] = -wdetJb * (drho * u_normal * velocity[j] + norm[j] * dP);
396a6e8f989SJames Wright     v[4][i] = - wdetJb * u_normal * (dE + dP);
397a6e8f989SJames Wright   } // End Quadrature Point Loop
398a6e8f989SJames Wright   return 0;
399a6e8f989SJames Wright }
400a6e8f989SJames Wright 
401b7190ff7SJames Wright /********************************************************************
402b7190ff7SJames Wright  * @brief QFunction to calculate the strongly enforce inflow BC
403b7190ff7SJames Wright  *
404b7190ff7SJames Wright  * This QF is for the strong application of STG via libCEED (rather than
405b7190ff7SJames Wright  * through the native PETSc `DMAddBoundary` -> `bcFunc` method.
406b7190ff7SJames Wright  */
407b7190ff7SJames Wright CEED_QFUNCTION(STGShur14_Inflow_StrongQF)(void *ctx, CeedInt Q,
408b7190ff7SJames Wright     const CeedScalar *const *in, CeedScalar *const *out) {
409b7190ff7SJames Wright 
410b7190ff7SJames Wright   //*INDENT-OFF*
411b7190ff7SJames Wright   const CeedScalar (*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA]) in[0],
412b7190ff7SJames Wright                    (*coords)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA]) in[1],
413b7190ff7SJames Wright                    (*scale)                  = (const CeedScalar(*)) in[2];
414b7190ff7SJames Wright 
415b7190ff7SJames Wright   CeedScalar(*bcval)[CEED_Q_VLA]            = (CeedScalar(*)[CEED_Q_VLA]) out[0];
416b7190ff7SJames Wright   //*INDENT-ON*
417b7190ff7SJames Wright 
418b7190ff7SJames Wright   const STGShur14Context stg_ctx = (STGShur14Context) ctx;
419b7190ff7SJames Wright   CeedScalar qn[STG_NMODES_MAX], u[3], ubar[3], cij[6], eps, lt;
420b7190ff7SJames Wright   const bool mean_only    = stg_ctx->mean_only;
421b7190ff7SJames Wright   const CeedScalar dx     = stg_ctx->dx;
422b7190ff7SJames Wright   const CeedScalar mu     = stg_ctx->newtonian_ctx.mu;
423b7190ff7SJames Wright   const CeedScalar time   = stg_ctx->time;
424b7190ff7SJames Wright   const CeedScalar theta0 = stg_ctx->theta0;
425b7190ff7SJames Wright   const CeedScalar P0     = stg_ctx->P0;
426b7190ff7SJames Wright   const CeedScalar cv     = stg_ctx->newtonian_ctx.cv;
427b7190ff7SJames Wright   const CeedScalar cp     = stg_ctx->newtonian_ctx.cp;
428b7190ff7SJames Wright   const CeedScalar Rd     = cp - cv;
429b7190ff7SJames Wright   const CeedScalar rho    = P0 / (Rd * theta0);
430b7190ff7SJames Wright 
431b7190ff7SJames Wright   CeedPragmaSIMD
432b7190ff7SJames Wright   for(CeedInt i=0; i<Q; i++) {
433b7190ff7SJames Wright     const CeedScalar x[] = { coords[0][i], coords[1][i], coords[2][i] };
434b7190ff7SJames Wright     const CeedScalar dXdx[2][3] = {
435b7190ff7SJames Wright       {q_data_sur[4][i], q_data_sur[5][i], q_data_sur[6][i]},
436b7190ff7SJames Wright       {q_data_sur[7][i], q_data_sur[8][i], q_data_sur[9][i]}
437b7190ff7SJames Wright     };
438b7190ff7SJames Wright 
439b7190ff7SJames Wright     CeedScalar h[3];
440b7190ff7SJames Wright     h[0] = dx;
441f6438f20SJames Wright     for (CeedInt j=1; j<3; j++)
442f6438f20SJames Wright       h[j] = 2/sqrt(Square(dXdx[0][j]) + Square(dXdx[1][j]));
443b7190ff7SJames Wright 
444b7190ff7SJames Wright     InterpolateProfile(coords[1][i], ubar, cij, &eps, &lt, stg_ctx);
445b7190ff7SJames Wright     if (!mean_only) {
446b7190ff7SJames Wright       CalcSpectrum(coords[1][i], eps, lt, h, mu/rho, qn, stg_ctx);
447b7190ff7SJames Wright       STGShur14_Calc(x, time, ubar, cij, qn, u, stg_ctx);
448b7190ff7SJames Wright     } else {
449b7190ff7SJames Wright       for (CeedInt j=0; j<3; j++) u[j] = ubar[j];
450b7190ff7SJames Wright     }
451b7190ff7SJames Wright 
452b7190ff7SJames Wright     bcval[0][i] = scale[i] * rho;
453b7190ff7SJames Wright     bcval[1][i] = scale[i] * rho * u[0];
454b7190ff7SJames Wright     bcval[2][i] = scale[i] * rho * u[1];
455b7190ff7SJames Wright     bcval[3][i] = scale[i] * rho * u[2];
45666531c8bSJames Wright     bcval[4][i] = 0.;
457b7190ff7SJames Wright   }
458b7190ff7SJames Wright   return 0;
459b7190ff7SJames Wright }
460b7190ff7SJames Wright 
461493642f1SJames Wright #endif // stg_shur14_h
462