xref: /honee/qfunctions/stabilization.h (revision 7e084dbeb599a34874b2d36cc7b30f0bb31927f0)
1ae2b091fSJames Wright // SPDX-FileCopyrightText: Copyright (c) 2017-2024, HONEE contributors.
2ae2b091fSJames Wright // SPDX-License-Identifier: Apache-2.0 OR BSD-2-Clause
3d1b9ef12SLeila Ghaffari 
4d1b9ef12SLeila Ghaffari /// @file
5d1b9ef12SLeila Ghaffari /// Helper functions for computing stabilization terms of a newtonian simulation
63e17a7a1SJames Wright #include <ceed/types.h>
72b916ea7SJeremy L Thompson 
8d0cce58aSJeremy L Thompson #include "newtonian_state.h"
9d1b9ef12SLeila Ghaffari 
10d1b9ef12SLeila Ghaffari // *****************************************************************************
1104e40bb6SJeremy L Thompson // Helper function for computing the variation in primitive variables, given Tau_d
12d1b9ef12SLeila Ghaffari // *****************************************************************************
13*7e084dbeSJames Wright CEED_QFUNCTION_HELPER void dYFromTau(const CeedScalar Y[5], const CeedScalar Tau_d[3], CeedScalar dY[5]) {
14d1b9ef12SLeila Ghaffari   dY[0] = Tau_d[0] * Y[0];
15d1b9ef12SLeila Ghaffari   dY[1] = Tau_d[1] * Y[1];
16d1b9ef12SLeila Ghaffari   dY[2] = Tau_d[1] * Y[2];
17d1b9ef12SLeila Ghaffari   dY[3] = Tau_d[1] * Y[3];
18d1b9ef12SLeila Ghaffari   dY[4] = Tau_d[2] * Y[4];
19d1b9ef12SLeila Ghaffari }
20d1b9ef12SLeila Ghaffari 
21d1b9ef12SLeila Ghaffari // *****************************************************************************
22d1b9ef12SLeila Ghaffari // Helper functions for computing the stabilization terms
23d1b9ef12SLeila Ghaffari // *****************************************************************************
24*7e084dbeSJames Wright CEED_QFUNCTION_HELPER void StabilizationMatrix(const NewtonianIdealGasContext gas, const State s, const CeedScalar Tau_d[3],
25*7e084dbeSJames Wright                                                const CeedScalar strong_residual[5], CeedScalar stab[5][3]) {
26d1b9ef12SLeila Ghaffari   CeedScalar        dY[5];
27d1b9ef12SLeila Ghaffari   StateConservative dF[3];
28d1b9ef12SLeila Ghaffari   // Zero stab so all future terms can safely sum into it
292b916ea7SJeremy L Thompson   for (CeedInt i = 0; i < 5; i++) {
302b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) stab[i][j] = 0;
312b916ea7SJeremy L Thompson   }
3251d861cbSJames Wright   dYFromTau(strong_residual, Tau_d, dY);
33edcfef1bSKenneth E. Jansen   State ds = StateFromY_fwd(gas, s, dY);
34d1b9ef12SLeila Ghaffari   FluxInviscid_fwd(gas, s, ds, dF);
35d1b9ef12SLeila Ghaffari   for (CeedInt i = 0; i < 3; i++) {
36d1b9ef12SLeila Ghaffari     CeedScalar dF_i[5];
37d1b9ef12SLeila Ghaffari     UnpackState_U(dF[i], dF_i);
382b916ea7SJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) stab[j][i] += dF_i[j];
39d1b9ef12SLeila Ghaffari   }
40d1b9ef12SLeila Ghaffari }
41d1b9ef12SLeila Ghaffari 
42*7e084dbeSJames Wright CEED_QFUNCTION_HELPER void Stabilization(const NewtonianIdealGasContext gas, const State s, const CeedScalar Tau_d[3], const State ds[3],
43*7e084dbeSJames Wright                                          const CeedScalar U_dot[5], const CeedScalar body_force[5], const CeedScalar divFdiff[5],
44*7e084dbeSJames Wright                                          CeedScalar stab[5][3]) {
45d1b9ef12SLeila Ghaffari   // -- Stabilization method: none (Galerkin), SU, or SUPG
4651d861cbSJames Wright   CeedScalar strong_residual[5] = {0};
47d1b9ef12SLeila Ghaffari   switch (gas->stabilization) {
48d1b9ef12SLeila Ghaffari     case STAB_NONE:
49d1b9ef12SLeila Ghaffari       break;
50d1b9ef12SLeila Ghaffari     case STAB_SU:
5151d861cbSJames Wright       FluxInviscidStrong(gas, s, ds, strong_residual);
52d1b9ef12SLeila Ghaffari       break;
53d1b9ef12SLeila Ghaffari     case STAB_SUPG:
5451d861cbSJames Wright       FluxInviscidStrong(gas, s, ds, strong_residual);
558c85b835SJames Wright       for (CeedInt j = 0; j < 5; j++) strong_residual[j] += U_dot[j] - body_force[j] + divFdiff[j];
56d1b9ef12SLeila Ghaffari       break;
57d1b9ef12SLeila Ghaffari   }
5851d861cbSJames Wright   StabilizationMatrix(gas, s, Tau_d, strong_residual, stab);
59d1b9ef12SLeila Ghaffari }
60d1b9ef12SLeila Ghaffari 
61d1b9ef12SLeila Ghaffari // *****************************************************************************
62d1b9ef12SLeila Ghaffari // Helper function for computing Tau elements (stabilization constant)
63d1b9ef12SLeila Ghaffari //   Model from:
64d1b9ef12SLeila Ghaffari //     PHASTA
65d1b9ef12SLeila Ghaffari //
66d1b9ef12SLeila Ghaffari //   Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial)
67d1b9ef12SLeila Ghaffari // *****************************************************************************
682b916ea7SJeremy L Thompson CEED_QFUNCTION_HELPER void Tau_diagPrim(NewtonianIdealGasContext gas, State s, const CeedScalar dXdx[3][3], const CeedScalar dt,
692b916ea7SJeremy L Thompson                                         CeedScalar Tau_d[3]) {
70d1b9ef12SLeila Ghaffari   // Context
71d1b9ef12SLeila Ghaffari   const CeedScalar Ctau_t = gas->Ctau_t;
72d1b9ef12SLeila Ghaffari   const CeedScalar Ctau_v = gas->Ctau_v;
73d1b9ef12SLeila Ghaffari   const CeedScalar Ctau_C = gas->Ctau_C;
74d1b9ef12SLeila Ghaffari   const CeedScalar Ctau_M = gas->Ctau_M;
75d1b9ef12SLeila Ghaffari   const CeedScalar Ctau_E = gas->Ctau_E;
76d1b9ef12SLeila Ghaffari   const CeedScalar cv     = gas->cv;
77d1b9ef12SLeila Ghaffari   const CeedScalar mu     = gas->mu;
78d1b9ef12SLeila Ghaffari   const CeedScalar rho    = s.U.density;
79d1b9ef12SLeila Ghaffari 
80d1b9ef12SLeila Ghaffari   CeedScalar tau;
81d1b9ef12SLeila Ghaffari   CeedScalar dts;
82d1b9ef12SLeila Ghaffari   CeedScalar fact;
83d1b9ef12SLeila Ghaffari 
84d5eb0e14SJames Wright   CeedScalar gijd_mat[3][3] = {{0.}}, velocity_term;
85d5eb0e14SJames Wright   MatMat3(dXdx, dXdx, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat);
86d1b9ef12SLeila Ghaffari 
87d1b9ef12SLeila Ghaffari   dts = Ctau_t / dt;
88d1b9ef12SLeila Ghaffari 
89d5eb0e14SJames Wright   {  // u_i g_ij u_j
90d5eb0e14SJames Wright     CeedScalar gij_uj[3] = {0.};
91d5eb0e14SJames Wright     MatVec3(gijd_mat, s.Y.velocity, CEED_NOTRANSPOSE, gij_uj);
92d5eb0e14SJames Wright     velocity_term = Dot3(s.Y.velocity, gij_uj);
93d5eb0e14SJames Wright   }
94d5eb0e14SJames Wright 
95d5eb0e14SJames Wright   tau = Square(rho) * (4. * Square(dts) + velocity_term) + Ctau_v * Square(mu) * DotN((CeedScalar *)gijd_mat, (CeedScalar *)gijd_mat, 9);
96d1b9ef12SLeila Ghaffari 
97d1b9ef12SLeila Ghaffari   fact = sqrt(tau);
98d1b9ef12SLeila Ghaffari 
99d5eb0e14SJames Wright   Tau_d[0] = Ctau_C * fact / (rho * (gijd_mat[0][0] + gijd_mat[1][1] + gijd_mat[2][2])) * 0.125;
100d1b9ef12SLeila Ghaffari   Tau_d[1] = Ctau_M / fact;
101d1b9ef12SLeila Ghaffari   Tau_d[2] = Ctau_E / (fact * cv);
102d1b9ef12SLeila Ghaffari 
10304e40bb6SJeremy L Thompson   // consider putting back the way I initially had it
10404e40bb6SJeremy 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
105d1b9ef12SLeila Ghaffari   // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M
10604e40bb6SJeremy 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
107d1b9ef12SLeila Ghaffari   // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T)
108d1b9ef12SLeila Ghaffari }
109