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