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