xref: /libCEED/examples/fluids/qfunctions/eulervortex.h (revision a71faab149de599cd7784196409e851b67144f0a)
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)            = 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     CeedScalar       wdetJ, dXdx[3][3];
306     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
307     // dU/dx
308     CeedScalar drhodx[3]       = {0.};
309     CeedScalar dEdx[3]         = {0.};
310     CeedScalar dUdx[3][3]      = {{0.}};
311     CeedScalar dXdxdXdxT[3][3] = {{0.}};
312     for (CeedInt j = 0; j < 3; j++) {
313       for (CeedInt k = 0; k < 3; k++) {
314         drhodx[j] += drho[k] * dXdx[k][j];
315         dEdx[j] += dE[k] * dXdx[k][j];
316         for (CeedInt l = 0; l < 3; l++) {
317           dUdx[j][k] += dU[j][l] * dXdx[l][k];
318           dXdxdXdxT[j][k] += dXdx[j][l] * dXdx[k][l];  // dXdx_j,k * dXdx_k,j
319         }
320       }
321     }
322     // Pressure
323     const CeedScalar E_kinetic = 0.5 * rho * (u[0] * u[0] + u[1] * u[1] + u[2] * u[2]), E_internal = E - E_kinetic,
324                      P = E_internal * (gamma - 1.);  // P = pressure
325 
326     // The Physics
327     // Zero v and dv so all future terms can safely sum into it
328     for (CeedInt j = 0; j < 5; j++) {
329       v[j][i] = 0.;
330       for (CeedInt k = 0; k < 3; k++) dv[k][j][i] = 0.;
331     }
332 
333     // -- Density
334     // ---- u rho
335     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]);
336     // -- Momentum
337     // ---- rho (u x u) + P I3
338     for (CeedInt j = 0; j < 3; j++) {
339       for (CeedInt k = 0; k < 3; k++) {
340         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] +
341                                     (rho * u[j] * u[2] + (j == 2 ? P : 0.)) * dXdx[k][2]);
342       }
343     }
344     // -- Total Energy Density
345     // ---- (E + P) u
346     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]);
347 
348     // --Stabilization terms
349     // ---- jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
350     CeedScalar jacob_F_conv[3][5][5] = {{{0.}}};
351     ConvectiveFluxJacobian_Euler(jacob_F_conv, rho, u, E, gamma);
352 
353     // ---- dqdx collects drhodx, dUdx and dEdx in one vector
354     CeedScalar dqdx[5][3];
355     for (CeedInt j = 0; j < 3; j++) {
356       dqdx[0][j] = drhodx[j];
357       dqdx[4][j] = dEdx[j];
358       for (CeedInt k = 0; k < 3; k++) dqdx[k + 1][j] = dUdx[k][j];
359     }
360 
361     // ---- strong_conv = dF/dq * dq/dx    (Strong convection)
362     CeedScalar strong_conv[5] = {0.};
363     for (CeedInt j = 0; j < 3; j++) {
364       for (CeedInt k = 0; k < 5; k++) {
365         for (CeedInt l = 0; l < 5; l++) strong_conv[k] += jacob_F_conv[j][k][l] * dqdx[l][j];
366       }
367     }
368 
369     // Stabilization
370     // -- Tau elements
371     const CeedScalar sound_speed = sqrt(gamma * P / rho);
372     CeedScalar       Tau_x[3]    = {0.};
373     Tau_spatial(Tau_x, dXdx, u, sound_speed, c_tau);
374 
375     // -- Stabilization method: none or SU
376     CeedScalar stab[5][3] = {{0.}};
377     switch (context->stabilization) {
378       case 0:  // Galerkin
379         break;
380       case 1:  // SU
381         for (CeedInt j = 0; j < 3; j++) {
382           for (CeedInt k = 0; k < 5; k++) {
383             for (CeedInt l = 0; l < 5; l++) stab[k][j] += jacob_F_conv[j][k][l] * Tau_x[j] * strong_conv[l];
384           }
385         }
386 
387         for (CeedInt j = 0; j < 5; j++) {
388           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]);
389         }
390         break;
391       case 2:  // SUPG is not implemented for explicit scheme
392         break;
393     }
394 
395   }  // End Quadrature Point Loop
396 
397   // Return
398   return 0;
399 }
400 
401 // *****************************************************************************
402 // This QFunction implements the Euler equations with (mentioned above) with implicit time stepping method
403 // *****************************************************************************
404 CEED_QFUNCTION(IFunction_Euler)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
405   // Inputs
406   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
407   const CeedScalar(*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
408   const CeedScalar(*q_dot)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
409   const CeedScalar(*q_data)            = in[3];
410 
411   // Outputs
412   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
413   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
414 
415   EulerContext     context = (EulerContext)ctx;
416   const CeedScalar c_tau   = context->c_tau;
417   const CeedScalar gamma   = 1.4;
418 
419   // Quadrature Point Loop
420   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
421     // Setup
422     // -- Interp in
423     const CeedScalar rho      = q[0][i];
424     const CeedScalar u[3]     = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
425     const CeedScalar E        = q[4][i];
426     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
427     const CeedScalar dU[3][3] = {
428         {dq[0][1][i], dq[1][1][i], dq[2][1][i]},
429         {dq[0][2][i], dq[1][2][i], dq[2][2][i]},
430         {dq[0][3][i], dq[1][3][i], dq[2][3][i]}
431     };
432     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
433     CeedScalar       wdetJ, dXdx[3][3];
434     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
435     // dU/dx
436     CeedScalar drhodx[3]       = {0.};
437     CeedScalar dEdx[3]         = {0.};
438     CeedScalar dUdx[3][3]      = {{0.}};
439     CeedScalar dXdxdXdxT[3][3] = {{0.}};
440     for (CeedInt j = 0; j < 3; j++) {
441       for (CeedInt k = 0; k < 3; k++) {
442         drhodx[j] += drho[k] * dXdx[k][j];
443         dEdx[j] += dE[k] * dXdx[k][j];
444         for (CeedInt l = 0; l < 3; l++) {
445           dUdx[j][k] += dU[j][l] * dXdx[l][k];
446           dXdxdXdxT[j][k] += dXdx[j][l] * dXdx[k][l];  // dXdx_j,k * dXdx_k,j
447         }
448       }
449     }
450     const CeedScalar E_kinetic = 0.5 * rho * (u[0] * u[0] + u[1] * u[1] + u[2] * u[2]), E_internal = E - E_kinetic,
451                      P = E_internal * (gamma - 1.);  // P = pressure
452 
453     // The Physics
454     // Zero v and dv so all future terms can safely sum into it
455     for (CeedInt j = 0; j < 5; j++) {
456       v[j][i] = 0.;
457       for (CeedInt k = 0; k < 3; k++) dv[k][j][i] = 0.;
458     }
459     //-----mass matrix
460     for (CeedInt j = 0; j < 5; j++) v[j][i] += wdetJ * q_dot[j][i];
461 
462     // -- Density
463     // ---- u rho
464     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]);
465     // -- Momentum
466     // ---- rho (u x u) + P I3
467     for (CeedInt j = 0; j < 3; j++) {
468       for (CeedInt k = 0; k < 3; k++) {
469         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] +
470                                     (rho * u[j] * u[2] + (j == 2 ? P : 0.)) * dXdx[k][2]);
471       }
472     }
473     // -- Total Energy Density
474     // ---- (E + P) u
475     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]);
476 
477     // -- Stabilization terms
478     // ---- jacob_F_conv[3][5][5] = dF(convective)/dq at each direction
479     CeedScalar jacob_F_conv[3][5][5] = {{{0.}}};
480     ConvectiveFluxJacobian_Euler(jacob_F_conv, rho, u, E, gamma);
481 
482     // ---- dqdx collects drhodx, dUdx and dEdx in one vector
483     CeedScalar dqdx[5][3];
484     for (CeedInt j = 0; j < 3; j++) {
485       dqdx[0][j] = drhodx[j];
486       dqdx[4][j] = dEdx[j];
487       for (CeedInt k = 0; k < 3; k++) dqdx[k + 1][j] = dUdx[k][j];
488     }
489 
490     // ---- strong_conv = dF/dq * dq/dx    (Strong convection)
491     CeedScalar strong_conv[5] = {0.};
492     for (CeedInt j = 0; j < 3; j++) {
493       for (CeedInt k = 0; k < 5; k++) {
494         for (CeedInt l = 0; l < 5; l++) strong_conv[k] += jacob_F_conv[j][k][l] * dqdx[l][j];
495       }
496     }
497 
498     // ---- Strong residual
499     CeedScalar strong_res[5];
500     for (CeedInt j = 0; j < 5; j++) strong_res[j] = q_dot[j][i] + strong_conv[j];
501 
502     // Stabilization
503     // -- Tau elements
504     const CeedScalar sound_speed = sqrt(gamma * P / rho);
505     CeedScalar       Tau_x[3]    = {0.};
506     Tau_spatial(Tau_x, dXdx, u, sound_speed, c_tau);
507 
508     // -- Stabilization method: none, SU, or SUPG
509     CeedScalar stab[5][3] = {{0.}};
510     switch (context->stabilization) {
511       case 0:  // Galerkin
512         break;
513       case 1:  // SU
514         for (CeedInt j = 0; j < 3; j++) {
515           for (CeedInt k = 0; k < 5; k++) {
516             for (CeedInt l = 0; l < 5; l++) stab[k][j] += jacob_F_conv[j][k][l] * Tau_x[j] * strong_conv[l];
517           }
518         }
519 
520         for (CeedInt j = 0; j < 5; j++) {
521           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]);
522         }
523         break;
524       case 2:  // SUPG
525         for (CeedInt j = 0; j < 3; j++) {
526           for (CeedInt k = 0; k < 5; k++) {
527             for (CeedInt l = 0; l < 5; l++) stab[k][j] = jacob_F_conv[j][k][l] * Tau_x[j] * strong_res[l];
528           }
529         }
530 
531         for (CeedInt j = 0; j < 5; j++) {
532           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]);
533         }
534         break;
535     }
536   }  // End Quadrature Point Loop
537 
538   // Return
539   return 0;
540 }
541 // *****************************************************************************
542 // This QFunction sets the inflow boundary conditions for the traveling vortex problem.
543 //
544 //  Prescribed T_inlet and P_inlet are converted to conservative variables and applied weakly.
545 // *****************************************************************************
546 CEED_QFUNCTION(TravelingVortex_Inflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
547   // Inputs
548   const CeedScalar(*q_data_sur) = in[2];
549   // Outputs
550   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
551   EulerContext     context       = (EulerContext)ctx;
552   const int        euler_test    = context->euler_test;
553   const bool       is_implicit   = context->implicit;
554   CeedScalar      *mean_velocity = context->mean_velocity;
555   const CeedScalar cv            = 2.5;
556   const CeedScalar R             = 1.;
557   CeedScalar       T_inlet;
558   CeedScalar       P_inlet;
559 
560   // For test cases 1 and 3 the background velocity is zero
561   if (euler_test == 1 || euler_test == 3) {
562     for (CeedInt i = 0; i < 3; i++) mean_velocity[i] = 0.;
563   }
564 
565   // For test cases 1 and 2, T_inlet = T_inlet = 0.4
566   if (euler_test == 1 || euler_test == 2) T_inlet = P_inlet = .4;
567   else T_inlet = P_inlet = 1.;
568 
569   // Quadrature Point Loop
570   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
571     CeedScalar wdetJb, norm[3];
572     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
573     wdetJb *= is_implicit ? -1. : 1.;
574 
575     // face_normal = Normal vector of the face
576     const CeedScalar face_normal = norm[0] * mean_velocity[0] + norm[1] * mean_velocity[1] + norm[2] * mean_velocity[2];
577     // The Physics
578     // Zero v so all future terms can safely sum into it
579     for (CeedInt j = 0; j < 5; j++) v[j][i] = 0.;
580 
581     // Implementing in/outflow BCs
582     if (face_normal > 0) {
583     } else {  // inflow
584       const CeedScalar rho_inlet       = P_inlet / (R * T_inlet);
585       const CeedScalar E_kinetic_inlet = (mean_velocity[0] * mean_velocity[0] + mean_velocity[1] * mean_velocity[1]) / 2.;
586       // incoming total energy
587       const CeedScalar E_inlet = rho_inlet * (cv * T_inlet + E_kinetic_inlet);
588 
589       // The Physics
590       // -- Density
591       v[0][i] -= wdetJb * rho_inlet * face_normal;
592 
593       // -- Momentum
594       for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho_inlet * face_normal * mean_velocity[j] + norm[j] * P_inlet);
595 
596       // -- Total Energy Density
597       v[4][i] -= wdetJb * face_normal * (E_inlet + P_inlet);
598     }
599 
600   }  // End Quadrature Point Loop
601   return 0;
602 }
603 
604 // *****************************************************************************
605 // This QFunction sets the outflow boundary conditions for the Euler solver.
606 //
607 //  Outflow BCs:
608 //    The validity of the weak form of the governing equations is extended to the outflow.
609 // *****************************************************************************
610 CEED_QFUNCTION(Euler_Outflow)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
611   // Inputs
612   const CeedScalar(*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
613   const CeedScalar(*q_data_sur)    = in[2];
614 
615   // Outputs
616   CeedScalar(*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
617   EulerContext context       = (EulerContext)ctx;
618   const bool   is_implicit   = context->implicit;
619   CeedScalar  *mean_velocity = context->mean_velocity;
620 
621   const CeedScalar gamma = 1.4;
622 
623   // Quadrature Point Loop
624   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
625     // Setup
626     // -- Interp in
627     const CeedScalar rho  = q[0][i];
628     const CeedScalar u[3] = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
629     const CeedScalar E    = q[4][i];
630 
631     CeedScalar wdetJb, norm[3];
632     QdataBoundaryUnpack_3D(Q, i, q_data_sur, &wdetJb, NULL, norm);
633     wdetJb *= is_implicit ? -1. : 1.;
634 
635     // face_normal = Normal vector of the face
636     const CeedScalar face_normal = norm[0] * mean_velocity[0] + norm[1] * mean_velocity[1] + norm[2] * mean_velocity[2];
637     // The Physics
638     // Zero v so all future terms can safely sum into it
639     for (CeedInt j = 0; j < 5; j++) v[j][i] = 0;
640 
641     // Implementing in/outflow BCs
642     if (face_normal > 0) {  // outflow
643       const CeedScalar E_kinetic = (u[0] * u[0] + u[1] * u[1]) / 2.;
644       const CeedScalar P         = (E - E_kinetic * rho) * (gamma - 1.);              // pressure
645       const CeedScalar u_normal  = norm[0] * u[0] + norm[1] * u[1] + norm[2] * u[2];  // Normal velocity
646       // The Physics
647       // -- Density
648       v[0][i] -= wdetJb * rho * u_normal;
649 
650       // -- Momentum
651       for (CeedInt j = 0; j < 3; j++) v[j + 1][i] -= wdetJb * (rho * u_normal * u[j] + norm[j] * P);
652 
653       // -- Total Energy Density
654       v[4][i] -= wdetJb * u_normal * (E + P);
655     }
656   }  // End Quadrature Point Loop
657   return 0;
658 }
659 
660 // *****************************************************************************
661 
662 #endif  // eulervortex_h
663