xref: /libCEED/examples/fluids/qfunctions/eulervortex.h (revision 49aac155e7a09736f56fb3abac0f57dab29f7cbf)
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 /// Euler traveling vortex initial condition and operator for Navier-Stokes
10 /// example using PETSc
11 
12 // Model from:
13 //   On the Order of Accuracy and Numerical Performance of Two Classes of Finite Volume WENO Schemes, Zhang, Zhang, and Shu (2011).
14 
15 #ifndef eulervortex_h
16 #define eulervortex_h
17 
18 #include <ceed.h>
19 #include <math.h>
20 
21 #include "utils.h"
22 
23 typedef struct EulerContext_ *EulerContext;
24 struct EulerContext_ {
25   CeedScalar center[3];
26   CeedScalar curr_time;
27   CeedScalar vortex_strength;
28   CeedScalar c_tau;
29   CeedScalar mean_velocity[3];
30   bool       implicit;
31   int        euler_test;
32   int        stabilization;  // See StabilizationType: 0=none, 1=SU, 2=SUPG
33 };
34 
35 // *****************************************************************************
36 // This function sets the initial conditions
37 //
38 //   Temperature:
39 //     T   = 1 - (gamma - 1) vortex_strength**2 exp(1 - r**2) / (8 gamma pi**2)
40 //   Density:
41 //     rho = (T/S_vortex)^(1 / (gamma - 1))
42 //   Pressure:
43 //     P   = rho * T
44 //   Velocity:
45 //     ui  = 1 + vortex_strength exp((1 - r**2)/2.) [yc - y, x - xc] / (2 pi)
46 //     r   = sqrt( (x - xc)**2 + (y - yc)**2 )
47 //   Velocity/Momentum Density:
48 //     Ui  = rho ui
49 //   Total Energy:
50 //     E   = P / (gamma - 1) + rho (u u)/2
51 //
52 // Constants:
53 //   cv              ,  Specific heat, constant volume
54 //   cp              ,  Specific heat, constant pressure
55 //   vortex_strength ,  Strength of vortex
56 //   center          ,  Location of bubble center
57 //   gamma  = cp / cv,  Specific heat ratio
58 //
59 // *****************************************************************************
60 
61 // *****************************************************************************
62 // This helper function provides support for the exact, time-dependent solution (currently not implemented) and IC formulation for Euler traveling
63 // vortex
64 // *****************************************************************************
65 CEED_QFUNCTION_HELPER int Exact_Euler(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
66   // Context
67   const EulerContext context         = (EulerContext)ctx;
68   const CeedScalar   vortex_strength = context->vortex_strength;
69   const CeedScalar  *center          = context->center;  // Center of the domain
70   const CeedScalar  *mean_velocity   = context->mean_velocity;
71 
72   // Setup
73   const CeedScalar gamma = 1.4;
74   const CeedScalar cv    = 2.5;
75   const CeedScalar R     = 1.;
76   const CeedScalar x = X[0], y = X[1];  // Coordinates
77   // Vortex center
78   const CeedScalar xc = center[0] + mean_velocity[0] * time;
79   const CeedScalar yc = center[1] + mean_velocity[1] * time;
80 
81   const CeedScalar x0       = x - xc;
82   const CeedScalar y0       = y - yc;
83   const CeedScalar r        = sqrt(x0 * x0 + y0 * y0);
84   const CeedScalar C        = vortex_strength * exp((1. - r * r) / 2.) / (2. * M_PI);
85   const CeedScalar delta_T  = -(gamma - 1.) * vortex_strength * vortex_strength * exp(1 - r * r) / (8. * gamma * M_PI * M_PI);
86   const CeedScalar S_vortex = 1;  // no perturbation in the entropy P / rho^gamma
87   const CeedScalar S_bubble = (gamma - 1.) * vortex_strength * vortex_strength / (8. * gamma * M_PI * M_PI);
88   CeedScalar       rho, P, T, E, u[3] = {0.};
89 
90   // Initial Conditions
91   switch (context->euler_test) {
92     case 0:  // Traveling vortex
93       T = 1 + delta_T;
94       // P = rho * T
95       // P = S * rho^gamma
96       // Solve for rho, then substitute for P
97       rho  = pow(T / S_vortex, 1 / (gamma - 1.));
98       P    = rho * T;
99       u[0] = mean_velocity[0] - C * y0;
100       u[1] = mean_velocity[1] + C * x0;
101 
102       // Assign exact solution
103       q[0] = rho;
104       q[1] = rho * u[0];
105       q[2] = rho * u[1];
106       q[3] = rho * u[2];
107       q[4] = P / (gamma - 1.) + rho * (u[0] * u[0] + u[1] * u[1]) / 2.;
108       break;
109     case 1:  // Constant zero velocity, density constant, total energy constant
110       rho = 1.;
111       E   = 2.;
112 
113       // Assign exact solution
114       q[0] = rho;
115       q[1] = rho * u[0];
116       q[2] = rho * u[1];
117       q[3] = rho * u[2];
118       q[4] = E;
119       break;
120     case 2:  // Constant nonzero velocity, density constant, total energy constant
121       rho  = 1.;
122       E    = 2.;
123       u[0] = mean_velocity[0];
124       u[1] = mean_velocity[1];
125 
126       // Assign exact solution
127       q[0] = rho;
128       q[1] = rho * u[0];
129       q[2] = rho * u[1];
130       q[3] = rho * u[2];
131       q[4] = E;
132       break;
133     case 3:  // Velocity zero, pressure constant (so density and internal energy will be non-constant), but the velocity should stay zero and the
134              // bubble won't diffuse
135       // (for Euler, where there is no thermal conductivity)
136       P   = 1.;
137       T   = 1. - S_bubble * exp(1. - r * r);
138       rho = P / (R * T);
139 
140       // Assign exact solution
141       q[0] = rho;
142       q[1] = rho * u[0];
143       q[2] = rho * u[1];
144       q[3] = rho * u[2];
145       q[4] = rho * (cv * T + (u[0] * u[0] + u[1] * u[1]) / 2.);
146       break;
147     case 4:  // Constant nonzero velocity, pressure constant (so density and internal energy will be non-constant),
148       // It should be transported across the domain, but velocity stays constant
149       P    = 1.;
150       T    = 1. - S_bubble * exp(1. - r * r);
151       rho  = P / (R * T);
152       u[0] = mean_velocity[0];
153       u[1] = mean_velocity[1];
154 
155       // Assign exact solution
156       q[0] = rho;
157       q[1] = rho * u[0];
158       q[2] = rho * u[1];
159       q[3] = rho * u[2];
160       q[4] = rho * (cv * T + (u[0] * u[0] + u[1] * u[1]) / 2.);
161       break;
162     case 5:  // non-smooth thermal bubble - cylinder
163       P    = 1.;
164       T    = 1. - (r < 1. ? S_bubble : 0.);
165       rho  = P / (R * T);
166       u[0] = mean_velocity[0];
167       u[1] = mean_velocity[1];
168 
169       // Assign exact solution
170       q[0] = rho;
171       q[1] = rho * u[0];
172       q[2] = rho * u[1];
173       q[3] = rho * u[2];
174       q[4] = rho * (cv * T + (u[0] * u[0] + u[1] * u[1]) / 2.);
175       break;
176   }
177   // Return
178   return 0;
179 }
180 
181 // *****************************************************************************
182 // Helper function for computing flux Jacobian
183 // *****************************************************************************
184 CEED_QFUNCTION_HELPER void ConvectiveFluxJacobian_Euler(CeedScalar dF[3][5][5], const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
185                                                         const CeedScalar gamma) {
186   CeedScalar u_sq = u[0] * u[0] + u[1] * u[1] + u[2] * u[2];  // Velocity square
187   for (CeedInt i = 0; i < 3; i++) {                           // Jacobian matrices for 3 directions
188     for (CeedInt j = 0; j < 3; j++) {                         // Rows of each Jacobian matrix
189       dF[i][j + 1][0] = ((i == j) ? ((gamma - 1.) * (u_sq / 2.)) : 0.) - u[i] * u[j];
190       for (CeedInt k = 0; k < 3; k++) {  // Columns of each Jacobian matrix
191         dF[i][0][k + 1]     = ((i == k) ? 1. : 0.);
192         dF[i][j + 1][k + 1] = ((j == k) ? u[i] : 0.) + ((i == k) ? u[j] : 0.) - ((i == j) ? u[k] : 0.) * (gamma - 1.);
193         dF[i][4][k + 1]     = ((i == k) ? (E * gamma / rho - (gamma - 1.) * u_sq / 2.) : 0.) - (gamma - 1.) * u[i] * u[k];
194       }
195       dF[i][j + 1][4] = ((i == j) ? (gamma - 1.) : 0.);
196     }
197     dF[i][4][0] = u[i] * ((gamma - 1.) * u_sq - E * gamma / rho);
198     dF[i][4][4] = u[i] * gamma;
199   }
200 }
201 
202 // *****************************************************************************
203 // Helper function for computing Tau elements (stabilization constant)
204 //   Model from:
205 //     Stabilized Methods for Compressible Flows, Hughes et al 2010
206 //
207 //   Spatial criterion #2 - Tau is a 3x3 diagonal matrix
208 //   Tau[i] = c_tau h[i] Xi(Pe) / rho(A[i]) (no sum)
209 //
210 // Where
211 //   c_tau     = stabilization constant (0.5 is reported as "optimal")
212 //   h[i]      = 2 length(dxdX[i])
213 //   Pe        = Peclet number ( Pe = sqrt(u u) / dot(dXdx,u) diffusivity )
214 //   Xi(Pe)    = coth Pe - 1. / Pe (1. at large local Peclet number )
215 //   rho(A[i]) = spectral radius of the convective flux Jacobian i, wave speed in direction i
216 // *****************************************************************************
217 CEED_QFUNCTION_HELPER void Tau_spatial(CeedScalar Tau_x[3], const CeedScalar dXdx[3][3], const CeedScalar u[3], const CeedScalar sound_speed,
218                                        const CeedScalar c_tau) {
219   for (CeedInt i = 0; i < 3; i++) {
220     // length of element in direction i
221     CeedScalar h = 2 / sqrt(dXdx[0][i] * dXdx[0][i] + dXdx[1][i] * dXdx[1][i] + dXdx[2][i] * dXdx[2][i]);
222     // fastest wave in direction i
223     CeedScalar fastest_wave = fabs(u[i]) + sound_speed;
224     Tau_x[i]                = c_tau * h / fastest_wave;
225   }
226 }
227 
228 // *****************************************************************************
229 // This QFunction sets the initial conditions for Euler traveling vortex
230 // *****************************************************************************
231 CEED_QFUNCTION(ICsEuler)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
232   // Inputs
233   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
234 
235   // Outputs
236   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
237   const EulerContext context  = (EulerContext)ctx;
238 
239   // Quadrature Point Loop
240   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
241     const CeedScalar x[]  = {X[0][i], X[1][i], X[2][i]};
242     CeedScalar       q[5] = {0.};
243 
244     Exact_Euler(3, context->curr_time, x, 5, q, ctx);
245 
246     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
247   }  // End of Quadrature Point Loop
248 
249   // Return
250   return 0;
251 }
252 
253 // *****************************************************************************
254 // This QFunction implements the following formulation of Euler equations with explicit time stepping method
255 //
256 // This is 3D Euler for compressible gas dynamics in conservation form with state variables of density, momentum density, and total energy density.
257 //
258 // State Variables: q = ( rho, U1, U2, U3, E )
259 //   rho - Mass Density
260 //   Ui  - Momentum Density,      Ui = rho ui
261 //   E   - Total Energy Density,  E  = P / (gamma - 1) + rho (u u)/2
262 //
263 // Euler Equations:
264 //   drho/dt + div( U )                   = 0
265 //   dU/dt   + div( rho (u x u) + P I3 )  = 0
266 //   dE/dt   + div( (E + P) u )           = 0
267 //
268 // Equation of State:
269 //   P = (gamma - 1) (E - rho (u u) / 2)
270 //
271 // Constants:
272 //   cv              ,  Specific heat, constant volume
273 //   cp              ,  Specific heat, constant pressure
274 //   g               ,  Gravity
275 //   gamma  = cp / cv,  Specific heat ratio
276 // *****************************************************************************
277 CEED_QFUNCTION(Euler)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
278   // Inputs
279   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
280   const CeedScalar(*dq)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
281   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
282 
283   // Outputs
284   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
285   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
286 
287   EulerContext     context = (EulerContext)ctx;
288   const CeedScalar c_tau   = context->c_tau;
289   const CeedScalar gamma   = 1.4;
290 
291   // Quadrature Point Loop
292   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
293     // Setup
294     // -- Interp in
295     const CeedScalar rho      = q[0][i];
296     const CeedScalar u[3]     = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
297     const CeedScalar E        = q[4][i];
298     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
299     const CeedScalar dU[3][3] = {
300         {dq[0][1][i], dq[1][1][i], dq[2][1][i]},
301         {dq[0][2][i], dq[1][2][i], dq[2][2][i]},
302         {dq[0][3][i], dq[1][3][i], dq[2][3][i]}
303     };
304     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
305     // -- Interp-to-Interp q_data
306     const CeedScalar wdetJ = q_data[0][i];
307     // -- Interp-to-Grad q_data
308     // ---- Inverse of change of coordinate matrix: X_i,j
309     const CeedScalar dXdx[3][3] = {
310         {q_data[1][i], q_data[2][i], q_data[3][i]},
311         {q_data[4][i], q_data[5][i], q_data[6][i]},
312         {q_data[7][i], q_data[8][i], q_data[9][i]}
313     };
314     // dU/dx
315     CeedScalar drhodx[3]       = {0.};
316     CeedScalar dEdx[3]         = {0.};
317     CeedScalar dUdx[3][3]      = {{0.}};
318     CeedScalar dXdxdXdxT[3][3] = {{0.}};
319     for (CeedInt j = 0; j < 3; j++) {
320       for (CeedInt k = 0; k < 3; k++) {
321         drhodx[j] += drho[k] * dXdx[k][j];
322         dEdx[j] += dE[k] * dXdx[k][j];
323         for (CeedInt l = 0; l < 3; l++) {
324           dUdx[j][k] += dU[j][l] * dXdx[l][k];
325           dXdxdXdxT[j][k] += dXdx[j][l] * dXdx[k][l];  // dXdx_j,k * dXdx_k,j
326         }
327       }
328     }
329     // Pressure
330     const CeedScalar E_kinetic = 0.5 * rho * (u[0] * u[0] + u[1] * u[1] + u[2] * u[2]), E_internal = E - E_kinetic,
331                      P = E_internal * (gamma - 1.);  // P = pressure
332 
333     // The Physics
334     // Zero v and dv so all future terms can safely sum into it
335     for (CeedInt j = 0; j < 5; j++) {
336       v[j][i] = 0.;
337       for (CeedInt k = 0; k < 3; k++) dv[k][j][i] = 0.;
338     }
339 
340     // -- Density
341     // ---- u rho
342     for (CeedInt j = 0; j < 3; j++) dv[j][0][i] += wdetJ * (rho * u[0] * dXdx[j][0] + rho * u[1] * dXdx[j][1] + rho * u[2] * dXdx[j][2]);
343     // -- Momentum
344     // ---- rho (u x u) + P I3
345     for (CeedInt j = 0; j < 3; j++) {
346       for (CeedInt k = 0; k < 3; k++) {
347         dv[k][j + 1][i] += wdetJ * ((rho * u[j] * u[0] + (j == 0 ? P : 0.)) * dXdx[k][0] + (rho * u[j] * u[1] + (j == 1 ? P : 0.)) * dXdx[k][1] +
348                                     (rho * u[j] * u[2] + (j == 2 ? P : 0.)) * dXdx[k][2]);
349       }
350     }
351     // -- Total Energy Density
352     // ---- (E + P) u
353     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] += wdetJ * (E + P) * (u[0] * dXdx[j][0] + u[1] * dXdx[j][1] + u[2] * dXdx[j][2]);
354 
355     // --Stabilization terms
356     // ---- jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
357     CeedScalar jacob_F_conv[3][5][5] = {{{0.}}};
358     ConvectiveFluxJacobian_Euler(jacob_F_conv, rho, u, E, gamma);
359 
360     // ---- dqdx collects drhodx, dUdx and dEdx in one vector
361     CeedScalar dqdx[5][3];
362     for (CeedInt j = 0; j < 3; j++) {
363       dqdx[0][j] = drhodx[j];
364       dqdx[4][j] = dEdx[j];
365       for (CeedInt k = 0; k < 3; k++) dqdx[k + 1][j] = dUdx[k][j];
366     }
367 
368     // ---- strong_conv = dF/dq * dq/dx    (Strong convection)
369     CeedScalar strong_conv[5] = {0.};
370     for (CeedInt j = 0; j < 3; j++) {
371       for (CeedInt k = 0; k < 5; k++) {
372         for (CeedInt l = 0; l < 5; l++) strong_conv[k] += jacob_F_conv[j][k][l] * dqdx[l][j];
373       }
374     }
375 
376     // Stabilization
377     // -- Tau elements
378     const CeedScalar sound_speed = sqrt(gamma * P / rho);
379     CeedScalar       Tau_x[3]    = {0.};
380     Tau_spatial(Tau_x, dXdx, u, sound_speed, c_tau);
381 
382     // -- Stabilization method: none or SU
383     CeedScalar stab[5][3] = {{0.}};
384     switch (context->stabilization) {
385       case 0:  // Galerkin
386         break;
387       case 1:  // SU
388         for (CeedInt j = 0; j < 3; j++) {
389           for (CeedInt k = 0; k < 5; k++) {
390             for (CeedInt l = 0; l < 5; l++) stab[k][j] += jacob_F_conv[j][k][l] * Tau_x[j] * strong_conv[l];
391           }
392         }
393 
394         for (CeedInt j = 0; j < 5; j++) {
395           for (CeedInt k = 0; k < 3; k++) dv[k][j][i] -= wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]);
396         }
397         break;
398       case 2:  // SUPG is not implemented for explicit scheme
399         break;
400     }
401 
402   }  // End Quadrature Point Loop
403 
404   // Return
405   return 0;
406 }
407 
408 // *****************************************************************************
409 // This QFunction implements the Euler equations with (mentioned above) with implicit time stepping method
410 // *****************************************************************************
411 CEED_QFUNCTION(IFunction_Euler)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
412   // Inputs
413   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
414   const CeedScalar(*dq)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
415   const CeedScalar(*q_dot)[CEED_Q_VLA]  = (const CeedScalar(*)[CEED_Q_VLA])in[2];
416   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[3];
417 
418   // Outputs
419   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
420   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
421 
422   EulerContext     context = (EulerContext)ctx;
423   const CeedScalar c_tau   = context->c_tau;
424   const CeedScalar gamma   = 1.4;
425 
426   // Quadrature Point Loop
427   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
428     // Setup
429     // -- Interp in
430     const CeedScalar rho      = q[0][i];
431     const CeedScalar u[3]     = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
432     const CeedScalar E        = q[4][i];
433     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
434     const CeedScalar dU[3][3] = {
435         {dq[0][1][i], dq[1][1][i], dq[2][1][i]},
436         {dq[0][2][i], dq[1][2][i], dq[2][2][i]},
437         {dq[0][3][i], dq[1][3][i], dq[2][3][i]}
438     };
439     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
440     // -- Interp-to-Interp q_data
441     const CeedScalar wdetJ = q_data[0][i];
442     // -- Interp-to-Grad q_data
443     // ---- Inverse of change of coordinate matrix: X_i,j
444     const CeedScalar dXdx[3][3] = {
445         {q_data[1][i], q_data[2][i], q_data[3][i]},
446         {q_data[4][i], q_data[5][i], q_data[6][i]},
447         {q_data[7][i], q_data[8][i], q_data[9][i]}
448     };
449     // dU/dx
450     CeedScalar drhodx[3]       = {0.};
451     CeedScalar dEdx[3]         = {0.};
452     CeedScalar dUdx[3][3]      = {{0.}};
453     CeedScalar dXdxdXdxT[3][3] = {{0.}};
454     for (CeedInt j = 0; j < 3; j++) {
455       for (CeedInt k = 0; k < 3; k++) {
456         drhodx[j] += drho[k] * dXdx[k][j];
457         dEdx[j] += dE[k] * dXdx[k][j];
458         for (CeedInt l = 0; l < 3; l++) {
459           dUdx[j][k] += dU[j][l] * dXdx[l][k];
460           dXdxdXdxT[j][k] += dXdx[j][l] * dXdx[k][l];  // dXdx_j,k * dXdx_k,j
461         }
462       }
463     }
464     const CeedScalar E_kinetic = 0.5 * rho * (u[0] * u[0] + u[1] * u[1] + u[2] * u[2]), E_internal = E - E_kinetic,
465                      P = E_internal * (gamma - 1.);  // P = pressure
466 
467     // The Physics
468     // Zero v and dv so all future terms can safely sum into it
469     for (CeedInt j = 0; j < 5; j++) {
470       v[j][i] = 0.;
471       for (CeedInt k = 0; k < 3; k++) dv[k][j][i] = 0.;
472     }
473     //-----mass matrix
474     for (CeedInt j = 0; j < 5; j++) v[j][i] += wdetJ * q_dot[j][i];
475 
476     // -- Density
477     // ---- u rho
478     for (CeedInt j = 0; j < 3; j++) dv[j][0][i] -= wdetJ * (rho * u[0] * dXdx[j][0] + rho * u[1] * dXdx[j][1] + rho * u[2] * dXdx[j][2]);
479     // -- Momentum
480     // ---- rho (u x u) + P I3
481     for (CeedInt j = 0; j < 3; j++) {
482       for (CeedInt k = 0; k < 3; k++) {
483         dv[k][j + 1][i] -= wdetJ * ((rho * u[j] * u[0] + (j == 0 ? P : 0.)) * dXdx[k][0] + (rho * u[j] * u[1] + (j == 1 ? P : 0.)) * dXdx[k][1] +
484                                     (rho * u[j] * u[2] + (j == 2 ? P : 0.)) * dXdx[k][2]);
485       }
486     }
487     // -- Total Energy Density
488     // ---- (E + P) u
489     for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * (E + P) * (u[0] * dXdx[j][0] + u[1] * dXdx[j][1] + u[2] * dXdx[j][2]);
490 
491     // -- Stabilization terms
492     // ---- jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
493     CeedScalar jacob_F_conv[3][5][5] = {{{0.}}};
494     ConvectiveFluxJacobian_Euler(jacob_F_conv, rho, u, E, gamma);
495 
496     // ---- dqdx collects drhodx, dUdx and dEdx in one vector
497     CeedScalar dqdx[5][3];
498     for (CeedInt j = 0; j < 3; j++) {
499       dqdx[0][j] = drhodx[j];
500       dqdx[4][j] = dEdx[j];
501       for (CeedInt k = 0; k < 3; k++) dqdx[k + 1][j] = dUdx[k][j];
502     }
503 
504     // ---- strong_conv = dF/dq * dq/dx    (Strong convection)
505     CeedScalar strong_conv[5] = {0.};
506     for (CeedInt j = 0; j < 3; j++) {
507       for (CeedInt k = 0; k < 5; k++) {
508         for (CeedInt l = 0; l < 5; l++) strong_conv[k] += jacob_F_conv[j][k][l] * dqdx[l][j];
509       }
510     }
511 
512     // ---- Strong residual
513     CeedScalar strong_res[5];
514     for (CeedInt j = 0; j < 5; j++) strong_res[j] = q_dot[j][i] + strong_conv[j];
515 
516     // Stabilization
517     // -- Tau elements
518     const CeedScalar sound_speed = sqrt(gamma * P / rho);
519     CeedScalar       Tau_x[3]    = {0.};
520     Tau_spatial(Tau_x, dXdx, u, sound_speed, c_tau);
521 
522     // -- Stabilization method: none, SU, or SUPG
523     CeedScalar stab[5][3] = {{0.}};
524     switch (context->stabilization) {
525       case 0:  // Galerkin
526         break;
527       case 1:  // SU
528         for (CeedInt j = 0; j < 3; j++) {
529           for (CeedInt k = 0; k < 5; k++) {
530             for (CeedInt l = 0; l < 5; l++) stab[k][j] += jacob_F_conv[j][k][l] * Tau_x[j] * strong_conv[l];
531           }
532         }
533 
534         for (CeedInt j = 0; j < 5; j++) {
535           for (CeedInt k = 0; k < 3; k++) dv[k][j][i] += wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]);
536         }
537         break;
538       case 2:  // SUPG
539         for (CeedInt j = 0; j < 3; j++) {
540           for (CeedInt k = 0; k < 5; k++) {
541             for (CeedInt l = 0; l < 5; l++) stab[k][j] = jacob_F_conv[j][k][l] * Tau_x[j] * strong_res[l];
542           }
543         }
544 
545         for (CeedInt j = 0; j < 5; j++) {
546           for (CeedInt k = 0; k < 3; k++) dv[k][j][i] += wdetJ * (stab[j][0] * dXdx[k][0] + stab[j][1] * dXdx[k][1] + stab[j][2] * dXdx[k][2]);
547         }
548         break;
549     }
550   }  // End Quadrature Point Loop
551 
552   // Return
553   return 0;
554 }
555 // *****************************************************************************
556 // This QFunction sets the inflow boundary conditions for the traveling vortex problem.
557 //
558 //  Prescribed T_inlet and P_inlet are converted to conservative variables and applied weakly.
559 // *****************************************************************************
560 CEED_QFUNCTION(TravelingVortex_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
561   // Inputs
562   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
563   // Outputs
564   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
565   EulerContext     context       = (EulerContext)ctx;
566   const int        euler_test    = context->euler_test;
567   const bool       implicit      = context->implicit;
568   CeedScalar      *mean_velocity = context->mean_velocity;
569   const CeedScalar cv            = 2.5;
570   const CeedScalar R             = 1.;
571   CeedScalar       T_inlet;
572   CeedScalar       P_inlet;
573 
574   // For test cases 1 and 3 the background velocity is zero
575   if (euler_test == 1 || euler_test == 3) {
576     for (CeedInt i = 0; i < 3; i++) mean_velocity[i] = 0.;
577   }
578 
579   // For test cases 1 and 2, T_inlet = T_inlet = 0.4
580   if (euler_test == 1 || euler_test == 2) T_inlet = P_inlet = .4;
581   else T_inlet = P_inlet = 1.;
582 
583   // Quadrature Point Loop
584   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
585     // Setup
586     // -- Interp-to-Interp q_data
587     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
588     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
589     // We can effect this by swapping the sign on this weight
590     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
591     // ---- Normal vect
592     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
593 
594     // face_normal = Normal vector of the face
595     const CeedScalar face_normal = norm[0] * mean_velocity[0] + norm[1] * mean_velocity[1] + norm[2] * mean_velocity[2];
596     // The Physics
597     // Zero v so all future terms can safely sum into it
598     for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.;
599 
600     // Implementing in/outflow BCs
601     if (face_normal > 0) {
602     } else {  // inflow
603       const CeedScalar rho_inlet       = P_inlet / (R * T_inlet);
604       const CeedScalar E_kinetic_inlet = (mean_velocity[0] * mean_velocity[0] + mean_velocity[1] * mean_velocity[1]) / 2.;
605       // incoming total energy
606       const CeedScalar E_inlet = rho_inlet * (cv * T_inlet + E_kinetic_inlet);
607 
608       // The Physics
609       // -- Density
610       v[0][i] -= wdetJb * rho_inlet * face_normal;
611 
612       // -- Momentum
613       for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho_inlet * face_normal * mean_velocity[j] + norm[j] * P_inlet);
614 
615       // -- Total Energy Density
616       v[4][i] -= wdetJb * face_normal * (E_inlet + P_inlet);
617     }
618 
619   }  // End Quadrature Point Loop
620   return 0;
621 }
622 
623 // *****************************************************************************
624 // This QFunction sets the outflow boundary conditions for the Euler solver.
625 //
626 //  Outflow BCs:
627 //    The validity of the weak form of the governing equations is extended to the outflow.
628 // *****************************************************************************
629 CEED_QFUNCTION(Euler_Outflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
630   // Inputs
631   const CeedScalar(*q)[CEED_Q_VLA]          = (const CeedScalar(*)[CEED_Q_VLA])in[0];
632   const CeedScalar(*q_data_sur)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
633 
634   // Outputs
635   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
636   EulerContext context       = (EulerContext)ctx;
637   const bool   implicit      = context->implicit;
638   CeedScalar  *mean_velocity = context->mean_velocity;
639 
640   const CeedScalar gamma = 1.4;
641 
642   // Quadrature Point Loop
643   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
644     // Setup
645     // -- Interp in
646     const CeedScalar rho  = q[0][i];
647     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
648     const CeedScalar E    = q[4][i];
649 
650     // -- Interp-to-Interp q_data
651     // For explicit mode, the surface integral is on the RHS of ODE q_dot = f(q).
652     // For implicit mode, it gets pulled to the LHS of implicit ODE/DAE g(q_dot, q).
653     // We can effect this by swapping the sign on this weight
654     const CeedScalar wdetJb = (implicit ? -1. : 1.) * q_data_sur[0][i];
655     // ---- Normal vectors
656     const CeedScalar norm[3] = {q_data_sur[1][i], q_data_sur[2][i], q_data_sur[3][i]};
657 
658     // face_normal = Normal vector of the face
659     const CeedScalar face_normal = norm[0] * mean_velocity[0] + norm[1] * mean_velocity[1] + norm[2] * mean_velocity[2];
660     // The Physics
661     // Zero v so all future terms can safely sum into it
662     for (CeedInt j = 0; j < 5; j++) v[j][i] = 0;
663 
664     // Implementing in/outflow BCs
665     if (face_normal > 0) {  // outflow
666       const CeedScalar E_kinetic = (u[0] * u[0] + u[1] * u[1]) / 2.;
667       const CeedScalar P         = (E - E_kinetic * rho) * (gamma - 1.);              // pressure
668       const CeedScalar u_normal  = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2];  // Normal velocity
669       // The Physics
670       // -- Density
671       v[0][i] -= wdetJb * rho * u_normal;
672 
673       // -- Momentum
674       for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho * u_normal * u[j] + norm[j] * P);
675 
676       // -- Total Energy Density
677       v[4][i] -= wdetJb * u_normal * (E + P);
678     }
679   }  // End Quadrature Point Loop
680   return 0;
681 }
682 
683 // *****************************************************************************
684 
685 #endif  // eulervortex_h
686