xref: /libCEED/examples/fluids/qfunctions/shocktube.h (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
2ea61e9acSJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3019b7682STimothy Aiken //
4ea61e9acSJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5019b7682STimothy Aiken //
6ea61e9acSJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7019b7682STimothy Aiken 
8019b7682STimothy Aiken /// @file
9ea61e9acSJeremy 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:
12ea61e9acSJeremy L Thompson //   On the Order of Accuracy and Numerical Performance of Two Classes of Finite Volume WENO Schemes, Zhang, Zhang, and Shu (2011).
13c0b5abf0SJeremy L Thompson #include <ceed/types.h>
14c0b5abf0SJeremy L Thompson #ifndef CEED_RUNNING_JIT_PASS
15c9c2c079SJeremy L Thompson #include <math.h>
16c0b5abf0SJeremy L Thompson #include <stdbool.h>
17c0b5abf0SJeremy L Thompson #endif
182b730f8bSJeremy L Thompson 
1913fa47b2SJames Wright #include "utils.h"
20019b7682STimothy Aiken 
2197baf651SJames Wright typedef struct SetupContextShock_ *SetupContextShock;
2297baf651SJames Wright struct SetupContextShock_ {
23019b7682STimothy Aiken   CeedScalar theta0;
24019b7682STimothy Aiken   CeedScalar thetaC;
25019b7682STimothy Aiken   CeedScalar P0;
26019b7682STimothy Aiken   CeedScalar N;
27019b7682STimothy Aiken   CeedScalar cv;
28019b7682STimothy Aiken   CeedScalar cp;
29019b7682STimothy Aiken   CeedScalar time;
30019b7682STimothy Aiken   CeedScalar mid_point;
31019b7682STimothy Aiken   CeedScalar P_high;
32019b7682STimothy Aiken   CeedScalar rho_high;
33019b7682STimothy Aiken   CeedScalar P_low;
34019b7682STimothy Aiken   CeedScalar rho_low;
35019b7682STimothy Aiken };
36019b7682STimothy Aiken 
37019b7682STimothy Aiken typedef struct ShockTubeContext_ *ShockTubeContext;
38019b7682STimothy Aiken struct ShockTubeContext_ {
39019b7682STimothy Aiken   CeedScalar Cyzb;
40019b7682STimothy Aiken   CeedScalar Byzb;
41019b7682STimothy Aiken   CeedScalar c_tau;
42019b7682STimothy Aiken   bool       implicit;
43019b7682STimothy Aiken   bool       yzb;
44019b7682STimothy Aiken   int        stabilization;
45019b7682STimothy Aiken };
46019b7682STimothy Aiken 
47019b7682STimothy Aiken // *****************************************************************************
48019b7682STimothy Aiken // This function sets the initial conditions
49019b7682STimothy Aiken //
50019b7682STimothy Aiken //   Temperature:
51019b7682STimothy Aiken //     T   = P / (rho * R)
52019b7682STimothy Aiken //   Density:
53019b7682STimothy Aiken //     rho = 1.0        if x <= mid_point
54019b7682STimothy Aiken //         = 0.125      if x >  mid_point
55019b7682STimothy Aiken //   Pressure:
56019b7682STimothy Aiken //     P   = 1.0        if x <= mid_point
57019b7682STimothy Aiken //         = 0.1        if x >  mid_point
58019b7682STimothy Aiken //   Velocity:
59019b7682STimothy Aiken //     u   = 0
60019b7682STimothy Aiken //   Velocity/Momentum Density:
61019b7682STimothy Aiken //     Ui  = rho ui
62019b7682STimothy Aiken //   Total Energy:
63019b7682STimothy Aiken //     E   = P / (gamma - 1) + rho (u u)/2
64019b7682STimothy Aiken //
65019b7682STimothy Aiken // Constants:
66019b7682STimothy Aiken //   cv              ,  Specific heat, constant volume
67019b7682STimothy Aiken //   cp              ,  Specific heat, constant pressure
68019b7682STimothy Aiken //   mid_point       ,  Location of initial domain mid_point
69019b7682STimothy Aiken //   gamma  = cp / cv,  Specific heat ratio
70019b7682STimothy Aiken //
71019b7682STimothy Aiken // *****************************************************************************
72019b7682STimothy Aiken 
73019b7682STimothy Aiken // *****************************************************************************
74ea61e9acSJeremy L Thompson // This helper function provides support for the exact, time-dependent solution (currently not implemented) and IC formulation for Euler traveling
75ea61e9acSJeremy L Thompson // vortex
76019b7682STimothy Aiken // *****************************************************************************
Exact_ShockTube(CeedInt dim,CeedScalar time,const CeedScalar X[],CeedInt Nf,CeedScalar q[],void * ctx)772b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedInt Exact_ShockTube(CeedInt dim, CeedScalar time, const CeedScalar X[], CeedInt Nf, CeedScalar q[], void *ctx) {
78019b7682STimothy Aiken   // Context
7997baf651SJames Wright   const SetupContextShock context   = (SetupContextShock)ctx;
80019b7682STimothy Aiken   const CeedScalar        mid_point = context->mid_point;  // Midpoint of the domain
81019b7682STimothy Aiken   const CeedScalar        P_high    = context->P_high;     // Driver section pressure
82019b7682STimothy Aiken   const CeedScalar        rho_high  = context->rho_high;   // Driver section density
83019b7682STimothy Aiken   const CeedScalar        P_low     = context->P_low;      // Driven section pressure
84019b7682STimothy Aiken   const CeedScalar        rho_low   = context->rho_low;    // Driven section density
85019b7682STimothy Aiken 
86019b7682STimothy Aiken   // Setup
87019b7682STimothy Aiken   const CeedScalar gamma = 1.4;   // ratio of specific heats
88019b7682STimothy Aiken   const CeedScalar x     = X[0];  // Coordinates
89019b7682STimothy Aiken 
90019b7682STimothy Aiken   CeedScalar rho, P, u[3] = {0.};
91019b7682STimothy Aiken 
92019b7682STimothy Aiken   // Initial Conditions
930814d5a7SKenneth E. Jansen   if (x <= mid_point + 200 * CEED_EPSILON) {
94019b7682STimothy Aiken     rho = rho_high;
95019b7682STimothy Aiken     P   = P_high;
96019b7682STimothy Aiken   } else {
97019b7682STimothy Aiken     rho = rho_low;
98019b7682STimothy Aiken     P   = P_low;
99019b7682STimothy Aiken   }
100019b7682STimothy Aiken 
101019b7682STimothy Aiken   // Assign exact solution
102019b7682STimothy Aiken   q[0] = rho;
103019b7682STimothy Aiken   q[1] = rho * u[0];
104019b7682STimothy Aiken   q[2] = rho * u[1];
105019b7682STimothy Aiken   q[3] = rho * u[2];
106019b7682STimothy Aiken   q[4] = P / (gamma - 1.0) + rho * (u[0] * u[0]) / 2.;
107019b7682STimothy Aiken 
108019b7682STimothy Aiken   return 0;
109019b7682STimothy Aiken }
110019b7682STimothy Aiken 
111019b7682STimothy Aiken // *****************************************************************************
112019b7682STimothy Aiken // Helper function for computing flux Jacobian
113019b7682STimothy Aiken // *****************************************************************************
ConvectiveFluxJacobian_Euler(CeedScalar dF[3][5][5],const CeedScalar rho,const CeedScalar u[3],const CeedScalar E,const CeedScalar gamma)1142b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void ConvectiveFluxJacobian_Euler(CeedScalar dF[3][5][5], const CeedScalar rho, const CeedScalar u[3], const CeedScalar E,
115019b7682STimothy Aiken                                                         const CeedScalar gamma) {
116019b7682STimothy Aiken   CeedScalar u_sq = u[0] * u[0] + u[1] * u[1] + u[2] * u[2];  // Velocity square
117019b7682STimothy Aiken   for (CeedInt i = 0; i < 3; i++) {                           // Jacobian matrices for 3 directions
118019b7682STimothy Aiken     for (CeedInt j = 0; j < 3; j++) {                         // Rows of each Jacobian matrix
119019b7682STimothy Aiken       dF[i][j + 1][0] = ((i == j) ? ((gamma - 1.) * (u_sq / 2.)) : 0.) - u[i] * u[j];
120019b7682STimothy Aiken       for (CeedInt k = 0; k < 3; k++) {  // Columns of each Jacobian matrix
121019b7682STimothy Aiken         dF[i][0][k + 1]     = ((i == k) ? 1. : 0.);
1222b730f8bSJeremy 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.);
1232b730f8bSJeremy L Thompson         dF[i][4][k + 1]     = ((i == k) ? (E * gamma / rho - (gamma - 1.) * u_sq / 2.) : 0.) - (gamma - 1.) * u[i] * u[k];
124019b7682STimothy Aiken       }
125019b7682STimothy Aiken       dF[i][j + 1][4] = ((i == j) ? (gamma - 1.) : 0.);
126019b7682STimothy Aiken     }
127019b7682STimothy Aiken     dF[i][4][0] = u[i] * ((gamma - 1.) * u_sq - E * gamma / rho);
128019b7682STimothy Aiken     dF[i][4][4] = u[i] * gamma;
129019b7682STimothy Aiken   }
130019b7682STimothy Aiken }
131019b7682STimothy Aiken 
132019b7682STimothy Aiken // *****************************************************************************
133ea61e9acSJeremy L Thompson // Helper function for calculating the covariant length scale in the direction of some 3 element input vector
134019b7682STimothy Aiken //
135019b7682STimothy Aiken // Where
136019b7682STimothy Aiken //  vec         = vector that length is measured in the direction of
137019b7682STimothy Aiken //  h           = covariant element length along vec
138019b7682STimothy Aiken // *****************************************************************************
Covariant_length_along_vector(CeedScalar vec[3],const CeedScalar dXdx[3][3])1392b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER CeedScalar Covariant_length_along_vector(CeedScalar vec[3], const CeedScalar dXdx[3][3]) {
140019b7682STimothy Aiken   CeedScalar vec_norm            = sqrt(vec[0] * vec[0] + vec[1] * vec[1] + vec[2] * vec[2]);
141019b7682STimothy Aiken   CeedScalar vec_dot_jacobian[3] = {0.0};
142019b7682STimothy Aiken   for (CeedInt i = 0; i < 3; i++) {
143019b7682STimothy Aiken     for (CeedInt j = 0; j < 3; j++) {
144019b7682STimothy Aiken       vec_dot_jacobian[i] += dXdx[j][i] * vec[i];
145019b7682STimothy Aiken     }
146019b7682STimothy Aiken   }
1472b730f8bSJeremy L Thompson   CeedScalar norm_vec_dot_jacobian =
1482b730f8bSJeremy 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]);
149019b7682STimothy Aiken   CeedScalar h = 2.0 * vec_norm / norm_vec_dot_jacobian;
150019b7682STimothy Aiken   return h;
151019b7682STimothy Aiken }
152019b7682STimothy Aiken 
153019b7682STimothy Aiken // *****************************************************************************
154019b7682STimothy Aiken // Helper function for computing Tau elements (stabilization constant)
155019b7682STimothy Aiken //   Model from:
156019b7682STimothy Aiken //     Stabilized Methods for Compressible Flows, Hughes et al 2010
157019b7682STimothy Aiken //
158019b7682STimothy Aiken //   Spatial criterion #2 - Tau is a 3x3 diagonal matrix
159019b7682STimothy Aiken //   Tau[i] = c_tau h[i] Xi(Pe) / rho(A[i]) (no sum)
160019b7682STimothy Aiken //
161019b7682STimothy Aiken // Where
162019b7682STimothy Aiken //   c_tau     = stabilization constant (0.5 is reported as "optimal")
163019b7682STimothy Aiken //   h[i]      = 2 length(dxdX[i])
164019b7682STimothy Aiken //   Pe        = Peclet number ( Pe = sqrt(u u) / dot(dXdx,u) diffusivity )
165019b7682STimothy Aiken //   Xi(Pe)    = coth Pe - 1. / Pe (1. at large local Peclet number )
166ea61e9acSJeremy L Thompson //   rho(A[i]) = spectral radius of the convective flux Jacobian i, wave speed in direction i
167019b7682STimothy Aiken // *****************************************************************************
Tau_spatial(CeedScalar Tau_x[3],const CeedScalar dXdx[3][3],const CeedScalar u[3],const CeedScalar sound_speed,const CeedScalar c_tau)1682b730f8bSJeremy 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,
1692b730f8bSJeremy L Thompson                                        const CeedScalar c_tau) {
170ba6664aeSJames Wright   for (CeedInt i = 0; i < 3; i++) {
171019b7682STimothy Aiken     // length of element in direction i
1722b730f8bSJeremy L Thompson     CeedScalar h = 2 / sqrt(dXdx[0][i] * dXdx[0][i] + dXdx[1][i] * dXdx[1][i] + dXdx[2][i] * dXdx[2][i]);
173019b7682STimothy Aiken     // fastest wave in direction i
174019b7682STimothy Aiken     CeedScalar fastest_wave = fabs(u[i]) + sound_speed;
175019b7682STimothy Aiken     Tau_x[i]                = c_tau * h / fastest_wave;
176019b7682STimothy Aiken   }
177019b7682STimothy Aiken }
178019b7682STimothy Aiken 
179019b7682STimothy Aiken // *****************************************************************************
180019b7682STimothy Aiken // This QFunction sets the initial conditions for shock tube
181019b7682STimothy Aiken // *****************************************************************************
ICsShockTube(void * ctx,CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)1822b730f8bSJeremy L Thompson CEED_QFUNCTION(ICsShockTube)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
183019b7682STimothy Aiken   const CeedScalar(*X)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
184019b7682STimothy Aiken   CeedScalar(*q0)[CEED_Q_VLA]      = (CeedScalar(*)[CEED_Q_VLA])out[0];
185019b7682STimothy Aiken 
186f0b01153SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
187019b7682STimothy Aiken     const CeedScalar x[] = {X[0][i], X[1][i], X[2][i]};
188019b7682STimothy Aiken     CeedScalar       q[5];
189019b7682STimothy Aiken 
190019b7682STimothy Aiken     Exact_ShockTube(3, 0., x, 5, q, ctx);
191019b7682STimothy Aiken 
1922b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) q0[j][i] = q[j];
193f0b01153SJames Wright   }
194019b7682STimothy Aiken   return 0;
195019b7682STimothy Aiken }
196019b7682STimothy Aiken 
197019b7682STimothy Aiken // *****************************************************************************
198ea61e9acSJeremy L Thompson // This QFunction implements the following formulation of Euler equations with explicit time stepping method
199019b7682STimothy Aiken //
200ea61e9acSJeremy L Thompson // This is 3D Euler for compressible gas dynamics in conservation form with state variables of density, momentum density, and total energy density.
201019b7682STimothy Aiken //
202019b7682STimothy Aiken // State Variables: q = ( rho, U1, U2, U3, E )
203019b7682STimothy Aiken //   rho - Mass Density
204019b7682STimothy Aiken //   Ui  - Momentum Density,      Ui = rho ui
205019b7682STimothy Aiken //   E   - Total Energy Density,  E  = P / (gamma - 1) + rho (u u)/2
206019b7682STimothy Aiken //
207019b7682STimothy Aiken // Euler Equations:
208019b7682STimothy Aiken //   drho/dt + div( U )                   = 0
209019b7682STimothy Aiken //   dU/dt   + div( rho (u x u) + P I3 )  = 0
210019b7682STimothy Aiken //   dE/dt   + div( (E + P) u )           = 0
211019b7682STimothy Aiken //
212019b7682STimothy Aiken // Equation of State:
213019b7682STimothy Aiken //   P = (gamma - 1) (E - rho (u u) / 2)
214019b7682STimothy Aiken //
215019b7682STimothy Aiken // Constants:
216019b7682STimothy Aiken //   cv              ,  Specific heat, constant volume
217019b7682STimothy Aiken //   cp              ,  Specific heat, constant pressure
218019b7682STimothy Aiken //   g               ,  Gravity
219019b7682STimothy Aiken //   gamma  = cp / cv,  Specific heat ratio
220019b7682STimothy Aiken // *****************************************************************************
EulerShockTube(void * ctx,CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)2212b730f8bSJeremy L Thompson CEED_QFUNCTION(EulerShockTube)(void *ctx, CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
22246603fc5SJames Wright   const CeedScalar(*q)[CEED_Q_VLA]     = (const CeedScalar(*)[CEED_Q_VLA])in[0];
22346603fc5SJames Wright   const CeedScalar(*dq)[5][CEED_Q_VLA] = (const CeedScalar(*)[5][CEED_Q_VLA])in[1];
224f3e15844SJames Wright   const CeedScalar(*q_data)            = in[2];
22546603fc5SJames Wright   CeedScalar(*v)[CEED_Q_VLA]           = (CeedScalar(*)[CEED_Q_VLA])out[0];
22646603fc5SJames Wright   CeedScalar(*dv)[5][CEED_Q_VLA]       = (CeedScalar(*)[5][CEED_Q_VLA])out[1];
227019b7682STimothy Aiken 
228019b7682STimothy Aiken   const CeedScalar gamma = 1.4;
229019b7682STimothy Aiken 
230019b7682STimothy Aiken   ShockTubeContext context = (ShockTubeContext)ctx;
231019b7682STimothy Aiken   const CeedScalar Cyzb    = context->Cyzb;
232019b7682STimothy Aiken   const CeedScalar Byzb    = context->Byzb;
233019b7682STimothy Aiken   const CeedScalar c_tau   = context->c_tau;
234019b7682STimothy Aiken 
235f0b01153SJames Wright   CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
236019b7682STimothy Aiken     // Setup
237019b7682STimothy Aiken     // -- Interp in
238019b7682STimothy Aiken     const CeedScalar rho      = q[0][i];
2392b730f8bSJeremy L Thompson     const CeedScalar u[3]     = {q[1][i] / rho, q[2][i] / rho, q[3][i] / rho};
240019b7682STimothy Aiken     const CeedScalar E        = q[4][i];
2412b730f8bSJeremy L Thompson     const CeedScalar drho[3]  = {dq[0][0][i], dq[1][0][i], dq[2][0][i]};
2422b730f8bSJeremy L Thompson     const CeedScalar dU[3][3] = {
2432b730f8bSJeremy L Thompson         {dq[0][1][i], dq[1][1][i], dq[2][1][i]},
2442b730f8bSJeremy L Thompson         {dq[0][2][i], dq[1][2][i], dq[2][2][i]},
2452b730f8bSJeremy L Thompson         {dq[0][3][i], dq[1][3][i], dq[2][3][i]}
246019b7682STimothy Aiken     };
2472b730f8bSJeremy L Thompson     const CeedScalar dE[3] = {dq[0][4][i], dq[1][4][i], dq[2][4][i]};
248f3e15844SJames Wright     CeedScalar       wdetJ, dXdx[3][3];
249f3e15844SJames Wright     QdataUnpack_3D(Q, i, q_data, &wdetJ, dXdx);
250019b7682STimothy Aiken     // dU/dx
251019b7682STimothy Aiken     CeedScalar du[3][3]        = {{0}};
252019b7682STimothy Aiken     CeedScalar drhodx[3]       = {0};
253019b7682STimothy Aiken     CeedScalar dEdx[3]         = {0};
254019b7682STimothy Aiken     CeedScalar dUdx[3][3]      = {{0}};
255019b7682STimothy Aiken     CeedScalar dXdxdXdxT[3][3] = {{0}};
256ba6664aeSJames Wright     for (CeedInt j = 0; j < 3; j++) {
257ba6664aeSJames Wright       for (CeedInt k = 0; k < 3; k++) {
258019b7682STimothy Aiken         du[j][k] = (dU[j][k] - drho[k] * u[j]) / rho;
259019b7682STimothy Aiken         drhodx[j] += drho[k] * dXdx[k][j];
260019b7682STimothy Aiken         dEdx[j] += dE[k] * dXdx[k][j];
261ba6664aeSJames Wright         for (CeedInt l = 0; l < 3; l++) {
262019b7682STimothy Aiken           dUdx[j][k] += dU[j][l] * dXdx[l][k];
263019b7682STimothy Aiken           dXdxdXdxT[j][k] += dXdx[j][l] * dXdx[k][l];  // dXdx_j,k * dXdx_k,j
264019b7682STimothy Aiken         }
265019b7682STimothy Aiken       }
266019b7682STimothy Aiken     }
267019b7682STimothy Aiken 
2682b730f8bSJeremy 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,
269019b7682STimothy Aiken                      P = E_internal * (gamma - 1);  // P = pressure
270019b7682STimothy Aiken 
271019b7682STimothy Aiken     // The Physics
272019b7682STimothy Aiken     // Zero v and dv so all future terms can safely sum into it
273ba6664aeSJames Wright     for (CeedInt j = 0; j < 5; j++) {
274019b7682STimothy Aiken       v[j][i] = 0;
2752b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) dv[k][j][i] = 0;
276019b7682STimothy Aiken     }
277019b7682STimothy Aiken 
278019b7682STimothy Aiken     // -- Density
279019b7682STimothy Aiken     // ---- u rho
2802b730f8bSJeremy 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]);
281019b7682STimothy Aiken     // -- Momentum
282019b7682STimothy Aiken     // ---- rho (u x u) + P I3
2832b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
2842b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) {
2852b730f8bSJeremy 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] +
286019b7682STimothy Aiken                                     (rho * u[j] * u[2] + (j == 2 ? P : 0)) * dXdx[k][2]);
2872b730f8bSJeremy L Thompson       }
2882b730f8bSJeremy L Thompson     }
289019b7682STimothy Aiken     // -- Total Energy Density
290019b7682STimothy Aiken     // ---- (E + P) u
2912b730f8bSJeremy 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]);
292019b7682STimothy Aiken 
293019b7682STimothy Aiken     // -- YZB stabilization
294019b7682STimothy Aiken     if (context->yzb) {
295019b7682STimothy Aiken       CeedScalar drho_norm    = 0.0;    // magnitude of the density gradient
296019b7682STimothy Aiken       CeedScalar j_vec[3]     = {0.0};  // unit vector aligned with the density gradient
297019b7682STimothy Aiken       CeedScalar h_shock      = 0.0;    // element lengthscale
298019b7682STimothy Aiken       CeedScalar acoustic_vel = 0.0;    // characteristic velocity, acoustic speed
299019b7682STimothy Aiken       CeedScalar tau_shock    = 0.0;    // timescale
300019b7682STimothy Aiken       CeedScalar nu_shock     = 0.0;    // artificial diffusion
301019b7682STimothy Aiken 
302019b7682STimothy Aiken       // Unit vector aligned with the density gradient
3032b730f8bSJeremy L Thompson       drho_norm = sqrt(drhodx[0] * drhodx[0] + drhodx[1] * drhodx[1] + drhodx[2] * drhodx[2]);
3042b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) j_vec[j] = drhodx[j] / (drho_norm + 1e-20);
305019b7682STimothy Aiken 
306019b7682STimothy Aiken       if (drho_norm == 0.0) {
307019b7682STimothy Aiken         nu_shock = 0.0;
308019b7682STimothy Aiken       } else {
309019b7682STimothy Aiken         h_shock = Covariant_length_along_vector(j_vec, dXdx);
310019b7682STimothy Aiken         h_shock /= Cyzb;
311019b7682STimothy Aiken         acoustic_vel = sqrt(gamma * P / rho);
312019b7682STimothy Aiken         tau_shock    = h_shock / (2 * acoustic_vel) * pow(drho_norm * h_shock / rho, Byzb);
313019b7682STimothy Aiken         nu_shock     = fabs(tau_shock * acoustic_vel * acoustic_vel);
314019b7682STimothy Aiken       }
315019b7682STimothy Aiken 
3162b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][0][i] -= wdetJ * nu_shock * drhodx[j];
317019b7682STimothy Aiken 
3182b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) {
3192b730f8bSJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) dv[j][k][i] -= wdetJ * nu_shock * du[k][j];
3202b730f8bSJeremy L Thompson       }
321019b7682STimothy Aiken 
3222b730f8bSJeremy L Thompson       for (CeedInt j = 0; j < 3; j++) dv[j][4][i] -= wdetJ * nu_shock * dEdx[j];
323019b7682STimothy Aiken     }
324019b7682STimothy Aiken 
325019b7682STimothy Aiken     // Stabilization
326019b7682STimothy Aiken     // Need the Jacobian for the advective fluxes for stabilization
327019b7682STimothy Aiken     //    indexed as: jacob_F_conv[direction][flux component][solution component]
328019b7682STimothy Aiken     CeedScalar jacob_F_conv[3][5][5] = {{{0.}}};
329019b7682STimothy Aiken     ConvectiveFluxJacobian_Euler(jacob_F_conv, rho, u, E, gamma);
330019b7682STimothy Aiken 
331019b7682STimothy Aiken     // dqdx collects drhodx, dUdx and dEdx in one vector
332019b7682STimothy Aiken     CeedScalar dqdx[5][3];
333ba6664aeSJames Wright     for (CeedInt j = 0; j < 3; j++) {
334019b7682STimothy Aiken       dqdx[0][j] = drhodx[j];
335019b7682STimothy Aiken       dqdx[4][j] = dEdx[j];
3362b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 3; k++) dqdx[k + 1][j] = dUdx[k][j];
337019b7682STimothy Aiken     }
338019b7682STimothy Aiken 
339019b7682STimothy Aiken     // strong_conv = dF/dq * dq/dx    (Strong convection)
340019b7682STimothy Aiken     CeedScalar strong_conv[5] = {0};
3412b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) {
3422b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < 5; k++) {
3432b730f8bSJeremy L Thompson         for (CeedInt l = 0; l < 5; l++) strong_conv[k] += jacob_F_conv[j][k][l] * dqdx[l][j];
3442b730f8bSJeremy L Thompson       }
3452b730f8bSJeremy L Thompson     }
346019b7682STimothy Aiken 
347019b7682STimothy Aiken     // Stabilization
348019b7682STimothy Aiken     // -- Tau elements
349019b7682STimothy Aiken     const CeedScalar sound_speed = sqrt(gamma * P / rho);
350019b7682STimothy Aiken     CeedScalar       Tau_x[3]    = {0.};
351019b7682STimothy Aiken     Tau_spatial(Tau_x, dXdx, u, sound_speed, c_tau);
352019b7682STimothy Aiken 
353019b7682STimothy Aiken     CeedScalar stab[5][3] = {0};
354019b7682STimothy Aiken     switch (context->stabilization) {
355019b7682STimothy Aiken       case 0:  // Galerkin
356019b7682STimothy Aiken         break;
357019b7682STimothy Aiken       case 1:  // SU
3582b730f8bSJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) {
3592b730f8bSJeremy L Thompson           for (CeedInt k = 0; k < 5; k++) {
360ba6664aeSJames Wright             for (CeedInt l = 0; l < 5; l++) {
361019b7682STimothy Aiken               stab[k][j] += jacob_F_conv[j][k][l] * Tau_x[j] * strong_conv[l];
362019b7682STimothy Aiken             }
3632b730f8bSJeremy L Thompson           }
3642b730f8bSJeremy L Thompson         }
3652b730f8bSJeremy L Thompson         for (CeedInt j = 0; j < 5; j++) {
3662b730f8bSJeremy 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]);
3672b730f8bSJeremy L Thompson         }
368019b7682STimothy Aiken         break;
369019b7682STimothy Aiken     }
370f0b01153SJames Wright   }
371019b7682STimothy Aiken   return 0;
372019b7682STimothy Aiken }
373