xref: /libCEED/examples/fluids/qfunctions/stabilization.h (revision 5aed82e4fa97acf4ba24a7f10a35f5303a6798e0)
1*5aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
22b89d87eSLeila Ghaffari // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
32b89d87eSLeila Ghaffari //
42b89d87eSLeila Ghaffari // SPDX-License-Identifier: BSD-2-Clause
52b89d87eSLeila Ghaffari //
62b89d87eSLeila Ghaffari // This file is part of CEED:  http://github.com/ceed
72b89d87eSLeila Ghaffari 
82b89d87eSLeila Ghaffari /// @file
92b89d87eSLeila Ghaffari /// Helper functions for computing stabilization terms of a newtonian simulation
102b89d87eSLeila Ghaffari 
112b89d87eSLeila Ghaffari #ifndef stabilization_h
122b89d87eSLeila Ghaffari #define stabilization_h
132b89d87eSLeila Ghaffari 
142b89d87eSLeila Ghaffari #include <ceed.h>
152b730f8bSJeremy L Thompson 
16c9c2c079SJeremy L Thompson #include "newtonian_state.h"
172b89d87eSLeila Ghaffari 
182b89d87eSLeila Ghaffari // *****************************************************************************
19ea61e9acSJeremy L Thompson // Helper function for computing the variation in primitive variables, given Tau_d
202b89d87eSLeila Ghaffari // *****************************************************************************
212b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void dYFromTau(CeedScalar Y[5], CeedScalar Tau_d[3], CeedScalar dY[5]) {
222b89d87eSLeila Ghaffari   dY[0] = Tau_d[0] * Y[0];
232b89d87eSLeila Ghaffari   dY[1] = Tau_d[1] * Y[1];
242b89d87eSLeila Ghaffari   dY[2] = Tau_d[1] * Y[2];
252b89d87eSLeila Ghaffari   dY[3] = Tau_d[1] * Y[3];
262b89d87eSLeila Ghaffari   dY[4] = Tau_d[2] * Y[4];
272b89d87eSLeila Ghaffari }
282b89d87eSLeila Ghaffari 
292b89d87eSLeila Ghaffari // *****************************************************************************
302b89d87eSLeila Ghaffari // Helper functions for computing the stabilization terms
312b89d87eSLeila Ghaffari // *****************************************************************************
328c3b32bfSJames Wright CEED_QFUNCTION_HELPER void StabilizationMatrix(NewtonianIdealGasContext gas, State s, CeedScalar Tau_d[3], CeedScalar strong_residual[5],
338c3b32bfSJames Wright                                                CeedScalar stab[5][3]) {
342b89d87eSLeila Ghaffari   CeedScalar        dY[5];
352b89d87eSLeila Ghaffari   StateConservative dF[3];
362b89d87eSLeila Ghaffari   // Zero stab so all future terms can safely sum into it
372b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < 5; i++) {
382b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 3; j++) stab[i][j] = 0;
392b730f8bSJeremy L Thompson   }
408c3b32bfSJames Wright   dYFromTau(strong_residual, Tau_d, dY);
413bd61617SKenneth E. Jansen   State ds = StateFromY_fwd(gas, s, dY);
422b89d87eSLeila Ghaffari   FluxInviscid_fwd(gas, s, ds, dF);
432b89d87eSLeila Ghaffari   for (CeedInt i = 0; i < 3; i++) {
442b89d87eSLeila Ghaffari     CeedScalar dF_i[5];
452b89d87eSLeila Ghaffari     UnpackState_U(dF[i], dF_i);
462b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < 5; j++) stab[j][i] += dF_i[j];
472b89d87eSLeila Ghaffari   }
482b89d87eSLeila Ghaffari }
492b89d87eSLeila Ghaffari 
502b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void Stabilization(NewtonianIdealGasContext gas, State s, CeedScalar Tau_d[3], State ds[3], CeedScalar U_dot[5],
513bd61617SKenneth E. Jansen                                          const CeedScalar body_force[5], CeedScalar stab[5][3]) {
522b89d87eSLeila Ghaffari   // -- Stabilization method: none (Galerkin), SU, or SUPG
538c3b32bfSJames Wright   CeedScalar strong_residual[5] = {0};
542b89d87eSLeila Ghaffari   switch (gas->stabilization) {
552b89d87eSLeila Ghaffari     case STAB_NONE:
562b89d87eSLeila Ghaffari       break;
572b89d87eSLeila Ghaffari     case STAB_SU:
588c3b32bfSJames Wright       FluxInviscidStrong(gas, s, ds, strong_residual);
592b89d87eSLeila Ghaffari       break;
602b89d87eSLeila Ghaffari     case STAB_SUPG:
618c3b32bfSJames Wright       FluxInviscidStrong(gas, s, ds, strong_residual);
628c3b32bfSJames Wright       for (CeedInt j = 0; j < 5; j++) strong_residual[j] += U_dot[j] - body_force[j];
632b89d87eSLeila Ghaffari       break;
642b89d87eSLeila Ghaffari   }
658c3b32bfSJames Wright   StabilizationMatrix(gas, s, Tau_d, strong_residual, stab);
662b89d87eSLeila Ghaffari }
672b89d87eSLeila Ghaffari 
682b89d87eSLeila Ghaffari // *****************************************************************************
692b89d87eSLeila Ghaffari // Helper function for computing Tau elements (stabilization constant)
702b89d87eSLeila Ghaffari //   Model from:
712b89d87eSLeila Ghaffari //     PHASTA
722b89d87eSLeila Ghaffari //
732b89d87eSLeila Ghaffari //   Tau[i] = itau=0 which is diagonal-Shakib (3 values still but not spatial)
742b89d87eSLeila Ghaffari // *****************************************************************************
752b730f8bSJeremy L Thompson CEED_QFUNCTION_HELPER void Tau_diagPrim(NewtonianIdealGasContext gas, State s, const CeedScalar dXdx[3][3], const CeedScalar dt,
762b730f8bSJeremy L Thompson                                         CeedScalar Tau_d[3]) {
772b89d87eSLeila Ghaffari   // Context
782b89d87eSLeila Ghaffari   const CeedScalar Ctau_t = gas->Ctau_t;
792b89d87eSLeila Ghaffari   const CeedScalar Ctau_v = gas->Ctau_v;
802b89d87eSLeila Ghaffari   const CeedScalar Ctau_C = gas->Ctau_C;
812b89d87eSLeila Ghaffari   const CeedScalar Ctau_M = gas->Ctau_M;
822b89d87eSLeila Ghaffari   const CeedScalar Ctau_E = gas->Ctau_E;
832b89d87eSLeila Ghaffari   const CeedScalar cv     = gas->cv;
842b89d87eSLeila Ghaffari   const CeedScalar mu     = gas->mu;
852b89d87eSLeila Ghaffari   const CeedScalar rho    = s.U.density;
862b89d87eSLeila Ghaffari 
872b89d87eSLeila Ghaffari   CeedScalar tau;
882b89d87eSLeila Ghaffari   CeedScalar dts;
892b89d87eSLeila Ghaffari   CeedScalar fact;
902b89d87eSLeila Ghaffari 
91b9b033b3SJames Wright   CeedScalar gijd_mat[3][3] = {{0.}}, velocity_term;
92b9b033b3SJames Wright   MatMat3(dXdx, dXdx, CEED_TRANSPOSE, CEED_NOTRANSPOSE, gijd_mat);
932b89d87eSLeila Ghaffari 
942b89d87eSLeila Ghaffari   dts = Ctau_t / dt;
952b89d87eSLeila Ghaffari 
96b9b033b3SJames Wright   {  // u_i g_ij u_j
97b9b033b3SJames Wright     CeedScalar gij_uj[3] = {0.};
98b9b033b3SJames Wright     MatVec3(gijd_mat, s.Y.velocity, CEED_NOTRANSPOSE, gij_uj);
99b9b033b3SJames Wright     velocity_term = Dot3(s.Y.velocity, gij_uj);
100b9b033b3SJames Wright   }
101b9b033b3SJames Wright 
102b9b033b3SJames Wright   tau = Square(rho) * (4. * Square(dts) + velocity_term) + Ctau_v * Square(mu) * DotN((CeedScalar *)gijd_mat, (CeedScalar *)gijd_mat, 9);
1032b89d87eSLeila Ghaffari 
1042b89d87eSLeila Ghaffari   fact = sqrt(tau);
1052b89d87eSLeila Ghaffari 
106b9b033b3SJames Wright   Tau_d[0] = Ctau_C * fact / (rho * (gijd_mat[0][0] + gijd_mat[1][1] + gijd_mat[2][2])) * 0.125;
1072b89d87eSLeila Ghaffari   Tau_d[1] = Ctau_M / fact;
1082b89d87eSLeila Ghaffari   Tau_d[2] = Ctau_E / (fact * cv);
1092b89d87eSLeila Ghaffari 
110ea61e9acSJeremy L Thompson   // consider putting back the way I initially had it
111ea61e9acSJeremy 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
1122b89d87eSLeila Ghaffari   // but in that case energy tau is scaled by the product of Ctau_E * Ctau_M
113ea61e9acSJeremy 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
1142b89d87eSLeila Ghaffari   // Ctau_v * mu * mu IF AND ONLY IF we don't add viscosity law =f(T)
1152b89d87eSLeila Ghaffari }
1162b89d87eSLeila Ghaffari 
1172b89d87eSLeila Ghaffari // *****************************************************************************
1182b89d87eSLeila Ghaffari 
1192b89d87eSLeila Ghaffari #endif  // stabilization_h
120