xref: /libCEED/examples/fluids/qfunctions/shocktube.h (revision 46603fc57e28d79cde01b07e9ca450b5fd78aed4)
1019b7682STimothy Aiken // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2019b7682STimothy Aiken // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3019b7682STimothy Aiken // reserved. See files LICENSE and NOTICE for details.
4019b7682STimothy Aiken //
5019b7682STimothy Aiken // This file is part of CEED, a collection of benchmarks, miniapps, software
6019b7682STimothy Aiken // libraries and APIs for efficient high-order finite element and spectral
7019b7682STimothy Aiken // element discretizations for exascale applications. For more information and
8019b7682STimothy Aiken // source code availability see http://github.com/ceed.
9019b7682STimothy Aiken //
10019b7682STimothy Aiken // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11019b7682STimothy Aiken // a collaborative effort of two U.S. Department of Energy organizations (Office
12019b7682STimothy Aiken // of Science and the National Nuclear Security Administration) responsible for
13019b7682STimothy Aiken // the planning and preparation of a capable exascale ecosystem, including
14019b7682STimothy Aiken // software, applications, hardware, advanced system engineering and early
15019b7682STimothy Aiken // testbed platforms, in support of the nation's exascale computing imperative.
16019b7682STimothy Aiken 
17019b7682STimothy Aiken /// @file
18019b7682STimothy Aiken /// Shock tube initial condition and Euler equation operator for Navier-Stokes
19019b7682STimothy Aiken /// example using PETSc - modified from eulervortex.h
20019b7682STimothy Aiken 
21019b7682STimothy Aiken // Model from:
22019b7682STimothy Aiken //   On the Order of Accuracy and Numerical Performance of Two Classes of
23019b7682STimothy Aiken //   Finite Volume WENO Schemes, Zhang, Zhang, and Shu (2011).
24019b7682STimothy Aiken 
25019b7682STimothy Aiken #ifndef shocktube_h
26019b7682STimothy Aiken #define shocktube_h
27019b7682STimothy Aiken 
28ba6664aeSJames Wright #include <ceed.h>
29c9c2c079SJeremy L Thompson #include <math.h>
302b730f8bSJeremy L Thompson 
3113fa47b2SJames Wright #include "utils.h"
32019b7682STimothy Aiken 
3397baf651SJames Wright typedef struct SetupContextShock_ *SetupContextShock;
3497baf651SJames Wright struct SetupContextShock_ {
35019b7682STimothy Aiken   CeedScalar theta0;
36019b7682STimothy Aiken   CeedScalar thetaC;
37019b7682STimothy Aiken   CeedScalar P0;
38019b7682STimothy Aiken   CeedScalar N;
39019b7682STimothy Aiken   CeedScalar cv;
40019b7682STimothy Aiken   CeedScalar cp;
41019b7682STimothy Aiken   CeedScalar time;
42019b7682STimothy Aiken   CeedScalar mid_point;
43019b7682STimothy Aiken   CeedScalar P_high;
44019b7682STimothy Aiken   CeedScalar rho_high;
45019b7682STimothy Aiken   CeedScalar P_low;
46019b7682STimothy Aiken   CeedScalar rho_low;
47019b7682STimothy Aiken   int        wind_type;               // See WindType: 0=ROTATION, 1=TRANSLATION
48019b7682STimothy Aiken   int        bubble_type;             // See BubbleType: 0=SPHERE, 1=CYLINDER
49019b7682STimothy Aiken   int        bubble_continuity_type;  // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK
50019b7682STimothy Aiken };
51019b7682STimothy Aiken 
52019b7682STimothy Aiken typedef struct ShockTubeContext_ *ShockTubeContext;
53019b7682STimothy Aiken struct ShockTubeContext_ {
54019b7682STimothy Aiken   CeedScalar Cyzb;
55019b7682STimothy Aiken   CeedScalar Byzb;
56019b7682STimothy Aiken   CeedScalar c_tau;
57019b7682STimothy Aiken   bool       implicit;
58019b7682STimothy Aiken   bool       yzb;
59019b7682STimothy Aiken   int        stabilization;
60019b7682STimothy Aiken };
61019b7682STimothy Aiken 
62019b7682STimothy Aiken // *****************************************************************************
63019b7682STimothy Aiken // This function sets the initial conditions
64019b7682STimothy Aiken //
65019b7682STimothy Aiken //   Temperature:
66019b7682STimothy Aiken //     T   = P / (rho * R)
67019b7682STimothy Aiken //   Density:
68019b7682STimothy Aiken //     rho = 1.0        if x <= mid_point
69019b7682STimothy Aiken //         = 0.125      if x >  mid_point
70019b7682STimothy Aiken //   Pressure:
71019b7682STimothy Aiken //     P   = 1.0        if x <= mid_point
72019b7682STimothy Aiken //         = 0.1        if x >  mid_point
73019b7682STimothy Aiken //   Velocity:
74019b7682STimothy Aiken //     u   = 0
75019b7682STimothy Aiken //   Velocity/Momentum Density:
76019b7682STimothy Aiken //     Ui  = rho ui
77019b7682STimothy Aiken //   Total Energy:
78019b7682STimothy Aiken //     E   = P / (gamma - 1) + rho (u u)/2
79019b7682STimothy Aiken //
80019b7682STimothy Aiken // Constants:
81019b7682STimothy Aiken //   cv              ,  Specific heat, constant volume
82019b7682STimothy Aiken //   cp              ,  Specific heat, constant pressure
83019b7682STimothy Aiken //   mid_point       ,  Location of initial domain mid_point
84019b7682STimothy Aiken //   gamma  = cp / cv,  Specific heat ratio
85019b7682STimothy Aiken //
86019b7682STimothy Aiken // *****************************************************************************
87019b7682STimothy Aiken 
88019b7682STimothy Aiken // *****************************************************************************
89019b7682STimothy Aiken // This helper function provides support for the exact, time-dependent solution
90019b7682STimothy Aiken //   (currently not implemented) and IC formulation for Euler traveling vortex
91019b7682STimothy Aiken // *****************************************************************************
922b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedInt Exact_ShockTube(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
93019b7682STimothy Aiken   // Context
9497baf651SJames Wright   const SetupContextShock context   = (SetupContextShock)ctx;
95019b7682STimothy Aiken   const CeedScalar        mid_point = context->mid_point;  // Midpoint of the domain
96019b7682STimothy Aiken   const CeedScalar        P_high    = context->P_high;     // Driver section pressure
97019b7682STimothy Aiken   const CeedScalar        rho_high  = context->rho_high;   // Driver section density
98019b7682STimothy Aiken   const CeedScalar        P_low     = context->P_low;      // Driven section pressure
99019b7682STimothy Aiken   const CeedScalar        rho_low   = context->rho_low;    // Driven section density
100019b7682STimothy Aiken 
101019b7682STimothy Aiken   // Setup
102019b7682STimothy Aiken   const CeedScalar gamma = 1.4;   // ratio of specific heats
103019b7682STimothy Aiken   const CeedScalar x     = X[0];  // Coordinates
104019b7682STimothy Aiken 
105019b7682STimothy Aiken   CeedScalar rho, P, u[3] = {0.};
106019b7682STimothy Aiken 
107019b7682STimothy Aiken   // Initial Conditions
108019b7682STimothy Aiken   if (x <= mid_point) {
109019b7682STimothy Aiken     rho = rho_high;
110019b7682STimothy Aiken     P   = P_high;
111019b7682STimothy Aiken   } else {
112019b7682STimothy Aiken     rho = rho_low;
113019b7682STimothy Aiken     P   = P_low;
114019b7682STimothy Aiken   }
115019b7682STimothy Aiken 
116019b7682STimothy Aiken   // Assign exact solution
117019b7682STimothy Aiken   q[0] = rho;
118019b7682STimothy Aiken   q[1] = rho * u[0];
119019b7682STimothy Aiken   q[2] = rho * u[1];
120019b7682STimothy Aiken   q[3] = rho * u[2];
121019b7682STimothy Aiken   q[4] = P / (gamma - 1.0) + rho * (u[0] * u[0]) / 2.;
122019b7682STimothy Aiken 
123019b7682STimothy Aiken   // Return
124019b7682STimothy Aiken   return 0;
125019b7682STimothy Aiken }
126019b7682STimothy Aiken 
127019b7682STimothy Aiken // *****************************************************************************
128019b7682STimothy Aiken // Helper function for computing flux Jacobian
129019b7682STimothy Aiken // *****************************************************************************
1302b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ConvectiveFluxJacobian_Euler(CeedScalar dF[3][5][5], const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
131019b7682STimothy Aiken                                                         const CeedScalar gamma) {
132019b7682STimothy Aiken   CeedScalar u_sq = u[0] * u[0] + u[1] * u[1] + u[2] * u[2];  // Velocity square
133019b7682STimothy Aiken   for (CeedInt i = 0; i < 3; i++) {                           // Jacobian matrices for 3 directions
134019b7682STimothy Aiken     for (CeedInt j = 0; j < 3; j++) {                         // Rows of each Jacobian matrix
135019b7682STimothy Aiken       dF[i][j + 1][0] = ((i == j) ? ((gamma - 1.) * (u_sq / 2.)) : 0.) - u[i] * u[j];
136019b7682STimothy Aiken       for (CeedInt k = 0; k < 3; k++) {  // Columns of each Jacobian matrix
137019b7682STimothy Aiken         dF[i][0][k + 1]     = ((i == k) ? 1. : 0.);
1382b730f8bSJeremy 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.);
1392b730f8bSJeremy L Thompson         dF[i][4][k + 1]     = ((i == k) ? (E * gamma / rho - (gamma - 1.) * u_sq / 2.) : 0.) - (gamma - 1.) * u[i] * u[k];
140019b7682STimothy Aiken       }
141019b7682STimothy Aiken       dF[i][j + 1][4] = ((i == j) ? (gamma - 1.) : 0.);
142019b7682STimothy Aiken     }
143019b7682STimothy Aiken     dF[i][4][0] = u[i] * ((gamma - 1.) * u_sq - E * gamma / rho);
144019b7682STimothy Aiken     dF[i][4][4] = u[i] * gamma;
145019b7682STimothy Aiken   }
146019b7682STimothy Aiken }
147019b7682STimothy Aiken 
148019b7682STimothy Aiken // *****************************************************************************
149019b7682STimothy Aiken // Helper function for calculating the covariant length scale in the direction
150019b7682STimothy Aiken // of some 3 element input vector
151019b7682STimothy Aiken //
152019b7682STimothy Aiken // Where
153019b7682STimothy Aiken //  vec         = vector that length is measured in the direction of
154019b7682STimothy Aiken //  h           = covariant element length along vec
155019b7682STimothy Aiken // *****************************************************************************
1562b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Covariant_length_along_vector(CeedScalar vec[3], const CeedScalar dXdx[3][3]) {
157019b7682STimothy Aiken   CeedScalar vec_norm            = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
158019b7682STimothy Aiken   CeedScalar vec_dot_jacobian[3] = {0.0};
159019b7682STimothy Aiken   for (CeedInt i = 0; i < 3; i++) {
160019b7682STimothy Aiken     for (CeedInt j = 0; j < 3; j++) {
161019b7682STimothy Aiken       vec_dot_jacobian[i] += dXdx[j][i] * vec[i];
162019b7682STimothy Aiken     }
163019b7682STimothy Aiken   }
1642b730f8bSJeremy L Thompson   CeedScalar norm_vec_dot_jacobian =
1652b730f8bSJeremy 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]);
166019b7682STimothy Aiken   CeedScalar h = 2.0 * vec_norm / norm_vec_dot_jacobian;
167019b7682STimothy Aiken   return h;
168019b7682STimothy Aiken }
169019b7682STimothy Aiken 
170019b7682STimothy Aiken // *****************************************************************************
171019b7682STimothy Aiken // Helper function for computing Tau elements (stabilization constant)
172019b7682STimothy Aiken //   Model from:
173019b7682STimothy Aiken //     Stabilized Methods for Compressible Flows, Hughes et al 2010
174019b7682STimothy Aiken //
175019b7682STimothy Aiken //   Spatial criterion #2 - Tau is a 3x3 diagonal matrix
176019b7682STimothy Aiken //   Tau[i] = c_tau h[i] Xi(Pe) / rho(A[i]) (no sum)
177019b7682STimothy Aiken //
178019b7682STimothy Aiken // Where
179019b7682STimothy Aiken //   c_tau     = stabilization constant (0.5 is reported as "optimal")
180019b7682STimothy Aiken //   h[i]      = 2 length(dxdX[i])
181019b7682STimothy Aiken //   Pe        = Peclet number ( Pe = sqrt(u u) / dot(dXdx,u) diffusivity )
182019b7682STimothy Aiken //   Xi(Pe)    = coth Pe - 1. / Pe (1. at large local Peclet number )
183019b7682STimothy Aiken //   rho(A[i]) = spectral radius of the convective flux Jacobian i,
184019b7682STimothy Aiken //               wave speed in direction i
185019b7682STimothy Aiken // *****************************************************************************
1862b730f8bSJeremy 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,
1872b730f8bSJeremy L Thompson                                        const CeedScalar c_tau) {
188ba6664aeSJames Wright   for (CeedInt i = 0; i < 3; i++) {
189019b7682STimothy Aiken     // length of element in direction i
1902b730f8bSJeremy L Thompson     CeedScalar h = 2 / sqrt(dXdx[0][i] * dXdx[0][i] + dXdx[1][i] * dXdx[1][i] + dXdx[2][i] * dXdx[2][i]);
191019b7682STimothy Aiken     // fastest wave in direction i
192019b7682STimothy Aiken     CeedScalar fastest_wave = fabs(u[i]) + sound_speed;
193019b7682STimothy Aiken     Tau_x[i]                = c_tau * h / fastest_wave;
194019b7682STimothy Aiken   }
195019b7682STimothy Aiken }
196019b7682STimothy Aiken 
197019b7682STimothy Aiken // *****************************************************************************
198019b7682STimothy Aiken // This QFunction sets the initial conditions for shock tube
199019b7682STimothy Aiken // *****************************************************************************
2002b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsShockTube)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
201019b7682STimothy Aiken   // Inputs
202019b7682STimothy Aiken   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
203019b7682STimothy Aiken 
204019b7682STimothy Aiken   // Outputs
205019b7682STimothy Aiken   CeedScalar(*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
206019b7682STimothy Aiken 
207019b7682STimothy Aiken   CeedPragmaSIMD
208019b7682STimothy Aiken       // Quadrature Point Loop
209019b7682STimothy Aiken       for (CeedInt i = 0; i < Q; i++) {
210019b7682STimothy Aiken     const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]};
211019b7682STimothy Aiken     CeedScalar       q[5];
212019b7682STimothy Aiken 
213019b7682STimothy Aiken     Exact_ShockTube(3, 0., x, 5, q, ctx);
214019b7682STimothy Aiken 
2152b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
216019b7682STimothy Aiken   }  // End of Quadrature Point Loop
217019b7682STimothy Aiken 
218019b7682STimothy Aiken   // Return
219019b7682STimothy Aiken   return 0;
220019b7682STimothy Aiken }
221019b7682STimothy Aiken 
222019b7682STimothy Aiken // *****************************************************************************
223019b7682STimothy Aiken // This QFunction implements the following formulation of Euler equations
224019b7682STimothy Aiken //   with explicit time stepping method
225019b7682STimothy Aiken //
226019b7682STimothy Aiken // This is 3D Euler for compressible gas dynamics in conservation
227019b7682STimothy Aiken //   form with state variables of density, momentum density, and total
228019b7682STimothy Aiken //   energy density.
229019b7682STimothy Aiken //
230019b7682STimothy Aiken // State Variables: q = ( rho, U1, U2, U3, E )
231019b7682STimothy Aiken //   rho - Mass Density
232019b7682STimothy Aiken //   Ui  - Momentum Density,      Ui = rho ui
233019b7682STimothy Aiken //   E   - Total Energy Density,  E  = P / (gamma - 1) + rho (u u)/2
234019b7682STimothy Aiken //
235019b7682STimothy Aiken // Euler Equations:
236019b7682STimothy Aiken //   drho/dt + div( U )                   = 0
237019b7682STimothy Aiken //   dU/dt   + div( rho (u x u) + P I3 )  = 0
238019b7682STimothy Aiken //   dE/dt   + div( (E + P) u )           = 0
239019b7682STimothy Aiken //
240019b7682STimothy Aiken // Equation of State:
241019b7682STimothy Aiken //   P = (gamma - 1) (E - rho (u u) / 2)
242019b7682STimothy Aiken //
243019b7682STimothy Aiken // Constants:
244019b7682STimothy Aiken //   cv              ,  Specific heat, constant volume
245019b7682STimothy Aiken //   cp              ,  Specific heat, constant pressure
246019b7682STimothy Aiken //   g               ,  Gravity
247019b7682STimothy Aiken //   gamma  = cp / cv,  Specific heat ratio
248019b7682STimothy Aiken // *****************************************************************************
2492b730f8bSJeremy L Thompson CEED_QFUNCTION(EulerShockTube)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
250019b7682STimothy Aiken   // Inputs
251*46603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]      = (const CeedScalar(*)[CEED_Q_VLA])in[0];
252*46603fc5SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA]  = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
253*46603fc5SJames Wright   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
254*46603fc5SJames Wright 
255019b7682STimothy Aiken   // Outputs
256*46603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]     = (CeedScalar(*)[CEED_Q_VLA])out[0];
257*46603fc5SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
258019b7682STimothy Aiken 
259019b7682STimothy Aiken   const CeedScalar gamma = 1.4;
260019b7682STimothy Aiken 
261019b7682STimothy Aiken   ShockTubeContext context = (ShockTubeContext)ctx;
262019b7682STimothy Aiken   const CeedScalar Cyzb    = context->Cyzb;
263019b7682STimothy Aiken   const CeedScalar Byzb    = context->Byzb;
264019b7682STimothy Aiken   const CeedScalar c_tau   = context->c_tau;
265019b7682STimothy Aiken 
266019b7682STimothy Aiken   CeedPragmaSIMD
267019b7682STimothy Aiken       // Quadrature Point Loop
268019b7682STimothy Aiken       for (CeedInt i = 0; i < Q; i++) {
269019b7682STimothy Aiken     // Setup
270019b7682STimothy Aiken     // -- Interp in
271019b7682STimothy Aiken     const CeedScalar rho      = q[0][i];
2722b730f8bSJeremy L Thompson     const CeedScalar u[3]     = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
273019b7682STimothy Aiken     const CeedScalar E        = q[4][i];
2742b730f8bSJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
2752b730f8bSJeremy L Thompson     const CeedScalar dU[3][3] = {
2762b730f8bSJeremy L Thompson         {dq[0][1][i], dq[1][1][i], dq[2][1][i]},
2772b730f8bSJeremy L Thompson         {dq[0][2][i], dq[1][2][i], dq[2][2][i]},
2782b730f8bSJeremy L Thompson         {dq[0][3][i], dq[1][3][i], dq[2][3][i]}
279019b7682STimothy Aiken     };
2802b730f8bSJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
281019b7682STimothy Aiken     // -- Interp-to-Interp q_data
282019b7682STimothy Aiken     const CeedScalar wdetJ = q_data[0][i];
283019b7682STimothy Aiken     // -- Interp-to-Grad q_data
284019b7682STimothy Aiken     // ---- Inverse of change of coordinate matrix: X_i,j
2852b730f8bSJeremy L Thompson     const CeedScalar dXdx[3][3] = {
2862b730f8bSJeremy L Thompson         {q_data[1][i], q_data[2][i], q_data[3][i]},
2872b730f8bSJeremy L Thompson         {q_data[4][i], q_data[5][i], q_data[6][i]},
2882b730f8bSJeremy L Thompson         {q_data[7][i], q_data[8][i], q_data[9][i]}
289019b7682STimothy Aiken     };
290019b7682STimothy Aiken     // dU/dx
291019b7682STimothy Aiken     CeedScalar du[3][3]        = {{0}};
292019b7682STimothy Aiken     CeedScalar drhodx[3]       = {0};
293019b7682STimothy Aiken     CeedScalar dEdx[3]         = {0};
294019b7682STimothy Aiken     CeedScalar dUdx[3][3]      = {{0}};
295019b7682STimothy Aiken     CeedScalar dXdxdXdxT[3][3] = {{0}};
296ba6664aeSJames Wright     for (CeedInt j = 0; j < 3; j++) {
297ba6664aeSJames Wright       for (CeedInt k = 0; k < 3; k++) {
298019b7682STimothy Aiken         du[j][k] = (dU[j][k] - drho[k] * u[j]) / rho;
299019b7682STimothy Aiken         drhodx[j] += drho[k] * dXdx[k][j];
300019b7682STimothy Aiken         dEdx[j] += dE[k] * dXdx[k][j];
301ba6664aeSJames Wright         for (CeedInt l = 0; l < 3; l++) {
302019b7682STimothy Aiken           dUdx[j][k] += dU[j][l] * dXdx[l][k];
303019b7682STimothy Aiken           dXdxdXdxT[j][k] += dXdx[j][l] * dXdx[k][l];  // dXdx_j,k * dXdx_k,j
304019b7682STimothy Aiken         }
305019b7682STimothy Aiken       }
306019b7682STimothy Aiken     }
307019b7682STimothy Aiken 
3082b730f8bSJeremy 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,
309019b7682STimothy Aiken                      P = E_internal * (gamma - 1);  // P = pressure
310019b7682STimothy Aiken 
311019b7682STimothy Aiken     // The Physics
312019b7682STimothy Aiken     // Zero v and dv so all future terms can safely sum into it
313ba6664aeSJames Wright     for (CeedInt j = 0; j < 5; j++) {
314019b7682STimothy Aiken       v[j][i] = 0;
3152b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) dv[k][j][i] = 0;
316019b7682STimothy Aiken     }
317019b7682STimothy Aiken 
318019b7682STimothy Aiken     // -- Density
319019b7682STimothy Aiken     // ---- u rho
3202b730f8bSJeremy 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]);
321019b7682STimothy Aiken     // -- Momentum
322019b7682STimothy Aiken     // ---- rho (u x u) + P I3
3232b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
3242b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) {
3252b730f8bSJeremy 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] +
326019b7682STimothy Aiken                                     (rho * u[j] * u[2] + (j == 2 ? P : 0)) * dXdx[k][2]);
3272b730f8bSJeremy L Thompson       }
3282b730f8bSJeremy L Thompson     }
329019b7682STimothy Aiken     // -- Total Energy Density
330019b7682STimothy Aiken     // ---- (E + P) u
3312b730f8bSJeremy 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]);
332019b7682STimothy Aiken 
333019b7682STimothy Aiken     // -- YZB stabilization
334019b7682STimothy Aiken     if (context->yzb) {
335019b7682STimothy Aiken       CeedScalar drho_norm    = 0.0;    // magnitude of the density gradient
336019b7682STimothy Aiken       CeedScalar j_vec[3]     = {0.0};  // unit vector aligned with the density gradient
337019b7682STimothy Aiken       CeedScalar h_shock      = 0.0;    // element lengthscale
338019b7682STimothy Aiken       CeedScalar acoustic_vel = 0.0;    // characteristic velocity, acoustic speed
339019b7682STimothy Aiken       CeedScalar tau_shock    = 0.0;    // timescale
340019b7682STimothy Aiken       CeedScalar nu_shock     = 0.0;    // artificial diffusion
341019b7682STimothy Aiken 
342019b7682STimothy Aiken       // Unit vector aligned with the density gradient
3432b730f8bSJeremy L Thompson       drho_norm = sqrt(drhodx[0] * drhodx[0] + drhodx[1] * drhodx[1] + drhodx[2] * drhodx[2]);
3442b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) j_vec[j] = drhodx[j] / (drho_norm + 1e-20);
345019b7682STimothy Aiken 
346019b7682STimothy Aiken       if (drho_norm == 0.0) {
347019b7682STimothy Aiken         nu_shock = 0.0;
348019b7682STimothy Aiken       } else {
349019b7682STimothy Aiken         h_shock = Covariant_length_along_vector(j_vec, dXdx);
350019b7682STimothy Aiken         h_shock /= Cyzb;
351019b7682STimothy Aiken         acoustic_vel = sqrt(gamma * P / rho);
352019b7682STimothy Aiken         tau_shock    = h_shock / (2 * acoustic_vel) * pow(drho_norm * h_shock / rho, Byzb);
353019b7682STimothy Aiken         nu_shock     = fabs(tau_shock * acoustic_vel * acoustic_vel);
354019b7682STimothy Aiken       }
355019b7682STimothy Aiken 
3562b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][0][i] -= wdetJ * nu_shock * drhodx[j];
357019b7682STimothy Aiken 
3582b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) {
3592b730f8bSJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) dv[j][k][i] -= wdetJ * nu_shock * du[k][j];
3602b730f8bSJeremy L Thompson       }
361019b7682STimothy Aiken 
3622b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * nu_shock * dEdx[j];
363019b7682STimothy Aiken     }
364019b7682STimothy Aiken 
365019b7682STimothy Aiken     // Stabilization
366019b7682STimothy Aiken     // Need the Jacobian for the advective fluxes for stabilization
367019b7682STimothy Aiken     //    indexed as: jacob_F_conv[direction][flux component][solution component]
368019b7682STimothy Aiken     CeedScalar jacob_F_conv[3][5][5] = {{{0.}}};
369019b7682STimothy Aiken     ConvectiveFluxJacobian_Euler(jacob_F_conv, rho, u, E, gamma);
370019b7682STimothy Aiken 
371019b7682STimothy Aiken     // dqdx collects drhodx, dUdx and dEdx in one vector
372019b7682STimothy Aiken     CeedScalar dqdx[5][3];
373ba6664aeSJames Wright     for (CeedInt j = 0; j < 3; j++) {
374019b7682STimothy Aiken       dqdx[0][j] = drhodx[j];
375019b7682STimothy Aiken       dqdx[4][j] = dEdx[j];
3762b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) dqdx[k + 1][j] = dUdx[k][j];
377019b7682STimothy Aiken     }
378019b7682STimothy Aiken 
379019b7682STimothy Aiken     // strong_conv = dF/dq * dq/dx    (Strong convection)
380019b7682STimothy Aiken     CeedScalar strong_conv[5] = {0};
3812b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
3822b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 5; k++) {
3832b730f8bSJeremy L Thompson         for (CeedInt l = 0; l < 5; l++) strong_conv[k] += jacob_F_conv[j][k][l] * dqdx[l][j];
3842b730f8bSJeremy L Thompson       }
3852b730f8bSJeremy L Thompson     }
386019b7682STimothy Aiken 
387019b7682STimothy Aiken     // Stabilization
388019b7682STimothy Aiken     // -- Tau elements
389019b7682STimothy Aiken     const CeedScalar sound_speed = sqrt(gamma * P / rho);
390019b7682STimothy Aiken     CeedScalar       Tau_x[3]    = {0.};
391019b7682STimothy Aiken     Tau_spatial(Tau_x, dXdx, u, sound_speed, c_tau);
392019b7682STimothy Aiken 
393019b7682STimothy Aiken     CeedScalar stab[5][3] = {0};
394019b7682STimothy Aiken     switch (context->stabilization) {
395019b7682STimothy Aiken       case 0:  // Galerkin
396019b7682STimothy Aiken         break;
397019b7682STimothy Aiken       case 1:  // SU
3982b730f8bSJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) {
3992b730f8bSJeremy L Thompson           for (CeedInt k = 0; k < 5; k++) {
400ba6664aeSJames Wright             for (CeedInt l = 0; l < 5; l++) {
401019b7682STimothy Aiken               stab[k][j] += jacob_F_conv[j][k][l] * Tau_x[j] * strong_conv[l];
402019b7682STimothy Aiken             }
4032b730f8bSJeremy L Thompson           }
4042b730f8bSJeremy L Thompson         }
4052b730f8bSJeremy L Thompson         for (CeedInt j = 0; j < 5; j++) {
4062b730f8bSJeremy 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]);
4072b730f8bSJeremy L Thompson         }
408019b7682STimothy Aiken         break;
409019b7682STimothy Aiken     }
410019b7682STimothy Aiken 
411019b7682STimothy Aiken   }  // End Quadrature Point Loop
412019b7682STimothy Aiken 
413019b7682STimothy Aiken   // Return
414019b7682STimothy Aiken   return 0;
415019b7682STimothy Aiken }
416019b7682STimothy Aiken 
417019b7682STimothy Aiken #endif  // shocktube_h
418