xref: /honee/qfunctions/shocktube.h (revision 0d0968c28710ac8ae311048b492b03f8a4ab28eb)
1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3af8870a9STimothy Aiken 
4af8870a9STimothy Aiken /// @file
5*ea615d4cSJames Wright /// Shock tube initial condition and Euler equation operator for HONEE - modified from eulervortex.h
6af8870a9STimothy Aiken 
7af8870a9STimothy Aiken // Model from:
804e40bb6SJeremy L Thompson //   On the Order of Accuracy and Numerical Performance of Two Classes of Finite Volume WENO Schemes, Zhang, Zhang, and Shu (2011).
93e17a7a1SJames Wright #include <ceed/types.h>
103e17a7a1SJames Wright #ifndef CEED_RUNNING_JIT_PASS
113e17a7a1SJames Wright #include <stdbool.h>
123e17a7a1SJames Wright #endif
132b916ea7SJeremy L Thompson 
14704b8bbeSJames Wright #include "utils.h"
15af8870a9STimothy Aiken 
163636f6a4SJames Wright typedef struct SetupContextShock_ *SetupContextShock;
173636f6a4SJames Wright struct SetupContextShock_ {
18af8870a9STimothy Aiken   CeedScalar theta0;
19af8870a9STimothy Aiken   CeedScalar thetaC;
20af8870a9STimothy Aiken   CeedScalar P0;
21af8870a9STimothy Aiken   CeedScalar N;
22af8870a9STimothy Aiken   CeedScalar cv;
23af8870a9STimothy Aiken   CeedScalar cp;
24af8870a9STimothy Aiken   CeedScalar time;
25af8870a9STimothy Aiken   CeedScalar mid_point;
26af8870a9STimothy Aiken   CeedScalar P_high;
27af8870a9STimothy Aiken   CeedScalar rho_high;
28af8870a9STimothy Aiken   CeedScalar P_low;
29af8870a9STimothy Aiken   CeedScalar rho_low;
30af8870a9STimothy Aiken };
31af8870a9STimothy Aiken 
32af8870a9STimothy Aiken typedef struct ShockTubeContext_ *ShockTubeContext;
33af8870a9STimothy Aiken struct ShockTubeContext_ {
34af8870a9STimothy Aiken   CeedScalar Cyzb;
35af8870a9STimothy Aiken   CeedScalar Byzb;
36af8870a9STimothy Aiken   CeedScalar c_tau;
37af8870a9STimothy Aiken   bool       implicit;
38af8870a9STimothy Aiken   bool       yzb;
39af8870a9STimothy Aiken   int        stabilization;
40af8870a9STimothy Aiken };
41af8870a9STimothy Aiken 
42af8870a9STimothy Aiken // *****************************************************************************
43af8870a9STimothy Aiken // This function sets the initial conditions
44af8870a9STimothy Aiken //
45af8870a9STimothy Aiken //   Temperature:
46af8870a9STimothy Aiken //     T   = P / (rho * R)
47af8870a9STimothy Aiken //   Density:
48af8870a9STimothy Aiken //     rho = 1.0        if x <= mid_point
49af8870a9STimothy Aiken //         = 0.125      if x >  mid_point
50af8870a9STimothy Aiken //   Pressure:
51af8870a9STimothy Aiken //     P   = 1.0        if x <= mid_point
52af8870a9STimothy Aiken //         = 0.1        if x >  mid_point
53af8870a9STimothy Aiken //   Velocity:
54af8870a9STimothy Aiken //     u   = 0
55af8870a9STimothy Aiken //   Velocity/Momentum Density:
56af8870a9STimothy Aiken //     Ui  = rho ui
57af8870a9STimothy Aiken //   Total Energy:
58af8870a9STimothy Aiken //     E   = P / (gamma - 1) + rho (u u)/2
59af8870a9STimothy Aiken //
60af8870a9STimothy Aiken // Constants:
61af8870a9STimothy Aiken //   cv              ,  Specific heat, constant volume
62af8870a9STimothy Aiken //   cp              ,  Specific heat, constant pressure
63af8870a9STimothy Aiken //   mid_point       ,  Location of initial domain mid_point
64af8870a9STimothy Aiken //   gamma  = cp / cv,  Specific heat ratio
65af8870a9STimothy Aiken //
66af8870a9STimothy Aiken // *****************************************************************************
67af8870a9STimothy Aiken 
68af8870a9STimothy Aiken // *****************************************************************************
6904e40bb6SJeremy L Thompson // This helper function provides support for the exact, time-dependent solution (currently not implemented) and IC formulation for Euler traveling
7004e40bb6SJeremy L Thompson // vortex
71af8870a9STimothy Aiken // *****************************************************************************
Exact_ShockTube(CeedInt dim,CeedScalar time,const CeedScalar X[],CeedInt Nf,CeedScalar q[],void * ctx)722b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedInt Exact_ShockTube(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
73af8870a9STimothy Aiken   // Context
743636f6a4SJames Wright   const SetupContextShock context   = (SetupContextShock)ctx;
75af8870a9STimothy Aiken   const CeedScalar        mid_point = context->mid_point;  // Midpoint of the domain
76af8870a9STimothy Aiken   const CeedScalar        P_high    = context->P_high;     // Driver section pressure
77af8870a9STimothy Aiken   const CeedScalar        rho_high  = context->rho_high;   // Driver section density
78af8870a9STimothy Aiken   const CeedScalar        P_low     = context->P_low;      // Driven section pressure
79af8870a9STimothy Aiken   const CeedScalar        rho_low   = context->rho_low;    // Driven section density
80af8870a9STimothy Aiken 
81af8870a9STimothy Aiken   // Setup
82af8870a9STimothy Aiken   const CeedScalar gamma = 1.4;   // ratio of specific heats
83af8870a9STimothy Aiken   const CeedScalar x     = X[0];  // Coordinates
84af8870a9STimothy Aiken 
85af8870a9STimothy Aiken   CeedScalar rho, P, u[3] = {0.};
86af8870a9STimothy Aiken 
87af8870a9STimothy Aiken   // Initial Conditions
8867263decSKenneth E. Jansen   if (x <= mid_point + 200 * CEED_EPSILON) {
89af8870a9STimothy Aiken     rho = rho_high;
90af8870a9STimothy Aiken     P   = P_high;
91af8870a9STimothy Aiken   } else {
92af8870a9STimothy Aiken     rho = rho_low;
93af8870a9STimothy Aiken     P   = P_low;
94af8870a9STimothy Aiken   }
95af8870a9STimothy Aiken 
96af8870a9STimothy Aiken   // Assign exact solution
97af8870a9STimothy Aiken   q[0] = rho;
98af8870a9STimothy Aiken   q[1] = rho * u[0];
99af8870a9STimothy Aiken   q[2] = rho * u[1];
100af8870a9STimothy Aiken   q[3] = rho * u[2];
101af8870a9STimothy Aiken   q[4] = P / (gamma - 1.0) + rho * (u[0] * u[0]) / 2.;
102af8870a9STimothy Aiken 
103af8870a9STimothy Aiken   return 0;
104af8870a9STimothy Aiken }
105af8870a9STimothy Aiken 
106af8870a9STimothy Aiken // *****************************************************************************
107af8870a9STimothy Aiken // Helper function for computing flux Jacobian
108af8870a9STimothy Aiken // *****************************************************************************
ConvectiveFluxJacobian_Euler(CeedScalar dF[3][5][5],const CeedScalar rho,const CeedScalar u[3],const CeedScalar E,const CeedScalar gamma)1092b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void ConvectiveFluxJacobian_Euler(CeedScalar dF[3][5][5], const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
110af8870a9STimothy Aiken                                                         const CeedScalar gamma) {
111af8870a9STimothy Aiken   CeedScalar u_sq = u[0] * u[0] + u[1] * u[1] + u[2] * u[2];  // Velocity square
112af8870a9STimothy Aiken   for (CeedInt i = 0; i < 3; i++) {                           // Jacobian matrices for 3 directions
113af8870a9STimothy Aiken     for (CeedInt j = 0; j < 3; j++) {                         // Rows of each Jacobian matrix
114af8870a9STimothy Aiken       dF[i][j + 1][0] = ((i == j) ? ((gamma - 1.) * (u_sq / 2.)) : 0.) - u[i] * u[j];
115af8870a9STimothy Aiken       for (CeedInt k = 0; k < 3; k++) {  // Columns of each Jacobian matrix
116af8870a9STimothy Aiken         dF[i][0][k + 1]     = ((i == k) ? 1. : 0.);
1172b916ea7SJeremy 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.);
1182b916ea7SJeremy L Thompson         dF[i][4][k + 1]     = ((i == k) ? (E * gamma / rho - (gamma - 1.) * u_sq / 2.) : 0.) - (gamma - 1.) * u[i] * u[k];
119af8870a9STimothy Aiken       }
120af8870a9STimothy Aiken       dF[i][j + 1][4] = ((i == j) ? (gamma - 1.) : 0.);
121af8870a9STimothy Aiken     }
122af8870a9STimothy Aiken     dF[i][4][0] = u[i] * ((gamma - 1.) * u_sq - E * gamma / rho);
123af8870a9STimothy Aiken     dF[i][4][4] = u[i] * gamma;
124af8870a9STimothy Aiken   }
125af8870a9STimothy Aiken }
126af8870a9STimothy Aiken 
127af8870a9STimothy Aiken // *****************************************************************************
12804e40bb6SJeremy L Thompson // Helper function for calculating the covariant length scale in the direction of some 3 element input vector
129af8870a9STimothy Aiken //
130af8870a9STimothy Aiken // Where
131af8870a9STimothy Aiken //  vec         = vector that length is measured in the direction of
132af8870a9STimothy Aiken //  h           = covariant element length along vec
133af8870a9STimothy Aiken // *****************************************************************************
Covariant_length_along_vector(CeedScalar vec[3],const CeedScalar dXdx[3][3])1342b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Covariant_length_along_vector(CeedScalar vec[3], const CeedScalar dXdx[3][3]) {
135af8870a9STimothy Aiken   CeedScalar vec_dot_jacobian[3] = {0.0};
13678e8b7daSJames Wright 
13778e8b7daSJames Wright   MatVec3(dXdx, vec, CEED_TRANSPOSE, vec_dot_jacobian);
13878e8b7daSJames Wright   return 2.0 * Norm3(vec) / Norm3(vec_dot_jacobian);
139af8870a9STimothy Aiken }
140af8870a9STimothy Aiken 
141af8870a9STimothy Aiken // *****************************************************************************
142af8870a9STimothy Aiken // Helper function for computing Tau elements (stabilization constant)
143af8870a9STimothy Aiken //   Model from:
144af8870a9STimothy Aiken //     Stabilized Methods for Compressible Flows, Hughes et al 2010
145af8870a9STimothy Aiken //
146af8870a9STimothy Aiken //   Spatial criterion #2 - Tau is a 3x3 diagonal matrix
147af8870a9STimothy Aiken //   Tau[i] = c_tau h[i] Xi(Pe) / rho(A[i]) (no sum)
148af8870a9STimothy Aiken //
149af8870a9STimothy Aiken // Where
150af8870a9STimothy Aiken //   c_tau     = stabilization constant (0.5 is reported as "optimal")
151af8870a9STimothy Aiken //   h[i]      = 2 length(dxdX[i])
152af8870a9STimothy Aiken //   Pe        = Peclet number ( Pe = sqrt(u u) / dot(dXdx,u) diffusivity )
153af8870a9STimothy Aiken //   Xi(Pe)    = coth Pe - 1. / Pe (1. at large local Peclet number )
15404e40bb6SJeremy L Thompson //   rho(A[i]) = spectral radius of the convective flux Jacobian i, wave speed in direction i
155af8870a9STimothy Aiken // *****************************************************************************
Tau_spatial(CeedScalar Tau_x[3],const CeedScalar dXdx[3][3],const CeedScalar u[3],const CeedScalar sound_speed,const CeedScalar c_tau)1562b916ea7SJeremy 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,
1572b916ea7SJeremy L Thompson                                        const CeedScalar c_tau) {
158493642f1SJames Wright   for (CeedInt i = 0; i < 3; i++) {
159af8870a9STimothy Aiken     // length of element in direction i
16078e8b7daSJames Wright     CeedScalar h = 2 / sqrt(Square(dXdx[0][i]) + Square(dXdx[1][i]) + Square(dXdx[2][i]));
161af8870a9STimothy Aiken     // fastest wave in direction i
162af8870a9STimothy Aiken     CeedScalar fastest_wave = fabs(u[i]) + sound_speed;
163af8870a9STimothy Aiken     Tau_x[i]                = c_tau * h / fastest_wave;
164af8870a9STimothy Aiken   }
165af8870a9STimothy Aiken }
166af8870a9STimothy Aiken 
167af8870a9STimothy Aiken // *****************************************************************************
168af8870a9STimothy Aiken // This QFunction sets the initial conditions for shock tube
169af8870a9STimothy Aiken // *****************************************************************************
ICsShockTube(void * ctx,CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)1702b916ea7SJeremy L Thompson CEED_QFUNCTION(ICsShockTube)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
171af8870a9STimothy Aiken   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
172af8870a9STimothy Aiken   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
173af8870a9STimothy Aiken 
174b193fadcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
175af8870a9STimothy Aiken     const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]};
176af8870a9STimothy Aiken     CeedScalar       q[5];
177af8870a9STimothy Aiken 
178af8870a9STimothy Aiken     Exact_ShockTube(3, 0., x, 5, q, ctx);
179af8870a9STimothy Aiken 
1802b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
181b193fadcSJames Wright   }
182af8870a9STimothy Aiken   return 0;
183af8870a9STimothy Aiken }
184af8870a9STimothy Aiken 
185af8870a9STimothy Aiken // *****************************************************************************
18604e40bb6SJeremy L Thompson // This QFunction implements the following formulation of Euler equations with explicit time stepping method
187af8870a9STimothy Aiken //
18804e40bb6SJeremy L Thompson // This is 3D Euler for compressible gas dynamics in conservation form with state variables of density, momentum density, and total energy density.
189af8870a9STimothy Aiken //
190af8870a9STimothy Aiken // State Variables: q = ( rho, U1, U2, U3, E )
191af8870a9STimothy Aiken //   rho - Mass Density
192af8870a9STimothy Aiken //   Ui  - Momentum Density,      Ui = rho ui
193af8870a9STimothy Aiken //   E   - Total Energy Density,  E  = P / (gamma - 1) + rho (u u)/2
194af8870a9STimothy Aiken //
195af8870a9STimothy Aiken // Euler Equations:
196af8870a9STimothy Aiken //   drho/dt + div( U )                   = 0
197af8870a9STimothy Aiken //   dU/dt   + div( rho (u x u) + P I3 )  = 0
198af8870a9STimothy Aiken //   dE/dt   + div( (E + P) u )           = 0
199af8870a9STimothy Aiken //
200af8870a9STimothy Aiken // Equation of State:
201af8870a9STimothy Aiken //   P = (gamma - 1) (E - rho (u u) / 2)
202af8870a9STimothy Aiken //
203af8870a9STimothy Aiken // Constants:
204af8870a9STimothy Aiken //   cv              ,  Specific heat, constant volume
205af8870a9STimothy Aiken //   cp              ,  Specific heat, constant pressure
206af8870a9STimothy Aiken //   g               ,  Gravity
207af8870a9STimothy Aiken //   gamma  = cp / cv,  Specific heat ratio
208af8870a9STimothy Aiken // *****************************************************************************
EulerShockTube(void * ctx,CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)2092b916ea7SJeremy L Thompson CEED_QFUNCTION(EulerShockTube)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
2103d65b166SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
2113d65b166SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
212ade49511SJames Wright   const CeedScalar(*q_data)            = in[2];
2133d65b166SJames Wright   CeedScalar(*v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
2143d65b166SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA]       = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
215af8870a9STimothy Aiken 
216af8870a9STimothy Aiken   const CeedScalar gamma = 1.4;
217af8870a9STimothy Aiken 
218af8870a9STimothy Aiken   ShockTubeContext context = (ShockTubeContext)ctx;
219af8870a9STimothy Aiken   const CeedScalar Cyzb    = context->Cyzb;
220af8870a9STimothy Aiken   const CeedScalar Byzb    = context->Byzb;
221af8870a9STimothy Aiken   const CeedScalar c_tau   = context->c_tau;
222af8870a9STimothy Aiken 
223b193fadcSJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
224af8870a9STimothy Aiken     // Setup
225af8870a9STimothy Aiken     // -- Interp in
226af8870a9STimothy Aiken     const CeedScalar rho      = q[0][i];
2272b916ea7SJeremy L Thompson     const CeedScalar u[3]     = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
228af8870a9STimothy Aiken     const CeedScalar E        = q[4][i];
2292b916ea7SJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
2302b916ea7SJeremy L Thompson     const CeedScalar dU[3][3] = {
2312b916ea7SJeremy L Thompson         {dq[0][1][i], dq[1][1][i], dq[2][1][i]},
2322b916ea7SJeremy L Thompson         {dq[0][2][i], dq[1][2][i], dq[2][2][i]},
2332b916ea7SJeremy L Thompson         {dq[0][3][i], dq[1][3][i], dq[2][3][i]}
234af8870a9STimothy Aiken     };
2352b916ea7SJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
236ade49511SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
237ade49511SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
238af8870a9STimothy Aiken     // dU/dx
239af8870a9STimothy Aiken     CeedScalar du[3][3]        = {{0}};
240af8870a9STimothy Aiken     CeedScalar drhodx[3]       = {0};
241af8870a9STimothy Aiken     CeedScalar dEdx[3]         = {0};
242af8870a9STimothy Aiken     CeedScalar dUdx[3][3]      = {{0}};
243af8870a9STimothy Aiken     CeedScalar dXdxdXdxT[3][3] = {{0}};
244493642f1SJames Wright     for (CeedInt j = 0; j < 3; j++) {
245493642f1SJames Wright       for (CeedInt k = 0; k < 3; k++) {
246af8870a9STimothy Aiken         du[j][k] = (dU[j][k] - drho[k] * u[j]) / rho;
247af8870a9STimothy Aiken         drhodx[j] += drho[k] * dXdx[k][j];
248af8870a9STimothy Aiken         dEdx[j] += dE[k] * dXdx[k][j];
249493642f1SJames Wright         for (CeedInt l = 0; l < 3; l++) {
250af8870a9STimothy Aiken           dUdx[j][k] += dU[j][l] * dXdx[l][k];
251af8870a9STimothy Aiken           dXdxdXdxT[j][k] += dXdx[j][l] * dXdx[k][l];  // dXdx_j,k * dXdx_k,j
252af8870a9STimothy Aiken         }
253af8870a9STimothy Aiken       }
254af8870a9STimothy Aiken     }
255af8870a9STimothy Aiken 
2562b916ea7SJeremy 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,
257af8870a9STimothy Aiken                      P = E_internal * (gamma - 1);  // P = pressure
258af8870a9STimothy Aiken 
259af8870a9STimothy Aiken     // The Physics
260af8870a9STimothy Aiken     // Zero v and dv so all future terms can safely sum into it
261493642f1SJames Wright     for (CeedInt j = 0; j < 5; j++) {
262af8870a9STimothy Aiken       v[j][i] = 0;
2632b916ea7SJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) dv[k][j][i] = 0;
264af8870a9STimothy Aiken     }
265af8870a9STimothy Aiken 
266af8870a9STimothy Aiken     // -- Density
267af8870a9STimothy Aiken     // ---- u rho
2682b916ea7SJeremy 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]);
269af8870a9STimothy Aiken     // -- Momentum
270af8870a9STimothy Aiken     // ---- rho (u x u) + P I3
2712b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
2722b916ea7SJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) {
2732b916ea7SJeremy 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] +
274af8870a9STimothy Aiken                                     (rho * u[j] * u[2] + (j == 2 ? P : 0)) * dXdx[k][2]);
2752b916ea7SJeremy L Thompson       }
2762b916ea7SJeremy L Thompson     }
277af8870a9STimothy Aiken     // -- Total Energy Density
278af8870a9STimothy Aiken     // ---- (E + P) u
2792b916ea7SJeremy 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]);
280af8870a9STimothy Aiken 
281af8870a9STimothy Aiken     // -- YZB stabilization
282af8870a9STimothy Aiken     if (context->yzb) {
283af8870a9STimothy Aiken       CeedScalar drho_norm    = 0.0;    // magnitude of the density gradient
284af8870a9STimothy Aiken       CeedScalar j_vec[3]     = {0.0};  // unit vector aligned with the density gradient
285af8870a9STimothy Aiken       CeedScalar h_shock      = 0.0;    // element lengthscale
286af8870a9STimothy Aiken       CeedScalar acoustic_vel = 0.0;    // characteristic velocity, acoustic speed
287af8870a9STimothy Aiken       CeedScalar tau_shock    = 0.0;    // timescale
288af8870a9STimothy Aiken       CeedScalar nu_shock     = 0.0;    // artificial diffusion
289af8870a9STimothy Aiken 
290af8870a9STimothy Aiken       // Unit vector aligned with the density gradient
29178e8b7daSJames Wright       drho_norm = Norm3(drhodx);
2922b916ea7SJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) j_vec[j] = drhodx[j] / (drho_norm + 1e-20);
293af8870a9STimothy Aiken 
294af8870a9STimothy Aiken       if (drho_norm == 0.0) {
295af8870a9STimothy Aiken         nu_shock = 0.0;
296af8870a9STimothy Aiken       } else {
297af8870a9STimothy Aiken         h_shock = Covariant_length_along_vector(j_vec, dXdx);
298af8870a9STimothy Aiken         h_shock /= Cyzb;
299af8870a9STimothy Aiken         acoustic_vel = sqrt(gamma * P / rho);
300af8870a9STimothy Aiken         tau_shock    = h_shock / (2 * acoustic_vel) * pow(drho_norm * h_shock / rho, Byzb);
301af8870a9STimothy Aiken         nu_shock     = fabs(tau_shock * acoustic_vel * acoustic_vel);
302af8870a9STimothy Aiken       }
303af8870a9STimothy Aiken 
3042b916ea7SJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][0][i] -= wdetJ * nu_shock * drhodx[j];
305af8870a9STimothy Aiken 
3062b916ea7SJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) {
3072b916ea7SJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) dv[j][k][i] -= wdetJ * nu_shock * du[k][j];
3082b916ea7SJeremy L Thompson       }
309af8870a9STimothy Aiken 
3102b916ea7SJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * nu_shock * dEdx[j];
311af8870a9STimothy Aiken     }
312af8870a9STimothy Aiken 
313af8870a9STimothy Aiken     // Stabilization
314af8870a9STimothy Aiken     // Need the Jacobian for the advective fluxes for stabilization
315af8870a9STimothy Aiken     //    indexed as: jacob_F_conv[direction][flux component][solution component]
316af8870a9STimothy Aiken     CeedScalar jacob_F_conv[3][5][5] = {{{0.}}};
317af8870a9STimothy Aiken     ConvectiveFluxJacobian_Euler(jacob_F_conv, rho, u, E, gamma);
318af8870a9STimothy Aiken 
319af8870a9STimothy Aiken     // dqdx collects drhodx, dUdx and dEdx in one vector
320af8870a9STimothy Aiken     CeedScalar dqdx[5][3];
321493642f1SJames Wright     for (CeedInt j = 0; j < 3; j++) {
322af8870a9STimothy Aiken       dqdx[0][j] = drhodx[j];
323af8870a9STimothy Aiken       dqdx[4][j] = dEdx[j];
3242b916ea7SJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) dqdx[k + 1][j] = dUdx[k][j];
325af8870a9STimothy Aiken     }
326af8870a9STimothy Aiken 
327af8870a9STimothy Aiken     // strong_conv = dF/dq * dq/dx    (Strong convection)
328af8870a9STimothy Aiken     CeedScalar strong_conv[5] = {0};
3292b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
3302b916ea7SJeremy L Thompson       for (CeedInt k = 0; k < 5; k++) {
3312b916ea7SJeremy L Thompson         for (CeedInt l = 0; l < 5; l++) strong_conv[k] += jacob_F_conv[j][k][l] * dqdx[l][j];
3322b916ea7SJeremy L Thompson       }
3332b916ea7SJeremy L Thompson     }
334af8870a9STimothy Aiken 
335af8870a9STimothy Aiken     // Stabilization
336af8870a9STimothy Aiken     // -- Tau elements
337af8870a9STimothy Aiken     const CeedScalar sound_speed = sqrt(gamma * P / rho);
338af8870a9STimothy Aiken     CeedScalar       Tau_x[3]    = {0.};
339af8870a9STimothy Aiken     Tau_spatial(Tau_x, dXdx, u, sound_speed, c_tau);
340af8870a9STimothy Aiken 
341af8870a9STimothy Aiken     CeedScalar stab[5][3] = {0};
342af8870a9STimothy Aiken     switch (context->stabilization) {
343af8870a9STimothy Aiken       case 0:  // Galerkin
344af8870a9STimothy Aiken         break;
345af8870a9STimothy Aiken       case 1:  // SU
3462b916ea7SJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) {
3472b916ea7SJeremy L Thompson           for (CeedInt k = 0; k < 5; k++) {
348493642f1SJames Wright             for (CeedInt l = 0; l < 5; l++) {
349af8870a9STimothy Aiken               stab[k][j] += jacob_F_conv[j][k][l] * Tau_x[j] * strong_conv[l];
350af8870a9STimothy Aiken             }
3512b916ea7SJeremy L Thompson           }
3522b916ea7SJeremy L Thompson         }
3532b916ea7SJeremy L Thompson         for (CeedInt j = 0; j < 5; j++) {
3542b916ea7SJeremy 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]);
3552b916ea7SJeremy L Thompson         }
356af8870a9STimothy Aiken         break;
357af8870a9STimothy Aiken     }
358b193fadcSJames Wright   }
359af8870a9STimothy Aiken   return 0;
360af8870a9STimothy Aiken }
361