xref: /honee/qfunctions/stabilization.h (revision 2b916ea7fa53b5c2584160b9274b1b14ca18ff4f)
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 /// Helper functions for computing stabilization terms of a newtonian simulation
10 
11 #ifndef stabilization_h
12 #define stabilization_h
13 
14 #include <ceed.h>
15 
16 #include "newtonian_state.h"
17 
18 // *****************************************************************************
19 // Helper function for computing the variation in primitive variables,
20 //   given Tau_d
21 // *****************************************************************************
22 CEED_QFUNCTION_HELPER void dYFromTau(CeedScalar Y[5], CeedScalar Tau_d[3], CeedScalar dY[5]) {
23   dY[0] = Tau_d[0] * Y[0];
24   dY[1] = Tau_d[1] * Y[1];
25   dY[2] = Tau_d[1] * Y[2];
26   dY[3] = Tau_d[1] * Y[3];
27   dY[4] = Tau_d[2] * Y[4];
28 }
29 
30 // *****************************************************************************
31 // Helper functions for computing the stabilization terms
32 // *****************************************************************************
33 CEED_QFUNCTION_HELPER void StabilizationMatrix(NewtonianIdealGasContext gas, State s, CeedScalar Tau_d[3], CeedScalar R[5], const CeedScalar x[3],
34                                                CeedScalar stab[5][3]) {
35   CeedScalar        dY[5];
36   const CeedScalar  dx_i[3] = {0};
37   StateConservative dF[3];
38   // Zero stab so all future terms can safely sum into it
39   for (CeedInt i = 0; i < 5; i++) {
40     for (CeedInt j = 0; j < 3; j++) stab[i][j] = 0;
41   }
42   dYFromTau(R, Tau_d, dY);
43   State ds = StateFromY_fwd(gas, s, dY, x, dx_i);
44   FluxInviscid_fwd(gas, s, ds, dF);
45   for (CeedInt i = 0; i < 3; i++) {
46     CeedScalar dF_i[5];
47     UnpackState_U(dF[i], dF_i);
48     for (CeedInt j = 0; j < 5; j++) stab[j][i] += dF_i[j];
49   }
50 }
51 
52 CEED_QFUNCTION_HELPER void Stabilization(NewtonianIdealGasContext gas, State s, CeedScalar Tau_d[3], State ds[3], CeedScalar U_dot[5],
53                                          const CeedScalar body_force[5], const CeedScalar x[3], CeedScalar stab[5][3]) {
54   // -- Stabilization method: none (Galerkin), SU, or SUPG
55   CeedScalar R[5] = {0};
56   switch (gas->stabilization) {
57     case STAB_NONE:
58       break;
59     case STAB_SU:
60       FluxInviscidStrong(gas, s, ds, R);
61       break;
62     case STAB_SUPG:
63       FluxInviscidStrong(gas, s, ds, R);
64       for (CeedInt j = 0; j < 5; j++) R[j] += U_dot[j] - body_force[j];
65       break;
66   }
67   StabilizationMatrix(gas, s, Tau_d, R, x, stab);
68 }
69 
70 // *****************************************************************************
71 // Helper function for computing Tau elements (stabilization constant)
72 //   Model from:
73 //     PHASTA
74 //
75 //   Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial)
76 //
77 // *****************************************************************************
78 CEED_QFUNCTION_HELPER void Tau_diagPrim(NewtonianIdealGasContext gas, State s, const CeedScalar dXdx[3][3], const CeedScalar dt,
79                                         CeedScalar Tau_d[3]) {
80   // Context
81   const CeedScalar Ctau_t = gas->Ctau_t;
82   const CeedScalar Ctau_v = gas->Ctau_v;
83   const CeedScalar Ctau_C = gas->Ctau_C;
84   const CeedScalar Ctau_M = gas->Ctau_M;
85   const CeedScalar Ctau_E = gas->Ctau_E;
86   const CeedScalar cv     = gas->cv;
87   const CeedScalar mu     = gas->mu;
88   const CeedScalar u[3]   = {s.Y.velocity[0], s.Y.velocity[1], s.Y.velocity[2]};
89   const CeedScalar rho    = s.U.density;
90 
91   CeedScalar gijd[6];
92   CeedScalar tau;
93   CeedScalar dts;
94   CeedScalar fact;
95 
96   //*INDENT-OFF*
97   gijd[0] = dXdx[0][0] * dXdx[0][0] + dXdx[1][0] * dXdx[1][0] + dXdx[2][0] * dXdx[2][0];
98 
99   gijd[1] = dXdx[0][0] * dXdx[0][1] + dXdx[1][0] * dXdx[1][1] + dXdx[2][0] * dXdx[2][1];
100 
101   gijd[2] = dXdx[0][1] * dXdx[0][1] + dXdx[1][1] * dXdx[1][1] + dXdx[2][1] * dXdx[2][1];
102 
103   gijd[3] = dXdx[0][0] * dXdx[0][2] + dXdx[1][0] * dXdx[1][2] + dXdx[2][0] * dXdx[2][2];
104 
105   gijd[4] = dXdx[0][1] * dXdx[0][2] + dXdx[1][1] * dXdx[1][2] + dXdx[2][1] * dXdx[2][2];
106 
107   gijd[5] = dXdx[0][2] * dXdx[0][2] + dXdx[1][2] * dXdx[1][2] + dXdx[2][2] * dXdx[2][2];
108   //*INDENT-ON*
109 
110   dts = Ctau_t / dt;
111 
112   tau = rho * rho *
113             ((4. * dts * dts) + u[0] * (u[0] * gijd[0] + 2. * (u[1] * gijd[1] + u[2] * gijd[3])) + u[1] * (u[1] * gijd[2] + 2. * u[2] * gijd[4]) +
114              u[2] * u[2] * gijd[5]) +
115         Ctau_v * mu * mu *
116             (gijd[0] * gijd[0] + gijd[2] * gijd[2] + gijd[5] * gijd[5] + +2. * (gijd[1] * gijd[1] + gijd[3] * gijd[3] + gijd[4] * gijd[4]));
117 
118   fact = sqrt(tau);
119 
120   Tau_d[0] = Ctau_C * fact / (rho * (gijd[0] + gijd[2] + gijd[5])) * 0.125;
121 
122   Tau_d[1] = Ctau_M / fact;
123   Tau_d[2] = Ctau_E / (fact * cv);
124 
125   // consider putting back the way I initially had it  Ctau_E * Tau_d[1] /cv
126   //  to avoid a division if the compiler is smart enough to see that cv IS
127   // a constant that it could invert once for all elements
128   // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M
129   // OR we could absorb cv into Ctau_E but this puts more burden on user to
130   // know how to change constants with a change of fluid or units.  Same for
131   // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T)
132 }
133 
134 // *****************************************************************************
135 
136 #endif  // stabilization_h
137