xref: /honee/qfunctions/shocktube.h (revision af8870a93789e789520500efb031a5d737509d24)
1*af8870a9STimothy Aiken // Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2*af8870a9STimothy Aiken // the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3*af8870a9STimothy Aiken // reserved. See files LICENSE and NOTICE for details.
4*af8870a9STimothy Aiken //
5*af8870a9STimothy Aiken // This file is part of CEED, a collection of benchmarks, miniapps, software
6*af8870a9STimothy Aiken // libraries and APIs for efficient high-order finite element and spectral
7*af8870a9STimothy Aiken // element discretizations for exascale applications. For more information and
8*af8870a9STimothy Aiken // source code availability see http://github.com/ceed.
9*af8870a9STimothy Aiken //
10*af8870a9STimothy Aiken // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11*af8870a9STimothy Aiken // a collaborative effort of two U.S. Department of Energy organizations (Office
12*af8870a9STimothy Aiken // of Science and the National Nuclear Security Administration) responsible for
13*af8870a9STimothy Aiken // the planning and preparation of a capable exascale ecosystem, including
14*af8870a9STimothy Aiken // software, applications, hardware, advanced system engineering and early
15*af8870a9STimothy Aiken // testbed platforms, in support of the nation's exascale computing imperative.
16*af8870a9STimothy Aiken 
17*af8870a9STimothy Aiken /// @file
18*af8870a9STimothy Aiken /// Shock tube initial condition and Euler equation operator for Navier-Stokes
19*af8870a9STimothy Aiken /// example using PETSc - modified from eulervortex.h
20*af8870a9STimothy Aiken 
21*af8870a9STimothy Aiken // Model from:
22*af8870a9STimothy Aiken //   On the Order of Accuracy and Numerical Performance of Two Classes of
23*af8870a9STimothy Aiken //   Finite Volume WENO Schemes, Zhang, Zhang, and Shu (2011).
24*af8870a9STimothy Aiken 
25*af8870a9STimothy Aiken #ifndef shocktube_h
26*af8870a9STimothy Aiken #define shocktube_h
27*af8870a9STimothy Aiken 
28*af8870a9STimothy Aiken #include <math.h>
29*af8870a9STimothy Aiken 
30*af8870a9STimothy Aiken #ifndef M_PI
31*af8870a9STimothy Aiken #define M_PI    3.14159265358979323846
32*af8870a9STimothy Aiken #endif
33*af8870a9STimothy Aiken 
34*af8870a9STimothy Aiken #ifndef setup_context_struct
35*af8870a9STimothy Aiken #define setup_context_struct
36*af8870a9STimothy Aiken typedef struct SetupContext_ *SetupContext;
37*af8870a9STimothy Aiken struct SetupContext_ {
38*af8870a9STimothy Aiken   CeedScalar theta0;
39*af8870a9STimothy Aiken   CeedScalar thetaC;
40*af8870a9STimothy Aiken   CeedScalar P0;
41*af8870a9STimothy Aiken   CeedScalar N;
42*af8870a9STimothy Aiken   CeedScalar cv;
43*af8870a9STimothy Aiken   CeedScalar cp;
44*af8870a9STimothy Aiken   CeedScalar g[3];
45*af8870a9STimothy Aiken   CeedScalar rc;
46*af8870a9STimothy Aiken   CeedScalar lx;
47*af8870a9STimothy Aiken   CeedScalar ly;
48*af8870a9STimothy Aiken   CeedScalar lz;
49*af8870a9STimothy Aiken   CeedScalar center[3];
50*af8870a9STimothy Aiken   CeedScalar dc_axis[3];
51*af8870a9STimothy Aiken   CeedScalar wind[3];
52*af8870a9STimothy Aiken   CeedScalar time;
53*af8870a9STimothy Aiken   CeedScalar mid_point;
54*af8870a9STimothy Aiken   CeedScalar P_high;
55*af8870a9STimothy Aiken   CeedScalar rho_high;
56*af8870a9STimothy Aiken   CeedScalar P_low;
57*af8870a9STimothy Aiken   CeedScalar rho_low;
58*af8870a9STimothy Aiken   int wind_type;              // See WindType: 0=ROTATION, 1=TRANSLATION
59*af8870a9STimothy Aiken   int bubble_type;            // See BubbleType: 0=SPHERE, 1=CYLINDER
60*af8870a9STimothy Aiken   int bubble_continuity_type; // See BubbleContinuityType: 0=SMOOTH, 1=BACK_SHARP 2=THICK
61*af8870a9STimothy Aiken };
62*af8870a9STimothy Aiken #endif
63*af8870a9STimothy Aiken 
64*af8870a9STimothy Aiken #ifndef shocktube_context_struct
65*af8870a9STimothy Aiken #define shocktube_context_struct
66*af8870a9STimothy Aiken typedef struct ShockTubeContext_ *ShockTubeContext;
67*af8870a9STimothy Aiken struct ShockTubeContext_ {
68*af8870a9STimothy Aiken   CeedScalar Cyzb;
69*af8870a9STimothy Aiken   CeedScalar Byzb;
70*af8870a9STimothy Aiken   CeedScalar c_tau;
71*af8870a9STimothy Aiken   bool implicit;
72*af8870a9STimothy Aiken   bool yzb;
73*af8870a9STimothy Aiken   int stabilization;
74*af8870a9STimothy Aiken };
75*af8870a9STimothy Aiken #endif
76*af8870a9STimothy Aiken 
77*af8870a9STimothy Aiken // *****************************************************************************
78*af8870a9STimothy Aiken // This function sets the initial conditions
79*af8870a9STimothy Aiken //
80*af8870a9STimothy Aiken //   Temperature:
81*af8870a9STimothy Aiken //     T   = P / (rho * R)
82*af8870a9STimothy Aiken //   Density:
83*af8870a9STimothy Aiken //     rho = 1.0        if x <= mid_point
84*af8870a9STimothy Aiken //         = 0.125      if x >  mid_point
85*af8870a9STimothy Aiken //   Pressure:
86*af8870a9STimothy Aiken //     P   = 1.0        if x <= mid_point
87*af8870a9STimothy Aiken //         = 0.1        if x >  mid_point
88*af8870a9STimothy Aiken //   Velocity:
89*af8870a9STimothy Aiken //     u   = 0
90*af8870a9STimothy Aiken //   Velocity/Momentum Density:
91*af8870a9STimothy Aiken //     Ui  = rho ui
92*af8870a9STimothy Aiken //   Total Energy:
93*af8870a9STimothy Aiken //     E   = P / (gamma - 1) + rho (u u)/2
94*af8870a9STimothy Aiken //
95*af8870a9STimothy Aiken // Constants:
96*af8870a9STimothy Aiken //   cv              ,  Specific heat, constant volume
97*af8870a9STimothy Aiken //   cp              ,  Specific heat, constant pressure
98*af8870a9STimothy Aiken //   mid_point       ,  Location of initial domain mid_point
99*af8870a9STimothy Aiken //   gamma  = cp / cv,  Specific heat ratio
100*af8870a9STimothy Aiken //
101*af8870a9STimothy Aiken // *****************************************************************************
102*af8870a9STimothy Aiken 
103*af8870a9STimothy Aiken // *****************************************************************************
104*af8870a9STimothy Aiken // This helper function provides support for the exact, time-dependent solution
105*af8870a9STimothy Aiken //   (currently not implemented) and IC formulation for Euler traveling vortex
106*af8870a9STimothy Aiken // *****************************************************************************
107*af8870a9STimothy Aiken CEED_QFUNCTION_HELPER int Exact_ShockTube(CeedInt dim, CeedScalar time,
108*af8870a9STimothy Aiken     const CeedScalar X[], CeedInt Nf, CeedScalar q[],
109*af8870a9STimothy Aiken     void *ctx) {
110*af8870a9STimothy Aiken 
111*af8870a9STimothy Aiken   // Context
112*af8870a9STimothy Aiken   const SetupContext context = (SetupContext)ctx;
113*af8870a9STimothy Aiken   const CeedScalar mid_point = context->mid_point;      // Midpoint of the domain
114*af8870a9STimothy Aiken   const CeedScalar P_high = context->P_high;            // Driver section pressure
115*af8870a9STimothy Aiken   const CeedScalar rho_high = context->rho_high;        // Driver section density
116*af8870a9STimothy Aiken   const CeedScalar P_low = context->P_low;              // Driven section pressure
117*af8870a9STimothy Aiken   const CeedScalar rho_low = context->rho_low;          // Driven section density
118*af8870a9STimothy Aiken 
119*af8870a9STimothy Aiken   // Setup
120*af8870a9STimothy Aiken   const CeedScalar gamma = 1.4;    // ratio of specific heats
121*af8870a9STimothy Aiken   const CeedScalar x     = X[0];   // Coordinates
122*af8870a9STimothy Aiken 
123*af8870a9STimothy Aiken   CeedScalar rho, P, u[3] = {0.};
124*af8870a9STimothy Aiken 
125*af8870a9STimothy Aiken   // Initial Conditions
126*af8870a9STimothy Aiken   if (x <= mid_point) {
127*af8870a9STimothy Aiken     rho = rho_high;
128*af8870a9STimothy Aiken     P   = P_high;
129*af8870a9STimothy Aiken   } else {
130*af8870a9STimothy Aiken     rho = rho_low;
131*af8870a9STimothy Aiken     P   = P_low;
132*af8870a9STimothy Aiken   }
133*af8870a9STimothy Aiken 
134*af8870a9STimothy Aiken   // Assign exact solution
135*af8870a9STimothy Aiken   q[0] = rho;
136*af8870a9STimothy Aiken   q[1] = rho * u[0];
137*af8870a9STimothy Aiken   q[2] = rho * u[1];
138*af8870a9STimothy Aiken   q[3] = rho * u[2];
139*af8870a9STimothy Aiken   q[4] = P / (gamma-1.0) + rho * (u[0]*u[0]) / 2.;
140*af8870a9STimothy Aiken 
141*af8870a9STimothy Aiken   // Return
142*af8870a9STimothy Aiken   return 0;
143*af8870a9STimothy Aiken }
144*af8870a9STimothy Aiken 
145*af8870a9STimothy Aiken // *****************************************************************************
146*af8870a9STimothy Aiken // Helper function for computing flux Jacobian
147*af8870a9STimothy Aiken // *****************************************************************************
148*af8870a9STimothy Aiken CEED_QFUNCTION_HELPER void ConvectiveFluxJacobian_Euler(CeedScalar dF[3][5][5],
149*af8870a9STimothy Aiken     const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
150*af8870a9STimothy Aiken     const CeedScalar gamma) {
151*af8870a9STimothy Aiken   CeedScalar u_sq = u[0]*u[0] + u[1]*u[1] + u[2]*u[2]; // Velocity square
152*af8870a9STimothy Aiken   for (CeedInt i=0; i<3; i++) { // Jacobian matrices for 3 directions
153*af8870a9STimothy Aiken     for (CeedInt j=0; j<3; j++) { // Rows of each Jacobian matrix
154*af8870a9STimothy Aiken       dF[i][j+1][0] = ((i==j) ? ((gamma-1.)*(u_sq/2.)) : 0.) - u[i]*u[j];
155*af8870a9STimothy Aiken       for (CeedInt k=0; k<3; k++) { // Columns of each Jacobian matrix
156*af8870a9STimothy Aiken         dF[i][0][k+1]   = ((i==k) ? 1. : 0.);
157*af8870a9STimothy Aiken         dF[i][j+1][k+1] = ((j==k) ? u[i] : 0.) +
158*af8870a9STimothy Aiken                           ((i==k) ? u[j] : 0.) -
159*af8870a9STimothy Aiken                           ((i==j) ? u[k] : 0.) * (gamma-1.);
160*af8870a9STimothy Aiken         dF[i][4][k+1]   = ((i==k) ? (E*gamma/rho - (gamma-1.)*u_sq/2.) : 0.) -
161*af8870a9STimothy Aiken                           (gamma-1.)*u[i]*u[k];
162*af8870a9STimothy Aiken       }
163*af8870a9STimothy Aiken       dF[i][j+1][4] = ((i==j) ? (gamma-1.) : 0.);
164*af8870a9STimothy Aiken     }
165*af8870a9STimothy Aiken     dF[i][4][0] = u[i] * ((gamma-1.)*u_sq - E*gamma/rho);
166*af8870a9STimothy Aiken     dF[i][4][4] = u[i] * gamma;
167*af8870a9STimothy Aiken   }
168*af8870a9STimothy Aiken }
169*af8870a9STimothy Aiken 
170*af8870a9STimothy Aiken // *****************************************************************************
171*af8870a9STimothy Aiken // Helper function for calculating the covariant length scale in the direction
172*af8870a9STimothy Aiken // of some 3 element input vector
173*af8870a9STimothy Aiken //
174*af8870a9STimothy Aiken // Where
175*af8870a9STimothy Aiken //  vec         = vector that length is measured in the direction of
176*af8870a9STimothy Aiken //  h           = covariant element length along vec
177*af8870a9STimothy Aiken // *****************************************************************************
178*af8870a9STimothy Aiken CEED_QFUNCTION_HELPER CeedScalar Covariant_length_along_vector(
179*af8870a9STimothy Aiken   CeedScalar vec[3], const CeedScalar dXdx[3][3]) {
180*af8870a9STimothy Aiken 
181*af8870a9STimothy Aiken   CeedScalar vec_norm = sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
182*af8870a9STimothy Aiken   CeedScalar vec_dot_jacobian[3] = {0.0};
183*af8870a9STimothy Aiken   for (CeedInt i=0; i<3; i++) {
184*af8870a9STimothy Aiken     for (CeedInt j=0; j<3; j++) {
185*af8870a9STimothy Aiken       vec_dot_jacobian[i] += dXdx[j][i]*vec[i];
186*af8870a9STimothy Aiken     }
187*af8870a9STimothy Aiken   }
188*af8870a9STimothy Aiken   CeedScalar norm_vec_dot_jacobian = sqrt(vec_dot_jacobian[0]*vec_dot_jacobian[0]+
189*af8870a9STimothy Aiken                                           vec_dot_jacobian[1]*vec_dot_jacobian[1]+
190*af8870a9STimothy Aiken                                           vec_dot_jacobian[2]*vec_dot_jacobian[2]);
191*af8870a9STimothy Aiken   CeedScalar h = 2.0 * vec_norm / norm_vec_dot_jacobian;
192*af8870a9STimothy Aiken   return h;
193*af8870a9STimothy Aiken }
194*af8870a9STimothy Aiken 
195*af8870a9STimothy Aiken 
196*af8870a9STimothy Aiken // *****************************************************************************
197*af8870a9STimothy Aiken // Helper function for computing Tau elements (stabilization constant)
198*af8870a9STimothy Aiken //   Model from:
199*af8870a9STimothy Aiken //     Stabilized Methods for Compressible Flows, Hughes et al 2010
200*af8870a9STimothy Aiken //
201*af8870a9STimothy Aiken //   Spatial criterion #2 - Tau is a 3x3 diagonal matrix
202*af8870a9STimothy Aiken //   Tau[i] = c_tau h[i] Xi(Pe) / rho(A[i]) (no sum)
203*af8870a9STimothy Aiken //
204*af8870a9STimothy Aiken // Where
205*af8870a9STimothy Aiken //   c_tau     = stabilization constant (0.5 is reported as "optimal")
206*af8870a9STimothy Aiken //   h[i]      = 2 length(dxdX[i])
207*af8870a9STimothy Aiken //   Pe        = Peclet number ( Pe = sqrt(u u) / dot(dXdx,u) diffusivity )
208*af8870a9STimothy Aiken //   Xi(Pe)    = coth Pe - 1. / Pe (1. at large local Peclet number )
209*af8870a9STimothy Aiken //   rho(A[i]) = spectral radius of the convective flux Jacobian i,
210*af8870a9STimothy Aiken //               wave speed in direction i
211*af8870a9STimothy Aiken // *****************************************************************************
212*af8870a9STimothy Aiken CEED_QFUNCTION_HELPER void Tau_spatial(CeedScalar Tau_x[3],
213*af8870a9STimothy Aiken                                        const CeedScalar dXdx[3][3], const CeedScalar u[3],
214*af8870a9STimothy Aiken                                        const CeedScalar sound_speed, const CeedScalar c_tau) {
215*af8870a9STimothy Aiken   for (int i=0; i<3; i++) {
216*af8870a9STimothy Aiken     // length of element in direction i
217*af8870a9STimothy Aiken     CeedScalar h = 2 / sqrt(dXdx[0][i]*dXdx[0][i] + dXdx[1][i]*dXdx[1][i] +
218*af8870a9STimothy Aiken                             dXdx[2][i]*dXdx[2][i]);
219*af8870a9STimothy Aiken     // fastest wave in direction i
220*af8870a9STimothy Aiken     CeedScalar fastest_wave = fabs(u[i]) + sound_speed;
221*af8870a9STimothy Aiken     Tau_x[i] = c_tau * h / fastest_wave;
222*af8870a9STimothy Aiken   }
223*af8870a9STimothy Aiken }
224*af8870a9STimothy Aiken 
225*af8870a9STimothy Aiken // *****************************************************************************
226*af8870a9STimothy Aiken // This QFunction sets the initial conditions for shock tube
227*af8870a9STimothy Aiken // *****************************************************************************
228*af8870a9STimothy Aiken CEED_QFUNCTION(ICsShockTube)(void *ctx, CeedInt Q,
229*af8870a9STimothy Aiken                              const CeedScalar *const *in, CeedScalar *const *out) {
230*af8870a9STimothy Aiken   // Inputs
231*af8870a9STimothy Aiken   const CeedScalar (*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
232*af8870a9STimothy Aiken 
233*af8870a9STimothy Aiken   // Outputs
234*af8870a9STimothy Aiken   CeedScalar (*q0)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
235*af8870a9STimothy Aiken 
236*af8870a9STimothy Aiken   CeedPragmaSIMD
237*af8870a9STimothy Aiken   // Quadrature Point Loop
238*af8870a9STimothy Aiken   for (CeedInt i=0; i<Q; i++) {
239*af8870a9STimothy Aiken     const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]};
240*af8870a9STimothy Aiken     CeedScalar q[5];
241*af8870a9STimothy Aiken 
242*af8870a9STimothy Aiken     Exact_ShockTube(3, 0., x, 5, q, ctx);
243*af8870a9STimothy Aiken 
244*af8870a9STimothy Aiken     for (CeedInt j=0; j<5; j++)
245*af8870a9STimothy Aiken       q0[j][i] = q[j];
246*af8870a9STimothy Aiken   } // End of Quadrature Point Loop
247*af8870a9STimothy Aiken 
248*af8870a9STimothy Aiken   // Return
249*af8870a9STimothy Aiken   return 0;
250*af8870a9STimothy Aiken }
251*af8870a9STimothy Aiken 
252*af8870a9STimothy Aiken // *****************************************************************************
253*af8870a9STimothy Aiken // This QFunction implements the following formulation of Euler equations
254*af8870a9STimothy Aiken //   with explicit time stepping method
255*af8870a9STimothy Aiken //
256*af8870a9STimothy Aiken // This is 3D Euler for compressible gas dynamics in conservation
257*af8870a9STimothy Aiken //   form with state variables of density, momentum density, and total
258*af8870a9STimothy Aiken //   energy density.
259*af8870a9STimothy Aiken //
260*af8870a9STimothy Aiken // State Variables: q = ( rho, U1, U2, U3, E )
261*af8870a9STimothy Aiken //   rho - Mass Density
262*af8870a9STimothy Aiken //   Ui  - Momentum Density,      Ui = rho ui
263*af8870a9STimothy Aiken //   E   - Total Energy Density,  E  = P / (gamma - 1) + rho (u u)/2
264*af8870a9STimothy Aiken //
265*af8870a9STimothy Aiken // Euler Equations:
266*af8870a9STimothy Aiken //   drho/dt + div( U )                   = 0
267*af8870a9STimothy Aiken //   dU/dt   + div( rho (u x u) + P I3 )  = 0
268*af8870a9STimothy Aiken //   dE/dt   + div( (E + P) u )           = 0
269*af8870a9STimothy Aiken //
270*af8870a9STimothy Aiken // Equation of State:
271*af8870a9STimothy Aiken //   P = (gamma - 1) (E - rho (u u) / 2)
272*af8870a9STimothy Aiken //
273*af8870a9STimothy Aiken // Constants:
274*af8870a9STimothy Aiken //   cv              ,  Specific heat, constant volume
275*af8870a9STimothy Aiken //   cp              ,  Specific heat, constant pressure
276*af8870a9STimothy Aiken //   g               ,  Gravity
277*af8870a9STimothy Aiken //   gamma  = cp / cv,  Specific heat ratio
278*af8870a9STimothy Aiken // *****************************************************************************
279*af8870a9STimothy Aiken CEED_QFUNCTION(EulerShockTube)(void *ctx, CeedInt Q,
280*af8870a9STimothy Aiken                                const CeedScalar *const *in, CeedScalar *const *out) {
281*af8870a9STimothy Aiken   // *INDENT-OFF*
282*af8870a9STimothy Aiken   // Inputs
283*af8870a9STimothy Aiken   const CeedScalar (*q)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0],
284*af8870a9STimothy Aiken                    (*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1],
285*af8870a9STimothy Aiken                    (*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
286*af8870a9STimothy Aiken   // Outputs
287*af8870a9STimothy Aiken   CeedScalar (*v)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0],
288*af8870a9STimothy Aiken              (*dv)[5][CEED_Q_VLA] = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
289*af8870a9STimothy Aiken 
290*af8870a9STimothy Aiken   const CeedScalar gamma = 1.4;
291*af8870a9STimothy Aiken 
292*af8870a9STimothy Aiken   ShockTubeContext context = (ShockTubeContext)ctx;
293*af8870a9STimothy Aiken   const CeedScalar Cyzb  = context->Cyzb;
294*af8870a9STimothy Aiken   const CeedScalar Byzb  = context->Byzb;
295*af8870a9STimothy Aiken   const CeedScalar c_tau = context->c_tau;
296*af8870a9STimothy Aiken 
297*af8870a9STimothy Aiken   CeedPragmaSIMD
298*af8870a9STimothy Aiken   // Quadrature Point Loop
299*af8870a9STimothy Aiken   for (CeedInt i=0; i<Q; i++) {
300*af8870a9STimothy Aiken     // *INDENT-OFF*
301*af8870a9STimothy Aiken     // Setup
302*af8870a9STimothy Aiken     // -- Interp in
303*af8870a9STimothy Aiken     const CeedScalar rho        =   q[0][i];
304*af8870a9STimothy Aiken     const CeedScalar u[3]       =  {q[1][i] / rho,
305*af8870a9STimothy Aiken                                     q[2][i] / rho,
306*af8870a9STimothy Aiken                                     q[3][i] / rho
307*af8870a9STimothy Aiken                                    };
308*af8870a9STimothy Aiken     const CeedScalar E          =   q[4][i];
309*af8870a9STimothy Aiken     const CeedScalar drho[3]    =  {dq[0][0][i],
310*af8870a9STimothy Aiken                                     dq[1][0][i],
311*af8870a9STimothy Aiken                                     dq[2][0][i]
312*af8870a9STimothy Aiken                                    };
313*af8870a9STimothy Aiken     const CeedScalar dU[3][3]   = {{dq[0][1][i],
314*af8870a9STimothy Aiken                                     dq[1][1][i],
315*af8870a9STimothy Aiken                                     dq[2][1][i]},
316*af8870a9STimothy Aiken                                    {dq[0][2][i],
317*af8870a9STimothy Aiken                                     dq[1][2][i],
318*af8870a9STimothy Aiken                                     dq[2][2][i]},
319*af8870a9STimothy Aiken                                    {dq[0][3][i],
320*af8870a9STimothy Aiken                                     dq[1][3][i],
321*af8870a9STimothy Aiken                                     dq[2][3][i]}
322*af8870a9STimothy Aiken                                   };
323*af8870a9STimothy Aiken     const CeedScalar dE[3]      =  {dq[0][4][i],
324*af8870a9STimothy Aiken                                     dq[1][4][i],
325*af8870a9STimothy Aiken                                     dq[2][4][i]
326*af8870a9STimothy Aiken                                    };
327*af8870a9STimothy Aiken     // -- Interp-to-Interp q_data
328*af8870a9STimothy Aiken     const CeedScalar wdetJ      =   q_data[0][i];
329*af8870a9STimothy Aiken     // -- Interp-to-Grad q_data
330*af8870a9STimothy Aiken     // ---- Inverse of change of coordinate matrix: X_i,j
331*af8870a9STimothy Aiken     // *INDENT-OFF*
332*af8870a9STimothy Aiken     const CeedScalar dXdx[3][3] = {{q_data[1][i],
333*af8870a9STimothy Aiken                                     q_data[2][i],
334*af8870a9STimothy Aiken                                     q_data[3][i]},
335*af8870a9STimothy Aiken                                    {q_data[4][i],
336*af8870a9STimothy Aiken                                     q_data[5][i],
337*af8870a9STimothy Aiken                                     q_data[6][i]},
338*af8870a9STimothy Aiken                                    {q_data[7][i],
339*af8870a9STimothy Aiken                                     q_data[8][i],
340*af8870a9STimothy Aiken                                     q_data[9][i]}
341*af8870a9STimothy Aiken                                   };
342*af8870a9STimothy Aiken     // dU/dx
343*af8870a9STimothy Aiken     CeedScalar du[3][3] = {{0}};
344*af8870a9STimothy Aiken     CeedScalar drhodx[3] = {0};
345*af8870a9STimothy Aiken     CeedScalar dEdx[3] = {0};
346*af8870a9STimothy Aiken     CeedScalar dUdx[3][3] = {{0}};
347*af8870a9STimothy Aiken     CeedScalar dXdxdXdxT[3][3] = {{0}};
348*af8870a9STimothy Aiken     for (int j=0; j<3; j++) {
349*af8870a9STimothy Aiken       for (int k=0; k<3; k++) {
350*af8870a9STimothy Aiken         du[j][k] = (dU[j][k] - drho[k]*u[j]) / rho;
351*af8870a9STimothy Aiken         drhodx[j] += drho[k] * dXdx[k][j];
352*af8870a9STimothy Aiken         dEdx[j] += dE[k] * dXdx[k][j];
353*af8870a9STimothy Aiken         for (int l=0; l<3; l++) {
354*af8870a9STimothy Aiken           dUdx[j][k] += dU[j][l] * dXdx[l][k];
355*af8870a9STimothy Aiken           dXdxdXdxT[j][k] += dXdx[j][l]*dXdx[k][l];  //dXdx_j,k * dXdx_k,j
356*af8870a9STimothy Aiken         }
357*af8870a9STimothy Aiken       }
358*af8870a9STimothy Aiken     }
359*af8870a9STimothy Aiken 
360*af8870a9STimothy Aiken     // *INDENT-ON*
361*af8870a9STimothy Aiken     const CeedScalar
362*af8870a9STimothy Aiken     E_kinetic  = 0.5 * rho * (u[0]*u[0] + u[1]*u[1] + u[2]*u[2]),
363*af8870a9STimothy Aiken     E_internal = E - E_kinetic,
364*af8870a9STimothy Aiken     P          = E_internal * (gamma - 1); // P = pressure
365*af8870a9STimothy Aiken 
366*af8870a9STimothy Aiken     // The Physics
367*af8870a9STimothy Aiken     // Zero v and dv so all future terms can safely sum into it
368*af8870a9STimothy Aiken     for (int j=0; j<5; j++) {
369*af8870a9STimothy Aiken       v[j][i] = 0;
370*af8870a9STimothy Aiken       for (int k=0; k<3; k++)
371*af8870a9STimothy Aiken         dv[k][j][i] = 0;
372*af8870a9STimothy Aiken     }
373*af8870a9STimothy Aiken 
374*af8870a9STimothy Aiken     // -- Density
375*af8870a9STimothy Aiken     // ---- u rho
376*af8870a9STimothy Aiken     for (int j=0; j<3; j++)
377*af8870a9STimothy Aiken       dv[j][0][i]  += wdetJ*(rho*u[0]*dXdx[j][0] + rho*u[1]*dXdx[j][1] +
378*af8870a9STimothy Aiken                              rho*u[2]*dXdx[j][2]);
379*af8870a9STimothy Aiken     // -- Momentum
380*af8870a9STimothy Aiken     // ---- rho (u x u) + P I3
381*af8870a9STimothy Aiken     for (int j=0; j<3; j++)
382*af8870a9STimothy Aiken       for (int k=0; k<3; k++)
383*af8870a9STimothy Aiken         dv[k][j+1][i]  += wdetJ*((rho*u[j]*u[0] + (j==0?P:0))*dXdx[k][0] +
384*af8870a9STimothy Aiken                                  (rho*u[j]*u[1] + (j==1?P:0))*dXdx[k][1] +
385*af8870a9STimothy Aiken                                  (rho*u[j]*u[2] + (j==2?P:0))*dXdx[k][2]);
386*af8870a9STimothy Aiken     // -- Total Energy Density
387*af8870a9STimothy Aiken     // ---- (E + P) u
388*af8870a9STimothy Aiken     for (int j=0; j<3; j++)
389*af8870a9STimothy Aiken       dv[j][4][i]  += wdetJ * (E + P) * (u[0]*dXdx[j][0] + u[1]*dXdx[j][1] +
390*af8870a9STimothy Aiken                                          u[2]*dXdx[j][2]);
391*af8870a9STimothy Aiken 
392*af8870a9STimothy Aiken     // -- YZB stabilization
393*af8870a9STimothy Aiken     if (context->yzb) {
394*af8870a9STimothy Aiken       CeedScalar drho_norm = 0.0;         // magnitude of the density gradient
395*af8870a9STimothy Aiken       CeedScalar j_vec[3] = {0.0};        // unit vector aligned with the density gradient
396*af8870a9STimothy Aiken       CeedScalar h_shock = 0.0;           // element lengthscale
397*af8870a9STimothy Aiken       CeedScalar acoustic_vel = 0.0;      // characteristic velocity, acoustic speed
398*af8870a9STimothy Aiken       CeedScalar tau_shock = 0.0;         // timescale
399*af8870a9STimothy Aiken       CeedScalar nu_shock = 0.0;          // artificial diffusion
400*af8870a9STimothy Aiken 
401*af8870a9STimothy Aiken       // Unit vector aligned with the density gradient
402*af8870a9STimothy Aiken       drho_norm = sqrt(drhodx[0]*drhodx[0] + drhodx[1]*drhodx[1] +
403*af8870a9STimothy Aiken                        drhodx[2]*drhodx[2]);
404*af8870a9STimothy Aiken       for (int j=0; j<3; j++)
405*af8870a9STimothy Aiken         j_vec[j] = drhodx[j] / (drho_norm + 1e-20);
406*af8870a9STimothy Aiken 
407*af8870a9STimothy Aiken       if (drho_norm == 0.0) {
408*af8870a9STimothy Aiken         nu_shock = 0.0;
409*af8870a9STimothy Aiken       } else {
410*af8870a9STimothy Aiken         h_shock = Covariant_length_along_vector(j_vec, dXdx);
411*af8870a9STimothy Aiken         h_shock /= Cyzb;
412*af8870a9STimothy Aiken         acoustic_vel = sqrt(gamma*P/rho);
413*af8870a9STimothy Aiken         tau_shock = h_shock / (2*acoustic_vel) * pow(drho_norm * h_shock / rho, Byzb);
414*af8870a9STimothy Aiken         nu_shock = fabs(tau_shock * acoustic_vel * acoustic_vel);
415*af8870a9STimothy Aiken       }
416*af8870a9STimothy Aiken 
417*af8870a9STimothy Aiken       for (int j=0; j<3; j++)
418*af8870a9STimothy Aiken         dv[j][0][i] -= wdetJ * nu_shock * drhodx[j];
419*af8870a9STimothy Aiken 
420*af8870a9STimothy Aiken       for (int k=0; k<3; k++)
421*af8870a9STimothy Aiken         for (int j=0; j<3; j++)
422*af8870a9STimothy Aiken           dv[j][k][i] -= wdetJ * nu_shock * du[k][j];
423*af8870a9STimothy Aiken 
424*af8870a9STimothy Aiken       for (int j=0; j<3; j++)
425*af8870a9STimothy Aiken         dv[j][4][i] -= wdetJ * nu_shock * dEdx[j];
426*af8870a9STimothy Aiken     }
427*af8870a9STimothy Aiken 
428*af8870a9STimothy Aiken     // Stabilization
429*af8870a9STimothy Aiken     // Need the Jacobian for the advective fluxes for stabilization
430*af8870a9STimothy Aiken     //    indexed as: jacob_F_conv[direction][flux component][solution component]
431*af8870a9STimothy Aiken     CeedScalar jacob_F_conv[3][5][5] = {{{0.}}};
432*af8870a9STimothy Aiken     ConvectiveFluxJacobian_Euler(jacob_F_conv, rho, u, E, gamma);
433*af8870a9STimothy Aiken 
434*af8870a9STimothy Aiken 
435*af8870a9STimothy Aiken     // dqdx collects drhodx, dUdx and dEdx in one vector
436*af8870a9STimothy Aiken     CeedScalar dqdx[5][3];
437*af8870a9STimothy Aiken     for (int j=0; j<3; j++) {
438*af8870a9STimothy Aiken       dqdx[0][j] = drhodx[j];
439*af8870a9STimothy Aiken       dqdx[4][j] = dEdx[j];
440*af8870a9STimothy Aiken       for (int k=0; k<3; k++)
441*af8870a9STimothy Aiken         dqdx[k+1][j] = dUdx[k][j];
442*af8870a9STimothy Aiken     }
443*af8870a9STimothy Aiken 
444*af8870a9STimothy Aiken     // strong_conv = dF/dq * dq/dx    (Strong convection)
445*af8870a9STimothy Aiken     CeedScalar strong_conv[5] = {0};
446*af8870a9STimothy Aiken     for (int j=0; j<3; j++)
447*af8870a9STimothy Aiken       for (int k=0; k<5; k++)
448*af8870a9STimothy Aiken         for (int l=0; l<5; l++)
449*af8870a9STimothy Aiken           strong_conv[k] += jacob_F_conv[j][k][l] * dqdx[l][j];
450*af8870a9STimothy Aiken 
451*af8870a9STimothy Aiken     // Stabilization
452*af8870a9STimothy Aiken     // -- Tau elements
453*af8870a9STimothy Aiken     const CeedScalar sound_speed = sqrt(gamma * P / rho);
454*af8870a9STimothy Aiken     CeedScalar Tau_x[3] = {0.};
455*af8870a9STimothy Aiken     Tau_spatial(Tau_x, dXdx, u, sound_speed, c_tau);
456*af8870a9STimothy Aiken 
457*af8870a9STimothy Aiken     CeedScalar stab[5][3] = {0};
458*af8870a9STimothy Aiken     switch (context->stabilization) {
459*af8870a9STimothy Aiken     case 0:        // Galerkin
460*af8870a9STimothy Aiken       break;
461*af8870a9STimothy Aiken     case 1:        // SU
462*af8870a9STimothy Aiken       for (int j=0; j<3; j++)
463*af8870a9STimothy Aiken         for (int k=0; k<5; k++)
464*af8870a9STimothy Aiken           for (int l=0; l<5; l++) {
465*af8870a9STimothy Aiken             stab[k][j] += jacob_F_conv[j][k][l] * Tau_x[j] * strong_conv[l];
466*af8870a9STimothy Aiken           }
467*af8870a9STimothy Aiken       for (int j=0; j<5; j++)
468*af8870a9STimothy Aiken         for (int k=0; k<3; k++)
469*af8870a9STimothy Aiken           dv[k][j][i] -= wdetJ*(stab[j][0] * dXdx[k][0] +
470*af8870a9STimothy Aiken                                 stab[j][1] * dXdx[k][1] +
471*af8870a9STimothy Aiken                                 stab[j][2] * dXdx[k][2]);
472*af8870a9STimothy Aiken       break;
473*af8870a9STimothy Aiken     }
474*af8870a9STimothy Aiken 
475*af8870a9STimothy Aiken   } // End Quadrature Point Loop
476*af8870a9STimothy Aiken 
477*af8870a9STimothy Aiken   // Return
478*af8870a9STimothy Aiken   return 0;
479*af8870a9STimothy Aiken }
480*af8870a9STimothy Aiken 
481*af8870a9STimothy Aiken #endif // shocktube_h
482