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