xref: /libCEED/examples/fluids/qfunctions/shocktube.h (revision ea61e9ac44808524e4667c1525a05976f536c19c)
1*ea61e9acSJeremy L Thompson // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2*ea61e9acSJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3019b7682STimothy Aiken //
4*ea61e9acSJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5019b7682STimothy Aiken //
6*ea61e9acSJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7019b7682STimothy Aiken 
8019b7682STimothy Aiken /// @file
9*ea61e9acSJeremy L Thompson /// Shock tube initial condition and Euler equation operator for Navier-Stokes example using PETSc - modified from eulervortex.h
10019b7682STimothy Aiken 
11019b7682STimothy Aiken // Model from:
12*ea61e9acSJeremy L Thompson //   On the Order of Accuracy and Numerical Performance of Two Classes of Finite Volume WENO Schemes, Zhang, Zhang, and Shu (2011).
13019b7682STimothy Aiken 
14019b7682STimothy Aiken #ifndef shocktube_h
15019b7682STimothy Aiken #define shocktube_h
16019b7682STimothy Aiken 
17ba6664aeSJames Wright #include <ceed.h>
18c9c2c079SJeremy L Thompson #include <math.h>
192b730f8bSJeremy L Thompson 
2013fa47b2SJames Wright #include "utils.h"
21019b7682STimothy Aiken 
2297baf651SJames Wright typedef struct SetupContextShock_ *SetupContextShock;
2397baf651SJames Wright struct SetupContextShock_ {
24019b7682STimothy Aiken   CeedScalar theta0;
25019b7682STimothy Aiken   CeedScalar thetaC;
26019b7682STimothy Aiken   CeedScalar P0;
27019b7682STimothy Aiken   CeedScalar N;
28019b7682STimothy Aiken   CeedScalar cv;
29019b7682STimothy Aiken   CeedScalar cp;
30019b7682STimothy Aiken   CeedScalar time;
31019b7682STimothy Aiken   CeedScalar mid_point;
32019b7682STimothy Aiken   CeedScalar P_high;
33019b7682STimothy Aiken   CeedScalar rho_high;
34019b7682STimothy Aiken   CeedScalar P_low;
35019b7682STimothy Aiken   CeedScalar rho_low;
36019b7682STimothy Aiken   int        wind_type;               // See WindType: 0=ROTATION, 1=TRANSLATION
37019b7682STimothy Aiken   int        bubble_type;             // See BubbleType: 0=SPHERE, 1=CYLINDER
38019b7682STimothy Aiken   int        bubble_continuity_type;  // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK
39019b7682STimothy Aiken };
40019b7682STimothy Aiken 
41019b7682STimothy Aiken typedef struct ShockTubeContext_ *ShockTubeContext;
42019b7682STimothy Aiken struct ShockTubeContext_ {
43019b7682STimothy Aiken   CeedScalar Cyzb;
44019b7682STimothy Aiken   CeedScalar Byzb;
45019b7682STimothy Aiken   CeedScalar c_tau;
46019b7682STimothy Aiken   bool       implicit;
47019b7682STimothy Aiken   bool       yzb;
48019b7682STimothy Aiken   int        stabilization;
49019b7682STimothy Aiken };
50019b7682STimothy Aiken 
51019b7682STimothy Aiken // *****************************************************************************
52019b7682STimothy Aiken // This function sets the initial conditions
53019b7682STimothy Aiken //
54019b7682STimothy Aiken //   Temperature:
55019b7682STimothy Aiken //     T   = P / (rho * R)
56019b7682STimothy Aiken //   Density:
57019b7682STimothy Aiken //     rho = 1.0        if x <= mid_point
58019b7682STimothy Aiken //         = 0.125      if x >  mid_point
59019b7682STimothy Aiken //   Pressure:
60019b7682STimothy Aiken //     P   = 1.0        if x <= mid_point
61019b7682STimothy Aiken //         = 0.1        if x >  mid_point
62019b7682STimothy Aiken //   Velocity:
63019b7682STimothy Aiken //     u   = 0
64019b7682STimothy Aiken //   Velocity/Momentum Density:
65019b7682STimothy Aiken //     Ui  = rho ui
66019b7682STimothy Aiken //   Total Energy:
67019b7682STimothy Aiken //     E   = P / (gamma - 1) + rho (u u)/2
68019b7682STimothy Aiken //
69019b7682STimothy Aiken // Constants:
70019b7682STimothy Aiken //   cv              ,  Specific heat, constant volume
71019b7682STimothy Aiken //   cp              ,  Specific heat, constant pressure
72019b7682STimothy Aiken //   mid_point       ,  Location of initial domain mid_point
73019b7682STimothy Aiken //   gamma  = cp / cv,  Specific heat ratio
74019b7682STimothy Aiken //
75019b7682STimothy Aiken // *****************************************************************************
76019b7682STimothy Aiken 
77019b7682STimothy Aiken // *****************************************************************************
78*ea61e9acSJeremy L Thompson // This helper function provides support for the exact, time-dependent solution (currently not implemented) and IC formulation for Euler traveling
79*ea61e9acSJeremy L Thompson // vortex
80019b7682STimothy Aiken // *****************************************************************************
812b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedInt Exact_ShockTube(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
82019b7682STimothy Aiken   // Context
8397baf651SJames Wright   const SetupContextShock context   = (SetupContextShock)ctx;
84019b7682STimothy Aiken   const CeedScalar        mid_point = context->mid_point;  // Midpoint of the domain
85019b7682STimothy Aiken   const CeedScalar        P_high    = context->P_high;     // Driver section pressure
86019b7682STimothy Aiken   const CeedScalar        rho_high  = context->rho_high;   // Driver section density
87019b7682STimothy Aiken   const CeedScalar        P_low     = context->P_low;      // Driven section pressure
88019b7682STimothy Aiken   const CeedScalar        rho_low   = context->rho_low;    // Driven section density
89019b7682STimothy Aiken 
90019b7682STimothy Aiken   // Setup
91019b7682STimothy Aiken   const CeedScalar gamma = 1.4;   // ratio of specific heats
92019b7682STimothy Aiken   const CeedScalar x     = X[0];  // Coordinates
93019b7682STimothy Aiken 
94019b7682STimothy Aiken   CeedScalar rho, P, u[3] = {0.};
95019b7682STimothy Aiken 
96019b7682STimothy Aiken   // Initial Conditions
97019b7682STimothy Aiken   if (x <= mid_point) {
98019b7682STimothy Aiken     rho = rho_high;
99019b7682STimothy Aiken     P   = P_high;
100019b7682STimothy Aiken   } else {
101019b7682STimothy Aiken     rho = rho_low;
102019b7682STimothy Aiken     P   = P_low;
103019b7682STimothy Aiken   }
104019b7682STimothy Aiken 
105019b7682STimothy Aiken   // Assign exact solution
106019b7682STimothy Aiken   q[0] = rho;
107019b7682STimothy Aiken   q[1] = rho * u[0];
108019b7682STimothy Aiken   q[2] = rho * u[1];
109019b7682STimothy Aiken   q[3] = rho * u[2];
110019b7682STimothy Aiken   q[4] = P / (gamma - 1.0) + rho * (u[0] * u[0]) / 2.;
111019b7682STimothy Aiken 
112019b7682STimothy Aiken   // Return
113019b7682STimothy Aiken   return 0;
114019b7682STimothy Aiken }
115019b7682STimothy Aiken 
116019b7682STimothy Aiken // *****************************************************************************
117019b7682STimothy Aiken // Helper function for computing flux Jacobian
118019b7682STimothy Aiken // *****************************************************************************
1192b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ConvectiveFluxJacobian_Euler(CeedScalar dF[3][5][5], const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
120019b7682STimothy Aiken                                                         const CeedScalar gamma) {
121019b7682STimothy Aiken   CeedScalar u_sq = u[0] * u[0] + u[1] * u[1] + u[2] * u[2];  // Velocity square
122019b7682STimothy Aiken   for (CeedInt i = 0; i < 3; i++) {                           // Jacobian matrices for 3 directions
123019b7682STimothy Aiken     for (CeedInt j = 0; j < 3; j++) {                         // Rows of each Jacobian matrix
124019b7682STimothy Aiken       dF[i][j + 1][0] = ((i == j) ? ((gamma - 1.) * (u_sq / 2.)) : 0.) - u[i] * u[j];
125019b7682STimothy Aiken       for (CeedInt k = 0; k < 3; k++) {  // Columns of each Jacobian matrix
126019b7682STimothy Aiken         dF[i][0][k + 1]     = ((i == k) ? 1. : 0.);
1272b730f8bSJeremy L Thompson         dF[i][j + 1][k + 1] = ((j == k) ? u[i] : 0.) + ((i == k) ? u[j] : 0.) - ((i == j) ? u[k] : 0.) * (gamma - 1.);
1282b730f8bSJeremy L Thompson         dF[i][4][k + 1]     = ((i == k) ? (E * gamma / rho - (gamma - 1.) * u_sq / 2.) : 0.) - (gamma - 1.) * u[i] * u[k];
129019b7682STimothy Aiken       }
130019b7682STimothy Aiken       dF[i][j + 1][4] = ((i == j) ? (gamma - 1.) : 0.);
131019b7682STimothy Aiken     }
132019b7682STimothy Aiken     dF[i][4][0] = u[i] * ((gamma - 1.) * u_sq - E * gamma / rho);
133019b7682STimothy Aiken     dF[i][4][4] = u[i] * gamma;
134019b7682STimothy Aiken   }
135019b7682STimothy Aiken }
136019b7682STimothy Aiken 
137019b7682STimothy Aiken // *****************************************************************************
138*ea61e9acSJeremy L Thompson // Helper function for calculating the covariant length scale in the direction of some 3 element input vector
139019b7682STimothy Aiken //
140019b7682STimothy Aiken // Where
141019b7682STimothy Aiken //  vec         = vector that length is measured in the direction of
142019b7682STimothy Aiken //  h           = covariant element length along vec
143019b7682STimothy Aiken // *****************************************************************************
1442b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Covariant_length_along_vector(CeedScalar vec[3], const CeedScalar dXdx[3][3]) {
145019b7682STimothy Aiken   CeedScalar vec_norm            = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
146019b7682STimothy Aiken   CeedScalar vec_dot_jacobian[3] = {0.0};
147019b7682STimothy Aiken   for (CeedInt i = 0; i < 3; i++) {
148019b7682STimothy Aiken     for (CeedInt j = 0; j < 3; j++) {
149019b7682STimothy Aiken       vec_dot_jacobian[i] += dXdx[j][i] * vec[i];
150019b7682STimothy Aiken     }
151019b7682STimothy Aiken   }
1522b730f8bSJeremy L Thompson   CeedScalar norm_vec_dot_jacobian =
1532b730f8bSJeremy L Thompson       sqrt(vec_dot_jacobian[0] * vec_dot_jacobian[0] + vec_dot_jacobian[1] * vec_dot_jacobian[1] + vec_dot_jacobian[2] * vec_dot_jacobian[2]);
154019b7682STimothy Aiken   CeedScalar h = 2.0 * vec_norm / norm_vec_dot_jacobian;
155019b7682STimothy Aiken   return h;
156019b7682STimothy Aiken }
157019b7682STimothy Aiken 
158019b7682STimothy Aiken // *****************************************************************************
159019b7682STimothy Aiken // Helper function for computing Tau elements (stabilization constant)
160019b7682STimothy Aiken //   Model from:
161019b7682STimothy Aiken //     Stabilized Methods for Compressible Flows, Hughes et al 2010
162019b7682STimothy Aiken //
163019b7682STimothy Aiken //   Spatial criterion #2 - Tau is a 3x3 diagonal matrix
164019b7682STimothy Aiken //   Tau[i] = c_tau h[i] Xi(Pe) / rho(A[i]) (no sum)
165019b7682STimothy Aiken //
166019b7682STimothy Aiken // Where
167019b7682STimothy Aiken //   c_tau     = stabilization constant (0.5 is reported as "optimal")
168019b7682STimothy Aiken //   h[i]      = 2 length(dxdX[i])
169019b7682STimothy Aiken //   Pe        = Peclet number ( Pe = sqrt(u u) / dot(dXdx,u) diffusivity )
170019b7682STimothy Aiken //   Xi(Pe)    = coth Pe - 1. / Pe (1. at large local Peclet number )
171*ea61e9acSJeremy L Thompson //   rho(A[i]) = spectral radius of the convective flux Jacobian i, wave speed in direction i
172019b7682STimothy Aiken // *****************************************************************************
1732b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void Tau_spatial(CeedScalar Tau_x[3], const CeedScalar dXdx[3][3], const CeedScalar u[3], const CeedScalar sound_speed,
1742b730f8bSJeremy L Thompson                                        const CeedScalar c_tau) {
175ba6664aeSJames Wright   for (CeedInt i = 0; i < 3; i++) {
176019b7682STimothy Aiken     // length of element in direction i
1772b730f8bSJeremy L Thompson     CeedScalar h = 2 / sqrt(dXdx[0][i] * dXdx[0][i] + dXdx[1][i] * dXdx[1][i] + dXdx[2][i] * dXdx[2][i]);
178019b7682STimothy Aiken     // fastest wave in direction i
179019b7682STimothy Aiken     CeedScalar fastest_wave = fabs(u[i]) + sound_speed;
180019b7682STimothy Aiken     Tau_x[i]                = c_tau * h / fastest_wave;
181019b7682STimothy Aiken   }
182019b7682STimothy Aiken }
183019b7682STimothy Aiken 
184019b7682STimothy Aiken // *****************************************************************************
185019b7682STimothy Aiken // This QFunction sets the initial conditions for shock tube
186019b7682STimothy Aiken // *****************************************************************************
1872b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsShockTube)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
188019b7682STimothy Aiken   // Inputs
189019b7682STimothy Aiken   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
190019b7682STimothy Aiken 
191019b7682STimothy Aiken   // Outputs
192019b7682STimothy Aiken   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
193019b7682STimothy Aiken 
194019b7682STimothy Aiken   CeedPragmaSIMD
195019b7682STimothy Aiken       // Quadrature Point Loop
196019b7682STimothy Aiken       for (CeedInt i = 0; i < Q; i++) {
197019b7682STimothy Aiken     const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]};
198019b7682STimothy Aiken     CeedScalar       q[5];
199019b7682STimothy Aiken 
200019b7682STimothy Aiken     Exact_ShockTube(3, 0., x, 5, q, ctx);
201019b7682STimothy Aiken 
2022b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
203019b7682STimothy Aiken   }  // End of Quadrature Point Loop
204019b7682STimothy Aiken 
205019b7682STimothy Aiken   // Return
206019b7682STimothy Aiken   return 0;
207019b7682STimothy Aiken }
208019b7682STimothy Aiken 
209019b7682STimothy Aiken // *****************************************************************************
210*ea61e9acSJeremy L Thompson // This QFunction implements the following formulation of Euler equations with explicit time stepping method
211019b7682STimothy Aiken //
212*ea61e9acSJeremy L Thompson // This is 3D Euler for compressible gas dynamics in conservation form with state variables of density, momentum density, and total energy density.
213019b7682STimothy Aiken //
214019b7682STimothy Aiken // State Variables: q = ( rho, U1, U2, U3, E )
215019b7682STimothy Aiken //   rho - Mass Density
216019b7682STimothy Aiken //   Ui  - Momentum Density,      Ui = rho ui
217019b7682STimothy Aiken //   E   - Total Energy Density,  E  = P / (gamma - 1) + rho (u u)/2
218019b7682STimothy Aiken //
219019b7682STimothy Aiken // Euler Equations:
220019b7682STimothy Aiken //   drho/dt + div( U )                   = 0
221019b7682STimothy Aiken //   dU/dt   + div( rho (u x u) + P I3 )  = 0
222019b7682STimothy Aiken //   dE/dt   + div( (E + P) u )           = 0
223019b7682STimothy Aiken //
224019b7682STimothy Aiken // Equation of State:
225019b7682STimothy Aiken //   P = (gamma - 1) (E - rho (u u) / 2)
226019b7682STimothy Aiken //
227019b7682STimothy Aiken // Constants:
228019b7682STimothy Aiken //   cv              ,  Specific heat, constant volume
229019b7682STimothy Aiken //   cp              ,  Specific heat, constant pressure
230019b7682STimothy Aiken //   g               ,  Gravity
231019b7682STimothy Aiken //   gamma  = cp / cv,  Specific heat ratio
232019b7682STimothy Aiken // *****************************************************************************
2332b730f8bSJeremy L Thompson CEED_QFUNCTION(EulerShockTube)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
234019b7682STimothy Aiken   // Inputs
23546603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
23646603fc5SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
23746603fc5SJames Wright   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
23846603fc5SJames Wright 
239019b7682STimothy Aiken   // Outputs
24046603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
24146603fc5SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
242019b7682STimothy Aiken 
243019b7682STimothy Aiken   const CeedScalar gamma = 1.4;
244019b7682STimothy Aiken 
245019b7682STimothy Aiken   ShockTubeContext context = (ShockTubeContext)ctx;
246019b7682STimothy Aiken   const CeedScalar Cyzb    = context->Cyzb;
247019b7682STimothy Aiken   const CeedScalar Byzb    = context->Byzb;
248019b7682STimothy Aiken   const CeedScalar c_tau   = context->c_tau;
249019b7682STimothy Aiken 
250019b7682STimothy Aiken   CeedPragmaSIMD
251019b7682STimothy Aiken       // Quadrature Point Loop
252019b7682STimothy Aiken       for (CeedInt i = 0; i < Q; i++) {
253019b7682STimothy Aiken     // Setup
254019b7682STimothy Aiken     // -- Interp in
255019b7682STimothy Aiken     const CeedScalar rho      = q[0][i];
2562b730f8bSJeremy L Thompson     const CeedScalar u[3]     = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
257019b7682STimothy Aiken     const CeedScalar E        = q[4][i];
2582b730f8bSJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
2592b730f8bSJeremy L Thompson     const CeedScalar dU[3][3] = {
2602b730f8bSJeremy L Thompson         {dq[0][1][i], dq[1][1][i], dq[2][1][i]},
2612b730f8bSJeremy L Thompson         {dq[0][2][i], dq[1][2][i], dq[2][2][i]},
2622b730f8bSJeremy L Thompson         {dq[0][3][i], dq[1][3][i], dq[2][3][i]}
263019b7682STimothy Aiken     };
2642b730f8bSJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
265019b7682STimothy Aiken     // -- Interp-to-Interp q_data
266019b7682STimothy Aiken     const CeedScalar wdetJ = q_data[0][i];
267019b7682STimothy Aiken     // -- Interp-to-Grad q_data
268019b7682STimothy Aiken     // ---- Inverse of change of coordinate matrix: X_i,j
2692b730f8bSJeremy L Thompson     const CeedScalar dXdx[3][3] = {
2702b730f8bSJeremy L Thompson         {q_data[1][i], q_data[2][i], q_data[3][i]},
2712b730f8bSJeremy L Thompson         {q_data[4][i], q_data[5][i], q_data[6][i]},
2722b730f8bSJeremy L Thompson         {q_data[7][i], q_data[8][i], q_data[9][i]}
273019b7682STimothy Aiken     };
274019b7682STimothy Aiken     // dU/dx
275019b7682STimothy Aiken     CeedScalar du[3][3]        = {{0}};
276019b7682STimothy Aiken     CeedScalar drhodx[3]       = {0};
277019b7682STimothy Aiken     CeedScalar dEdx[3]         = {0};
278019b7682STimothy Aiken     CeedScalar dUdx[3][3]      = {{0}};
279019b7682STimothy Aiken     CeedScalar dXdxdXdxT[3][3] = {{0}};
280ba6664aeSJames Wright     for (CeedInt j = 0; j < 3; j++) {
281ba6664aeSJames Wright       for (CeedInt k = 0; k < 3; k++) {
282019b7682STimothy Aiken         du[j][k] = (dU[j][k] - drho[k] * u[j]) / rho;
283019b7682STimothy Aiken         drhodx[j] += drho[k] * dXdx[k][j];
284019b7682STimothy Aiken         dEdx[j] += dE[k] * dXdx[k][j];
285ba6664aeSJames Wright         for (CeedInt l = 0; l < 3; l++) {
286019b7682STimothy Aiken           dUdx[j][k] += dU[j][l] * dXdx[l][k];
287019b7682STimothy Aiken           dXdxdXdxT[j][k] += dXdx[j][l] * dXdx[k][l];  // dXdx_j,k * dXdx_k,j
288019b7682STimothy Aiken         }
289019b7682STimothy Aiken       }
290019b7682STimothy Aiken     }
291019b7682STimothy Aiken 
2922b730f8bSJeremy L Thompson     const CeedScalar E_kinetic = 0.5 * rho * (u[0] * u[0] + u[1] * u[1] + u[2] * u[2]), E_internal = E - E_kinetic,
293019b7682STimothy Aiken                      P = E_internal * (gamma - 1);  // P = pressure
294019b7682STimothy Aiken 
295019b7682STimothy Aiken     // The Physics
296019b7682STimothy Aiken     // Zero v and dv so all future terms can safely sum into it
297ba6664aeSJames Wright     for (CeedInt j = 0; j < 5; j++) {
298019b7682STimothy Aiken       v[j][i] = 0;
2992b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) dv[k][j][i] = 0;
300019b7682STimothy Aiken     }
301019b7682STimothy Aiken 
302019b7682STimothy Aiken     // -- Density
303019b7682STimothy Aiken     // ---- u rho
3042b730f8bSJeremy L Thompson     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]);
305019b7682STimothy Aiken     // -- Momentum
306019b7682STimothy Aiken     // ---- rho (u x u) + P I3
3072b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
3082b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) {
3092b730f8bSJeremy L Thompson         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] +
310019b7682STimothy Aiken                                     (rho * u[j] * u[2] + (j == 2 ? P : 0)) * dXdx[k][2]);
3112b730f8bSJeremy L Thompson       }
3122b730f8bSJeremy L Thompson     }
313019b7682STimothy Aiken     // -- Total Energy Density
314019b7682STimothy Aiken     // ---- (E + P) u
3152b730f8bSJeremy L Thompson     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]);
316019b7682STimothy Aiken 
317019b7682STimothy Aiken     // -- YZB stabilization
318019b7682STimothy Aiken     if (context->yzb) {
319019b7682STimothy Aiken       CeedScalar drho_norm    = 0.0;    // magnitude of the density gradient
320019b7682STimothy Aiken       CeedScalar j_vec[3]     = {0.0};  // unit vector aligned with the density gradient
321019b7682STimothy Aiken       CeedScalar h_shock      = 0.0;    // element lengthscale
322019b7682STimothy Aiken       CeedScalar acoustic_vel = 0.0;    // characteristic velocity, acoustic speed
323019b7682STimothy Aiken       CeedScalar tau_shock    = 0.0;    // timescale
324019b7682STimothy Aiken       CeedScalar nu_shock     = 0.0;    // artificial diffusion
325019b7682STimothy Aiken 
326019b7682STimothy Aiken       // Unit vector aligned with the density gradient
3272b730f8bSJeremy L Thompson       drho_norm = sqrt(drhodx[0] * drhodx[0] + drhodx[1] * drhodx[1] + drhodx[2] * drhodx[2]);
3282b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) j_vec[j] = drhodx[j] / (drho_norm + 1e-20);
329019b7682STimothy Aiken 
330019b7682STimothy Aiken       if (drho_norm == 0.0) {
331019b7682STimothy Aiken         nu_shock = 0.0;
332019b7682STimothy Aiken       } else {
333019b7682STimothy Aiken         h_shock = Covariant_length_along_vector(j_vec, dXdx);
334019b7682STimothy Aiken         h_shock /= Cyzb;
335019b7682STimothy Aiken         acoustic_vel = sqrt(gamma * P / rho);
336019b7682STimothy Aiken         tau_shock    = h_shock / (2 * acoustic_vel) * pow(drho_norm * h_shock / rho, Byzb);
337019b7682STimothy Aiken         nu_shock     = fabs(tau_shock * acoustic_vel * acoustic_vel);
338019b7682STimothy Aiken       }
339019b7682STimothy Aiken 
3402b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][0][i] -= wdetJ * nu_shock * drhodx[j];
341019b7682STimothy Aiken 
3422b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) {
3432b730f8bSJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) dv[j][k][i] -= wdetJ * nu_shock * du[k][j];
3442b730f8bSJeremy L Thompson       }
345019b7682STimothy Aiken 
3462b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * nu_shock * dEdx[j];
347019b7682STimothy Aiken     }
348019b7682STimothy Aiken 
349019b7682STimothy Aiken     // Stabilization
350019b7682STimothy Aiken     // Need the Jacobian for the advective fluxes for stabilization
351019b7682STimothy Aiken     //    indexed as: jacob_F_conv[direction][flux component][solution component]
352019b7682STimothy Aiken     CeedScalar jacob_F_conv[3][5][5] = {{{0.}}};
353019b7682STimothy Aiken     ConvectiveFluxJacobian_Euler(jacob_F_conv, rho, u, E, gamma);
354019b7682STimothy Aiken 
355019b7682STimothy Aiken     // dqdx collects drhodx, dUdx and dEdx in one vector
356019b7682STimothy Aiken     CeedScalar dqdx[5][3];
357ba6664aeSJames Wright     for (CeedInt j = 0; j < 3; j++) {
358019b7682STimothy Aiken       dqdx[0][j] = drhodx[j];
359019b7682STimothy Aiken       dqdx[4][j] = dEdx[j];
3602b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) dqdx[k + 1][j] = dUdx[k][j];
361019b7682STimothy Aiken     }
362019b7682STimothy Aiken 
363019b7682STimothy Aiken     // strong_conv = dF/dq * dq/dx    (Strong convection)
364019b7682STimothy Aiken     CeedScalar strong_conv[5] = {0};
3652b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
3662b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 5; k++) {
3672b730f8bSJeremy L Thompson         for (CeedInt l = 0; l < 5; l++) strong_conv[k] += jacob_F_conv[j][k][l] * dqdx[l][j];
3682b730f8bSJeremy L Thompson       }
3692b730f8bSJeremy L Thompson     }
370019b7682STimothy Aiken 
371019b7682STimothy Aiken     // Stabilization
372019b7682STimothy Aiken     // -- Tau elements
373019b7682STimothy Aiken     const CeedScalar sound_speed = sqrt(gamma * P / rho);
374019b7682STimothy Aiken     CeedScalar       Tau_x[3]    = {0.};
375019b7682STimothy Aiken     Tau_spatial(Tau_x, dXdx, u, sound_speed, c_tau);
376019b7682STimothy Aiken 
377019b7682STimothy Aiken     CeedScalar stab[5][3] = {0};
378019b7682STimothy Aiken     switch (context->stabilization) {
379019b7682STimothy Aiken       case 0:  // Galerkin
380019b7682STimothy Aiken         break;
381019b7682STimothy Aiken       case 1:  // SU
3822b730f8bSJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) {
3832b730f8bSJeremy L Thompson           for (CeedInt k = 0; k < 5; k++) {
384ba6664aeSJames Wright             for (CeedInt l = 0; l < 5; l++) {
385019b7682STimothy Aiken               stab[k][j] += jacob_F_conv[j][k][l] * Tau_x[j] * strong_conv[l];
386019b7682STimothy Aiken             }
3872b730f8bSJeremy L Thompson           }
3882b730f8bSJeremy L Thompson         }
3892b730f8bSJeremy L Thompson         for (CeedInt j = 0; j < 5; j++) {
3902b730f8bSJeremy L Thompson           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]);
3912b730f8bSJeremy L Thompson         }
392019b7682STimothy Aiken         break;
393019b7682STimothy Aiken     }
394019b7682STimothy Aiken 
395019b7682STimothy Aiken   }  // End Quadrature Point Loop
396019b7682STimothy Aiken 
397019b7682STimothy Aiken   // Return
398019b7682STimothy Aiken   return 0;
399019b7682STimothy Aiken }
400019b7682STimothy Aiken 
401019b7682STimothy Aiken #endif  // shocktube_h
402