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