xref: /honee/qfunctions/stabilization.h (revision 51d861cb7f8a1024b4b66d954632f12b64fdb494)
1d1b9ef12SLeila Ghaffari // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2d1b9ef12SLeila Ghaffari // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3d1b9ef12SLeila Ghaffari //
4d1b9ef12SLeila Ghaffari // SPDX-License-Identifier: BSD-2-Clause
5d1b9ef12SLeila Ghaffari //
6d1b9ef12SLeila Ghaffari // This file is part of CEED:  http://github.com/ceed
7d1b9ef12SLeila Ghaffari 
8d1b9ef12SLeila Ghaffari /// @file
9d1b9ef12SLeila Ghaffari /// Helper functions for computing stabilization terms of a newtonian simulation
10d1b9ef12SLeila Ghaffari 
11d1b9ef12SLeila Ghaffari #ifndef stabilization_h
12d1b9ef12SLeila Ghaffari #define stabilization_h
13d1b9ef12SLeila Ghaffari 
14d1b9ef12SLeila Ghaffari #include <ceed.h>
152b916ea7SJeremy L Thompson 
16d0cce58aSJeremy L Thompson #include "newtonian_state.h"
17d1b9ef12SLeila Ghaffari 
18d1b9ef12SLeila Ghaffari // *****************************************************************************
1904e40bb6SJeremy L Thompson // Helper function for computing the variation in primitive variables, given Tau_d
20d1b9ef12SLeila Ghaffari // *****************************************************************************
212b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void dYFromTau(CeedScalar Y[5], CeedScalar Tau_d[3], CeedScalar dY[5]) {
22d1b9ef12SLeila Ghaffari   dY[0] = Tau_d[0] * Y[0];
23d1b9ef12SLeila Ghaffari   dY[1] = Tau_d[1] * Y[1];
24d1b9ef12SLeila Ghaffari   dY[2] = Tau_d[1] * Y[2];
25d1b9ef12SLeila Ghaffari   dY[3] = Tau_d[1] * Y[3];
26d1b9ef12SLeila Ghaffari   dY[4] = Tau_d[2] * Y[4];
27d1b9ef12SLeila Ghaffari }
28d1b9ef12SLeila Ghaffari 
29d1b9ef12SLeila Ghaffari // *****************************************************************************
30d1b9ef12SLeila Ghaffari // Helper functions for computing the stabilization terms
31d1b9ef12SLeila Ghaffari // *****************************************************************************
32*51d861cbSJames Wright CEED_QFUNCTION_HELPER void StabilizationMatrix(NewtonianIdealGasContext gas, State s, CeedScalar Tau_d[3], CeedScalar strong_residual[5],
33*51d861cbSJames Wright                                                CeedScalar stab[5][3]) {
34d1b9ef12SLeila Ghaffari   CeedScalar        dY[5];
35d1b9ef12SLeila Ghaffari   StateConservative dF[3];
36d1b9ef12SLeila Ghaffari   // Zero stab so all future terms can safely sum into it
372b916ea7SJeremy L Thompson   for (CeedInt i = 0; i < 5; i++) {
382b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) stab[i][j] = 0;
392b916ea7SJeremy L Thompson   }
40*51d861cbSJames Wright   dYFromTau(strong_residual, Tau_d, dY);
41edcfef1bSKenneth E. Jansen   State ds = StateFromY_fwd(gas, s, dY);
42d1b9ef12SLeila Ghaffari   FluxInviscid_fwd(gas, s, ds, dF);
43d1b9ef12SLeila Ghaffari   for (CeedInt i = 0; i < 3; i++) {
44d1b9ef12SLeila Ghaffari     CeedScalar dF_i[5];
45d1b9ef12SLeila Ghaffari     UnpackState_U(dF[i], dF_i);
462b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) stab[j][i] += dF_i[j];
47d1b9ef12SLeila Ghaffari   }
48d1b9ef12SLeila Ghaffari }
49d1b9ef12SLeila Ghaffari 
502b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void Stabilization(NewtonianIdealGasContext gas, State s, CeedScalar Tau_d[3], State ds[3], CeedScalar U_dot[5],
51edcfef1bSKenneth E. Jansen                                          const CeedScalar body_force[5], CeedScalar stab[5][3]) {
52d1b9ef12SLeila Ghaffari   // -- Stabilization method: none (Galerkin), SU, or SUPG
53*51d861cbSJames Wright   CeedScalar strong_residual[5] = {0};
54d1b9ef12SLeila Ghaffari   switch (gas->stabilization) {
55d1b9ef12SLeila Ghaffari     case STAB_NONE:
56d1b9ef12SLeila Ghaffari       break;
57d1b9ef12SLeila Ghaffari     case STAB_SU:
58*51d861cbSJames Wright       FluxInviscidStrong(gas, s, ds, strong_residual);
59d1b9ef12SLeila Ghaffari       break;
60d1b9ef12SLeila Ghaffari     case STAB_SUPG:
61*51d861cbSJames Wright       FluxInviscidStrong(gas, s, ds, strong_residual);
62*51d861cbSJames Wright       for (CeedInt j = 0; j < 5; j++) strong_residual[j] += U_dot[j] - body_force[j];
63d1b9ef12SLeila Ghaffari       break;
64d1b9ef12SLeila Ghaffari   }
65*51d861cbSJames Wright   StabilizationMatrix(gas, s, Tau_d, strong_residual, stab);
66d1b9ef12SLeila Ghaffari }
67d1b9ef12SLeila Ghaffari 
68d1b9ef12SLeila Ghaffari // *****************************************************************************
69d1b9ef12SLeila Ghaffari // Helper function for computing Tau elements (stabilization constant)
70d1b9ef12SLeila Ghaffari //   Model from:
71d1b9ef12SLeila Ghaffari //     PHASTA
72d1b9ef12SLeila Ghaffari //
73d1b9ef12SLeila Ghaffari //   Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial)
74d1b9ef12SLeila Ghaffari // *****************************************************************************
752b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void Tau_diagPrim(NewtonianIdealGasContext gas, State s, const CeedScalar dXdx[3][3], const CeedScalar dt,
762b916ea7SJeremy L Thompson                                         CeedScalar Tau_d[3]) {
77d1b9ef12SLeila Ghaffari   // Context
78d1b9ef12SLeila Ghaffari   const CeedScalar Ctau_t = gas->Ctau_t;
79d1b9ef12SLeila Ghaffari   const CeedScalar Ctau_v = gas->Ctau_v;
80d1b9ef12SLeila Ghaffari   const CeedScalar Ctau_C = gas->Ctau_C;
81d1b9ef12SLeila Ghaffari   const CeedScalar Ctau_M = gas->Ctau_M;
82d1b9ef12SLeila Ghaffari   const CeedScalar Ctau_E = gas->Ctau_E;
83d1b9ef12SLeila Ghaffari   const CeedScalar cv     = gas->cv;
84d1b9ef12SLeila Ghaffari   const CeedScalar mu     = gas->mu;
85d1b9ef12SLeila Ghaffari   const CeedScalar rho    = s.U.density;
86d1b9ef12SLeila Ghaffari 
87d1b9ef12SLeila Ghaffari   CeedScalar tau;
88d1b9ef12SLeila Ghaffari   CeedScalar dts;
89d1b9ef12SLeila Ghaffari   CeedScalar fact;
90d1b9ef12SLeila Ghaffari 
91d5eb0e14SJames Wright   CeedScalar gijd_mat[3][3] = {{0.}}, velocity_term;
92d5eb0e14SJames Wright   MatMat3(dXdx, dXdx, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat);
93d1b9ef12SLeila Ghaffari 
94d1b9ef12SLeila Ghaffari   dts = Ctau_t / dt;
95d1b9ef12SLeila Ghaffari 
96d5eb0e14SJames Wright   {  // u_i g_ij u_j
97d5eb0e14SJames Wright     CeedScalar gij_uj[3] = {0.};
98d5eb0e14SJames Wright     MatVec3(gijd_mat, s.Y.velocity, CEED_NOTRANSPOSE, gij_uj);
99d5eb0e14SJames Wright     velocity_term = Dot3(s.Y.velocity, gij_uj);
100d5eb0e14SJames Wright   }
101d5eb0e14SJames Wright 
102d5eb0e14SJames Wright   tau = Square(rho) * (4. * Square(dts) + velocity_term) + Ctau_v * Square(mu) * DotN((CeedScalar *)gijd_mat, (CeedScalar *)gijd_mat, 9);
103d1b9ef12SLeila Ghaffari 
104d1b9ef12SLeila Ghaffari   fact = sqrt(tau);
105d1b9ef12SLeila Ghaffari 
106d5eb0e14SJames Wright   Tau_d[0] = Ctau_C * fact / (rho * (gijd_mat[0][0] + gijd_mat[1][1] + gijd_mat[2][2])) * 0.125;
107d1b9ef12SLeila Ghaffari   Tau_d[1] = Ctau_M / fact;
108d1b9ef12SLeila Ghaffari   Tau_d[2] = Ctau_E / (fact * cv);
109d1b9ef12SLeila Ghaffari 
11004e40bb6SJeremy L Thompson   // consider putting back the way I initially had it
11104e40bb6SJeremy L Thompson   // 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
112d1b9ef12SLeila Ghaffari   // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M
11304e40bb6SJeremy L Thompson   // 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
114d1b9ef12SLeila Ghaffari   // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T)
115d1b9ef12SLeila Ghaffari }
116d1b9ef12SLeila Ghaffari 
117d1b9ef12SLeila Ghaffari // *****************************************************************************
118d1b9ef12SLeila Ghaffari 
119d1b9ef12SLeila Ghaffari #endif  // stabilization_h
120